You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Discovery strategy is still mysterious w/PEP 420 namespace packages #6989
So PEP 420 style namespace packages do seem to influence the behavior here.
Since we may have additional directories and namespace packages under src in other repositories, I'd prefer not to have to invoke mypy with src/linkedin, but there doesn't seem to be any other option.
Maybe I'm doing something wrong, so please tell me if this is pebkac, or is unsupported. Should mypy be able to discover the linkedin namespace package using src-style package layouts with PEP 420 namespaces?
This is totally mypy's fault. Namespace packages are only discovered during import following, not during initial file discovery from the command line. I recommend dropping an empty __init__.pyi (note: .pyi) in the namespace package.
Thanks for the follow up Guido. Adding an empty placeholder file isn't really an option for us, for reasons not worth going into here.
Namespace packages are only discovered during import following, not during initial file discovery from the command line.
Would it make sense, when the -p option is given, to do the import and use the resulting module's __spec__.submodule_search_locations to find the directory to start scanning? Something like the following:
mypy -p linkedin --namespace-packages would be morally equivalent to:
Alternatively, if a src directory is given, just continue to recursively traverse until you find a concrete package and .py files to start scanning.
I'm sure both of these have subtitles that make it tricky. If we must, we'll have to auto-discover the subdirectories ourselves and pass them along on the command line.
I would rather not have mypy "do the import" -- that could run code which might have undesirable side effects, or it could just fail mysteriously. (In general we're pretty adamant that mypy shouldn't ever run your code in order to check it.) Also mypy won't know that the argument refers to a namespace package until it's too late.
I think the approach you mentioned that "does work", mypy src/linkedin --namespace-packages, doesn't actually really work, because it ends up thinking that the fullly-qualified name of bar.py is foo.bar, whereas it should really be linkedin.foo.bar. This distinction becomes important when other files are being checked.
I suppose the most surprising part is that MYPYPATH=src mypy -p linkedin --namespace-package doesn't work. Possibly we could make this work with some tweaks to the implementation of the -p flag in mypy/main.py. (Although it would be slightly inconsistent, since mypy's package discovery still wouldn't work if you were to write mypy src/linkedin/foo/bar.py -- that would still think the full name is foo.bar.)
Yes! I think it would be fine if the second invocation form was a little inconsistent.
Here's how I'm thinking about it. Let's say I do this:
% touch src/linkedin/__init__.pyi
% MYPYPATH=src mypy -p linkedin --namespace-packages --no-site-packages
src/linkedin/foo/bar.py:2: error: Incompatible return value type (got "int", expected "str")
That makes sense to me; I want to check the linkedin namespace anchored underneath src. What bothers me is that I have to create a dummy __init__.pyi file to make this work, and that this doesn't work the same way:
I think we all agree that that should work. I was hoping that you'd try to come up with a PR for mypy to make it work that way, since the whole mypy team is basically super distracted by other priorities at the moment...
Activity
ilevkivskyi commentedon Jun 14, 2019
I think @gvanrossum is the best person to ask about this.
gvanrossum commentedon Jun 14, 2019
This is totally mypy's fault. Namespace packages are only discovered during import following, not during initial file discovery from the command line. I recommend dropping an empty
__init__.pyi(note:.pyi) in the namespace package.warsaw commentedon Jun 14, 2019
Thanks for the follow up Guido. Adding an empty placeholder file isn't really an option for us, for reasons not worth going into here.
Would it make sense, when the
-poption is given, to do the import and use the resulting module's__spec__.submodule_search_locationsto find the directory to start scanning? Something like the following:mypy -p linkedin --namespace-packageswould be morally equivalent to:Then you'd just iterate over the elements?
Alternatively, if a
srcdirectory is given, just continue to recursively traverse until you find a concrete package and.pyfiles to start scanning.I'm sure both of these have subtitles that make it tricky. If we must, we'll have to auto-discover the subdirectories ourselves and pass them along on the command line.
ilevkivskyi commentedon Jun 16, 2019
Btw, it looks like this is related to #6385
gvanrossum commentedon Jun 17, 2019
I would rather not have mypy "do the import" -- that could run code which might have undesirable side effects, or it could just fail mysteriously. (In general we're pretty adamant that mypy shouldn't ever run your code in order to check it.) Also mypy won't know that the argument refers to a namespace package until it's too late.
I think the approach you mentioned that "does work",
mypy src/linkedin --namespace-packages, doesn't actually really work, because it ends up thinking that the fullly-qualified name ofbar.pyisfoo.bar, whereas it should really belinkedin.foo.bar. This distinction becomes important when other files are being checked.I suppose the most surprising part is that
MYPYPATH=src mypy -p linkedin --namespace-packagedoesn't work. Possibly we could make this work with some tweaks to the implementation of the-pflag inmypy/main.py. (Although it would be slightly inconsistent, since mypy's package discovery still wouldn't work if you were to writemypy src/linkedin/foo/bar.py-- that would still think the full name isfoo.bar.)Would you be interested in giving this a try?
warsaw commentedon Jun 26, 2019
Yes! I think it would be fine if the second invocation form was a little inconsistent.
Here's how I'm thinking about it. Let's say I do this:
That makes sense to me; I want to check the
linkedinnamespace anchored underneathsrc. What bothers me is that I have to create a dummy__init__.pyifile to make this work, and that this doesn't work the same way:Is that a reasonable thing to do?
gvanrossum commentedon Jun 26, 2019
I think we all agree that that should work. I was hoping that you'd try to come up with a PR for mypy to make it work that way, since the whole mypy team is basically super distracted by other priorities at the moment...
warsaw commentedon Jun 26, 2019
Fair enough! I'll give it a go, although it likely won't be until after the week of July 4th.
warsaw commentedon Jul 17, 2019
See the above PR. I've done internal testing and it works well for us.