Skip to content

Support PEP420 (implicit namespace packages) as --pyargs target. #13426

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

karlicoss
Copy link

Previously, when running --pyargs pkg, if you didn't have pkg/__init__.py, pytest would fail with ERROR: module or package not found: pkg (missing __init__.py?).

Now it's discovering the package and tests inside it correctly.

If used in conjunction with consider_namespace_packages in config, test modules get correct __package__ and __name__ attributes as well.

Fixes: #478

Other relevant issues:

In addition, remove "namespace" origin handling -- this value isn't used since python 3.8. See:

Previously, when running `--pyargs pkg`, if you didn't have `pkg/__init__.py`,
pytest would fail with `ERROR: module or package not found: pkg (missing __init__.py?)`.

Now it's discovering the package and tests inside it correctly.

If used in conjunction with `consider_namespace_packages` in config, test
modules get correct `__package__` and `__name__` attributes as well.

Fixes: pytest-dev#478

Other relevant issues:
- pytest-dev#2371
- pytest-dev#10569

In addition, remove `"namespace"` origin handling -- this value isn't used since python 3.8.
See:
- python/cpython#5481
- https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label May 16, 2025
"""Dotted name and parts."""
if namespace_package:
Copy link
Author

Choose a reason for hiding this comment

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

I kind of piggybacked on the existing test that covers this functionality, but not sure whether it's a bit dirty!
Please let me know if you'd prefer me to do it in some other way, e.g. create a new fixture specifically for this case. Or perhaps this should be tested in some different place rather than test_main?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--pyargs does not understand namespace packages
1 participant