Repository navigation
How to typecheck "2nd" party packages? #3350
Description
Activity
You can write or generate your own stubs for
aand updateMYPYPATHto include the directory containing those stubs (the parent of the directoryacontaininga/module.pyietc., presumably).Or, assuming the source for
atype-checks without errors, you can point mypy at packageadirectly, again by settingMYPYPATH-- the trick is that you shouldn't use the installed version ofa, because that presumably contains lots of other 3rd party packages/modules for which you would prefer the typeshed stubs.Sorry for taking so long to respond, but I haven't had the opportunity to try this out until now.
Now I've tried both approaches.
As I mentioned in my original post, writing or generating stubs incurs an unnecessary maintenance burden I would prefer to avoid, since
atype-checks without issues.Pointing MYPYPATH to a cloned repo of
ais a better approach, but requiring thatais cloned somewhere locally is also a bit annoying, especially since it is already installed and available in the virtualenv.Ideally, I would have liked to be able to explicitly whitelist specific packages inside the virtualenv's
site-packagesfolder. E.g.,MYPYPATH_WHITELIST=some_package:some_other_package, and make mypy aware that usage ofsome_packageandsome_other_packageshould be type-checked if found to be installed in the venv.Or is that something you don't want to support with mypy?
I'm working on a proposal to standardize a way to have a site-packages-like directory for stubs; see python/typing#84.
Reacted by Łukasz Langa@JelleZijlstra Does that solve the issue I have, though? I don't see the point of using
.pyifiles (i.e., stubs), if the.pyfiles themselves are already type hinted. It's like we're regressing back to requiring C header files.Or can
.pyfiles be used as stubs as well?To exemplify, I've solved the issue I describe in the previous posts by having my tox.ini copy
some_packageandsome_other_packagefrom site-packages into a temp-folder, then pointingMYPYPATHthere. I.e.,cp -R <venvpath>/lib/python3.6/site-packages/some_package /tmp/mypypath cp -R <venvpath>/lib/python3.6/site-packages/some_other_package /tmp/mypypath MYPYPATH=/tmp/mypypath mypy src/my_packageIt works, but it's an ugly hack.
The problem with using
.pyfiles is that they might only type check cleanly with specific mypy options, or only with specific mypy versions. Stub files are more likely to work consistently in multiple contexts, though even there it's possible to have issues ifOptional[...]is not used consistently. One option that we've discussed is making a tool that can automatically generate annotated.pyifiles from.pyfiles. Then a 3rd party package would ship annotated.pyfiles and automatically generated.pyifiles, and mypy would somehow automatically find and use the.pyifiles. Another option would be to implicitly ignore all type errors in 3rd party modules, but that would be less robust as mypy version incompatibilities can result in invalid inferred types for public methods, for example.One option that we've discussed is making a tool that can automatically generate annotated
.pyifiles from.pyfiles. Then a 3rd party package would ship annotated.pyfiles and automatically generated.pyifiles, and mypy would somehow automatically find and use the.pyifiles.Note that there is #3169 for preserving existing annotations by
stubgen. I hope @dmoisset will continue working on it.Just preserving annotations is not quite enough, since some types are inferred. Stubgen would have to type check the code and then print out the inferred types.
Well, to be honest, I am personally not that enamored with stubs, since I think they are redundant in the same way I think header files are redundant. I see why they exist, and why they might be required in some cases, but I don't understand why they should be required. And as my hacky workaround above shows, they aren't actually necessary at all.
A mypy user who wants to utilize 2nd/3rd party
.pyfiles from some arbitrary installed package when type checking their code, should be able to do so, don't you think? Obviously, it's then also up to the user to worry about whether the correct mypy options/versions are in effect.Anyway, I'm asking for clarification here because I want to know if you are at all open to the idea of adding the option to whitelist stuff in site-packages. If yes, I could start working on a pull request, but I realize that what I'm asking for might not be general enough for inclusion in mypy, and I don't want to waste time implementing something you don't want at all.
We are open to the idea, but it's not clear what the option should look like. Preferable a user shouldn't need to know which packages have inline annotations in them -- mypy should be able to figure this out automatically somehow. Perhaps each package should declare the presence of inline annotations. There is more discussion about this topic in #1190. Maybe we should close this issue and continue discussion in #1190?
Here's a summary of how this could move forward:
- Propose a concrete approach for how to do this (no implementation yet) in Search procedure for inline annotations #1190. Read the existing discussion first -- there are a bunch of things to consider.
- Once we reach agreement, you (or somebody else) can implement it in mypy.
- Once there is an implementation, somebody can write it up as a PEP in case the approach can be generalized to other tools beyond mypy.
Since this is not mypy-specific, I'd suggest continuing in python/typing#84.
Closing in favor of python/typing#84.
Let's say we have two separate packages,
aandb.bdepends on stuff ina, soais installed whenbis installed. E.g., there's a module inbthat does the following:If I run
mypyonb/module.pyI will get the following error:error: Cannot find module named 'a', even thoughais installed in my virtualenv.As I understand it, this is the intended behavior, and the solution is to either append
--ignore-missing-importsor add stubs to typeshed.The problem in our case is that
ais a private package we have developed for our own internal use, so typeshed is not an optionmypy --ignore-missing-imports b/module.pywill not be able to verify thatmodule.barcorrectly callsmodule.fooMYPYPATHseems to be highly discouraged, and is also not a great option becausebmight also depend on and install 3rd party stuff that isn't properly type hintedSo basically, my question is this: How can we tell mypy to include the imported package
awhen typecheckingb?I suppose we could add
.pyistubs forainside thebrepository and pointMYPYPATHthere, but that is suboptimal as it would require us to maintain the API forain two separate repositories, and entirely unnecessary since everything inais already properly type hinted.