-
-
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
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,3 @@ | ||
Support PEP420 (implicit namespace packages) as `--pyargs` target when :confval:`consider_namespace_packages` is `true` in the config. | ||
|
||
Previously, this option only impacted package names, now it also impacts tests discovery. |
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. 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.
No, agree, it makes a lot of sense! From experience, such random breakages are pretty annoying. Happy to implement a feature flag and add documentation for it. 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've been thinking about the flag name & documentation.. and actually, perhaps it makes sense to simply reuse From my experience with it, right now only helps with package/module names. E.g. if there is a test inside
And we run It correctly discovers tests in both cases, but.
This is consistent with
However, according to https://docs.pytest.org/en/stable/reference/reference.html#confval-consider_namespace_packages
So the docs say it "should attempt to idenfity when collecting", but I'm not sure what this actually means here. E.g. if you have the following hierarchy:
The only difference is
From a lurk in pytest code, to me it feels that indeed it has to do more with naming than test discovery -- e.g. I think stuff in this file is mostly after we already collected candidate files to run? Line 494 in 83536b4
So perhaps if I just reuse This also limits the potential impact from the change -- there are "only" 400ish matches for this setting on github https://github.com/search?q=consider_namespace_packages&type=code (tiny amount comparing to total pytest users), and I'd imagine people who use this setting know what they are doing anyway. What do you think? 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. Hey @nicoddemus --- any thoughts on this? :) ^ 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, sorry I missed your comment. Yeah I think reusing the option makes sense, and the impact is has a small radius as you mention. 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. Thanks! Reused the option, feels like it's much more consistent now! I also added an extra 'end-to-end' test for |
||||
# 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( | ||||
|
@@ -186,7 +191,10 @@ def test_pypath(self, invocation_path: Path) -> None: | |||
module_name="pkg.test", | ||||
) | ||||
assert resolve_collection_argument( | ||||
invocation_path, "pkg", as_pypath=True | ||||
invocation_path, | ||||
"pkg", | ||||
as_pypath=True, | ||||
consider_namespace_packages=namespace_package, | ||||
) == CollectionArgument( | ||||
path=invocation_path / "src/pkg", | ||||
parts=[], | ||||
|
@@ -197,7 +205,10 @@ def test_pypath(self, invocation_path: Path) -> None: | |||
UsageError, match=r"package argument cannot contain :: selection parts" | ||||
): | ||||
resolve_collection_argument( | ||||
invocation_path, "pkg::foo::bar", as_pypath=True | ||||
invocation_path, | ||||
"pkg::foo::bar", | ||||
as_pypath=True, | ||||
consider_namespace_packages=namespace_package, | ||||
) | ||||
|
||||
def test_parametrized_name_with_colons(self, invocation_path: Path) -> None: | ||||
|
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.
Perhaps instead of just returning it blindly, we should check if this is a directory before returning?
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.
So I looked at it:
If
spec.submodule_search_locations
is set at all, thenspec.origin
will beNone
, so we can't even fall back ontoos.path.dirname(spec.origin)
.IMO it's fine to keep it as is, especially considering now this is hidden behind
consider_namespace_packages
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.
I agree... the docs might say "a path" or "a directory", but given it is a string, who knows what some loader/package might be returning...