-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Ensure that a module within a namespace package can be found by --pyargs #1568
Conversation
CI caught a few small mistakes. I'll fix them as soon as possible. |
This PR now satisfies the test-suite, but the solution I went for is not overly robust. The only reliable way to determine if a package is a namespace package is to import it. I did shy away from importing anything at the argument parsing stage, but the result is a bit fragile. To have a reliable solution, one must either import the package during argument parsing or defer Any feedback? |
IMHO import for pyarg usage is acceptable as other consideraion, we could also change how/when pyargs are considered (i.e. move them to the collect stage) in any case i think putting this in as a "experimental" "feature" might make sense where the experimental flag is mainly used to allow for removal in case we go for the different consideration later on @The-Compiler @nicoddemus please lean in your opinions as well |
I can't say much, I've never used |
return [pathname] | ||
else: | ||
init_file = os.path.join(pathname, '__init__.py') | ||
if os.path.getsize(init_file) == 0: |
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.
Could you please put a comment explaining why do you check for the file size at this point?
Hi @taschini, first of all thanks for the work! 😁 I made a few comments on the PR, I'm mostly curious why the I've never used |
Hi @nicoddemus, I incorporated your feedback in the latest commit. Now, as for Even leaving aside this PR, emulation using $ mkdir -p foo/bar
$ touch foo/bar/__init__.py
$ python2.7 -c 'import foo.bar; print(foo.bar)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named foo.bar
$ python3.5 -c 'import foo.bar; print(foo.bar)'
<module 'foo.bar' from '.../foo/bar/__init__.py'>
$ py.test-3.5 --pyarg foo.bar
================================================================ test session starts ================================================================
platform darwin -- Python 3.5.1, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: .../pytest_bug4, inifile:
plugins: cov-2.2.1, remove-stale-bytecode-2.1
=========================================================== no tests ran in 0.00 seconds ============================================================
ERROR: file or package not found: foo.bar So the ultimate resolution to #1567 will need to import the packages, either straight away at the argument parsing stage or at the collection stage, by delaying the handling of In this PR, which I started before all the intricacies of namespace packages were clear to me, I was trying to stay away from importing the package, and I tried to implement a conservative emulation that would throw an exception in case of doubt. As it turns out, doing so naïvely would let py.test give up on the directory resolution of packages too often, so I added a little heuristic: if the The question is now how to go forward: whether you guys accept this PR as a preliminary solution or reject it, and which the two alternatives:
is preferrable. |
Hi @taschini, thanks for implementing the changes and further clarification of the issue, it is an interesting reading as I never used namespace packages myself. While I agree with the reasoning that it is acceptable to import the package during argument parsing, I think it would fit better overall to handle Apart from that, do you guys feel that perhaps the current solution is good enough, or would leave too much corner cases uncovered (I ask because it is not clear to me when you said "will inevitably leave some corner cases exposed." you meant before using |
I created a new pull request (#1597) that uses |
This PR addresses the problem highlighted in #1567 and in part #478.
The function
test_cmdline_python_namespace_package
inacceptance_test.py
tests and demonstrates the fix. First it creates the following tree wherens_pkg
is a namespace package:Then it verifies that with
hello
andworld
added the python path the following commands works as expected::For the time being I came to the conclusion that if you specify the namespace package itself, the safest solution is to explicitly flag it as an error: