Skip to content
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

Allow one to specify which packages to follow from command line and comments and which do not #3998

Closed
mitar opened this issue Sep 24, 2017 · 4 comments

Comments

@mitar
Copy link

mitar commented Sep 24, 2017

I have a package A which imports multiple my package B, C and so on. All of the packages have typing annotations, but they are not public. I am trying to integrate mypy into my CI, where I install all dependencies for A and try to type check current code. The issue is that my other packages cannot be imported. On the other hand, if I use MYPYPATH to point it to site-packages directory where they were installed, mypy tries to type check many other packages which then fail (despite some of them having stubs, and other imports have # type: ignore next to them).

Now, reading documentation it seems that the expected thing would be that I generate stub files for other packages? But where do I put them? If I put them next to package files, then I have to point MYPYPATH to site-packages and we are again to square one. If I put them elsewhere, how do I easily get them through installation of requirements? Also, if my code is nicely typed, why should I be generating stub files at all and worry if they are in sync with code?

It seems that the other option would be to put my dependencies in a separate directory, but then I have to duplicate my dependencies installation and copying them into a separate directory, double work, especially because the first one is done automatically.

So I would suggest that:

  • if import A has next to it a comment # type: ignore, that package is not even tried to be imported and analyzed
  • that I could list on the command line, use things you find in MYPY, but just for package B, C, and so on (fragile, I have to keep this list manually)
  • that if a standard library has stubs, it does not try to load it and analyze it

So maybe just the first and last suggestion would already work for me. If I see that some import is blowing up, I could just add # type: ignore to it and it wouldn't. And all standard libraries would keep working. And then only those other relevant imports of B and C would matter. In that case, it would be great if mypy could automatically load from sys.path. So that I would not have to run it with:

MYPYPATH=$(python -c 'import sys; print(":".join(sys.path))') mypy ...
@gvanrossum
Copy link
Member

A problem with your proposed semantics for # type: ignore is that in general the presence of # type: ignore should not change the behavior of the checker -- it should only suppress errors generated on that line.

@mitar
Copy link
Author

mitar commented Sep 24, 2017

What about then # type: recursive ignore? :-) So to suppress any errors in imported modules as well. And then we can see the fact that checker does not go into import only as an optimization.

@gvanrossum
Copy link
Member

If you want to ignore import foo you can put this in your mypy.ini file:

[mypy]
[mypy-foo]
follow-imports = skip

FWIW I think eventually what you want will be solved by the proposal here: python/typing#84 (comment) (or something like it).

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2020

The workaround discussed above seems good enough.

@JukkaL JukkaL closed this as completed Jan 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants