-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support PEP420 (implicit namespace packages) as `--pyargs` target. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -991,11 +991,19 @@ def search_pypath(module_name: str) -> str | None: | |
# ValueError: not a module name | ||
except (AttributeError, ImportError, ValueError): | ||
return None | ||
if spec is None or spec.origin is None or spec.origin == "namespace": | ||
|
||
if spec is None: | ||
return None | ||
elif spec.submodule_search_locations: | ||
return os.path.dirname(spec.origin) | ||
elif ( | ||
spec.submodule_search_locations is not None | ||
and len(spec.submodule_search_locations) > 0 | ||
): | ||
# If submodule_search_locations is set, it's a package (regular or namespace). | ||
# Typically there is a single entry, but documentation claims it can be empty too | ||
# (e.g. if the package has no physical location). | ||
return spec.submodule_search_locations[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps instead of just returning it blindly, we should check if this is a directory before returning? |
||
else: | ||
# Must be a simple module. | ||
return spec.origin | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,8 +169,13 @@ def test_dir(self, invocation_path: Path) -> None: | |
): | ||
resolve_collection_argument(invocation_path, "src/pkg::foo::bar") | ||
|
||
def test_pypath(self, invocation_path: Path) -> None: | ||
@pytest.mark.parametrize("namespace_package", [False, True]) | ||
def test_pypath(self, namespace_package: bool, invocation_path: Path) -> None: | ||
"""Dotted name and parts.""" | ||
if namespace_package: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @karlicoss, I think it is perfectly fine to reuse an existing test like this one, thanks! However functionality related to collection and packages tends to break complex test suites in subtle ways... been there done that. But to be honest not sure how we can even foresee how this will be handled in the wild, unless we put it in the wild. I'm considering adding this behind a feature flag, so if it causes havoc, we can optionally revert it... On the other hand, we can just bite the bullet and see. If this causes massive breakage, we can always revert the patch and make a hot fix. Just thinking aloud here. |
||
# Namespace package doesn't have to contain __init__py | ||
(invocation_path / "src/pkg/__init__.py").unlink() | ||
|
||
assert resolve_collection_argument( | ||
invocation_path, "pkg.test", as_pypath=True | ||
) == CollectionArgument( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a section to the docs related to this support somewhere in the docs?