-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support namespace packages from command line #5759
Comments
Update: |
I have a similar problem. Here's a minimal reproduction: $ mypy --version
mypy 0.641
$ mkdir {a,b} && touch {a,b}/a.py
$ mypy a/a.py b/a.py --namespace-packages
b/a.py: error: Duplicate module named 'a' I'm working around this by adding |
I'm running into this issue. While |
nextstrain is a namespace package (no __init__.py), which mypy doesn't yet support in imports if also named on the command-line.¹ While the previous version of this code was correct and ran just fine, mypy threw an error. The namespace package is a) more modern Python 3 style and b) better for future co-operation in the nextstrain.* namespace, so I wanted to keep it instead of caving and adding an __init__.py just for mypy. Instead, import a little more messily from nextstrain.cli.__version__. ¹ See <python/mypy#5759>.
It's on our list of issue, but that list is rather long. Perhaps one of you is interested in submitting a PR to help this along more quickly? |
It seems as if @rafales started on addressing this: master...rafales:namespace-packages I might be able to find the time to make a PR, but would appreciate some guidance on where to start in order to save myself a lot of bootstrapping time understanding how mypy is put together. |
It looks like it comes down to the options not being passed to the FindModuleCache object here: Lines 783 to 784 in b027308
|
It's more complicated, right now mypy relies on the existence of
__init__.py files to figure out package name. If you end up with
subpackage named the same as a global package, you'll end up with
confusing errors (eg. "mycompany.graphql" and "graphql").
I did start looking into the problem and fixed it enough to work for
my usecase on the branch mentioned above. But it still needs some work
before turning it into PR.
…On Fri, Feb 1, 2019 at 9:47 PM russellwinstead ***@***.***> wrote:
It looks like it comes down to the options not being passed to the FindModuleCache object here:
https://github.com/python/mypy/blob/b027308116d19dc79bf80334ff06aab7c0001e11/mypy/main.py#L783-L784
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Packages should be typechecked with `--package` flag. Previously, a typecheck error in attribute.py went unnoticed by mypy. Additionally, for `--package` to work, __init__.py files are required (at least for now, see python/mypy#5759 ).
Packages should be typechecked with `--package` flag. Previously, a typecheck error in attribute.py went unnoticed by mypy. Additionally, for `--package` to work, __init__.py files are required (at least for now, see python/mypy#5759 ).
Fixes part of python#5759 The other part is passing files arguments, which we make steps towards in python#9614
This is the successor to #9632. Things should basically be as discussed in that PR. Since #9616 is merged, this should now resolve #5759. We leave the Bazel integration with `--package-root` almost entirely untouched, save for a) one change that's a bugfix / doesn't affect the core of what `--package-root` is doing, b) another drive by bugfix that's not related to this PR. Change a) fixes the package root `__init__.py` hackery when passed absolute paths. Change b) fixes the validation logic for package roots above the current directory; it was broken if you passed `..` as a package root Since we're leaving `--package-root` alone for now, I named the new flag `--explicit-package-base` to try and avoid confusion. Doing so also matches the language used by BuildSource a little better. The new logic is summarised in the docstring of `SourceFinder.crawl_up`. Some commentary: - I change `find_sources_in_dir ` to call `crawl_up` directly to construct the BuildSource. This helps codify the fact that files discovered will use the same module names as if you passed them directly. - Doing so keeps things DRY with the more complicated logic and means, for instance, that we now do more sensible things in some cases when we recursively explore directories that have invalid package names. - Speaking of invalid package names, if we encounter a directory name with an invalid package name, we stop crawling. This is necessary because with namespace packages there's no guarantee that what we're crawling was meant to be a Python package. I add back in a check in the presence of `__init__.py` to preserve current unit tests where we raise InvalidSourceList. - The changes to modulefinder are purely cosmetic and can be ignored (there's some similar logic between the two files and this just makes sure they mirror each other closely) - One notable change is we now always use absolute paths to crawl. This makes the behaviour more predictable and addresses a common complaint: fixes #9677, fixes #8726 and others. - I figured this could use more extensive testing than a couple slow cmdline tests. Hopefully this test setup also helps clarify the behaviour :-) Co-authored-by: hauntsaninja <>
Thanks! |
Currently the
--namespace-packages
option only applies to imported modules. Files, modules and packages specified on the command line (as files or with-p
or-m
) cannot be in namespace packages. We should fix this.Follow-up to #5591.
The text was updated successfully, but these errors were encountered: