Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-80958: Restore unittest discovery support for namespace packages as start directory #123820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jacobtylerwalls
Copy link
Contributor

@jacobtylerwalls jacobtylerwalls commented Sep 7, 2024

Restore unittest discovery support for PEP 420 namespace packages as start directory (-s).

Rationale

There are two flavors of failures related to unittest discovery of namespace packages:

The silent failure ticket was marked as a duplicate of #68070, which was closed by a commit that removed support for namespace package discovery, but that solved neither issue. We still have a silent failure for user error (which I suppose is fine) and we still have a cryptic failure if a user attempts to provide it specifically (which is now user error according to the 3.11 revert).

We should do better than the cryptic error. But in looking into it, it seems a shame to give up so quickly when all the patch for #80958 needed to do was to cope with __name__ being None and __loader__ being set.

Notes

Re: the comment about this code being too hard to maintain: I think this regressed because of the heavy use of mocking in test_discovery.py. With real types this would have been caught by the existing test.


📚 Documentation preview 📚: https://cpython-previews--123820.org.readthedocs.build/

@picnixz
Copy link
Member

picnixz commented Sep 7, 2024

@ericvsmith, you said in #68070 (comment):

As the author of PEP-420, I think having test discovery support namespace packages is a mistake, at least in the sense of pretending every directory is a namespace package.

Had your stance changed since then? do we revert it and make it work? or should we just only focus on improving the error message?

@jacobtylerwalls
Copy link
Contributor Author

jacobtylerwalls commented Sep 7, 2024

I'm eager to hear Eric's view also. I'll just note that when the directory is provided explicitly as described in #80958, that strikes me as importantly distinct from an apporach that assumes every directory is/could be a namespace package.

The revert (and subsequent fix) here only solves the explicit discovery issue, not the implicit discovery issue which is still a silent user error either way.

@jacobtylerwalls jacobtylerwalls changed the title gh-80958 Restore unittest discovery of PEP 420 namespace packages gh-80958: Restore unittest discovery of PEP 420 namespace packages Sep 8, 2024
Copy link
Member

@methane methane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start_dir can be namespace package. But it must not affect how treat subdirs.

@methane
Copy link
Member

methane commented Sep 8, 2024

Would you add a test case?

$ find foo
foo
foo/noop
foo/noop/test_noop.py
foo/noop/no2
foo/noop/no2/__init__.py
foo/noop/no2/test_no2.py
foo/test_foo.py
foo/bar
foo/bar/__init__.py
foo/bar/test_bar.py

when unittest discover foo is executed, only test_foo and test_bar are discovered.

@erlend-aasland erlend-aasland removed their request for review October 14, 2024 22:14
@methane methane force-pushed the restore-namespace-pkg-test-discovery branch 2 times, most recently from d0b6e04 to be8d0e7 Compare October 22, 2024 09:39
@jacobtylerwalls jacobtylerwalls changed the title gh-80958: Restore unittest discovery of PEP 420 namespace packages gh-80958: Restore unittest discovery support for namespace packages as start directory Oct 22, 2024
Comment on lines +356 to +357
To avoid scanning directories unrelated to Python,
tests are not searched in subdirectories that do not contain ``__init__.py``.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is great 👍

@methane methane merged commit c75ff2e into python:main Oct 23, 2024
35 checks passed
@jacobtylerwalls jacobtylerwalls deleted the restore-namespace-pkg-test-discovery branch October 23, 2024 11:16
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants