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
--silent-imports prevent my code from being checked #2411
I'm not sure I understand this feature request. (Though it seems there are others who do, so clearly you have something important here.) Do you want mypy to exit 0 if there are errors but they are all errors that would be suppressed by --silent-imports?
If I understand your setup correctly, I think the way you are supposed to use this with --silent-imports is to explicitly pass the source package's parent directory (presumably src in your case) when type-checking the tests. But I can't confirm this without seeing exactly how you are invoking mypy, and what is in the various directories in your project. Is your project open source?
But doing so will also prevent mypy from following imports from src/ww, despite src being explicitly in MYPYPATH. Suddenly, mypy doesn't check the lines of code in tests using src/ww objects, and so I can't see any warning about the type errors I made.
error: No library stub file for module 'pytest' disapears, but so does"ww.IterableWrapper" expects no type arguments, but 1 given, and half of my code is now unchecked.
OK, so a reasonable feature would presumably a flag that silences
complaints about totally missing imports (like pytest or past) but doesn't
have the other effect of `--silent-imports` (which is to require stubs for
modules not explicitly listed on the command line).
I do think that in the meantime, most of your problems should go away if
you simply run `mypy tests --silent-imports src/ww <flags>` since that will
explicitly check all .py files in src/www.
OK, so a reasonable feature would presumably a flag that silences complaints about totally missing imports.
You'd probably want 2 flags, --silence-missing-stubs and --silence-import-error, so that people can select to ignore missing stubs, but be warned if they are forgetting to include an import, or vice-versa.
But a better approach would be the one used in most popular linters such as flake8, pylint, etc: each type of errors have a code, and you can pass the ones you wish to ignore as a coma separated list using either the command line, a code comment or a config file. Each code is also displayed at the begining of each respective warning in the tool output, so that you can grep for it easily.
This would be a more general way of solving this problem:
it works with one flag instead of many;
it's flexible enough to cover potential future new use cases;
with the config file, you can commit it in your VCS and share the checks you wish to apply with your team.
In the mean time I can just create a wrapper on top of mypy that filter out stdin manually an include it in the source.
The "code" idea is a good one, but also one that will require a lot of
changes to mypy, and it may not even work (there just are so many errors
that mypy can emit). In any case it requires a separate issue to track as
an idea.
In the short term this one is much more actionable (in fact I recommend that you try giving it a shot). Basically there are two places that check for silent_imports, they should probably check different flags (and silent_imports may have to imply both those other flags). Also see almost_silent. All the code is in mypy/build.py.
So the new release (0.470) has these new flags instead of --silent-imports. There's --ignore-missing imports and there's separately --follow-imports={normal,silent,skip,error}. In particular "silent" is a behavior that the old --silent-imports couldn't do. These can also be specified on a per-module basis in the mypy.ini config file.
@sametmax I'm closing this issue now because I don't think we're going to be able to do much more about this. If you still have a scenario that you can't solve using the new flags please open a new issue!
Activity
gvanrossum commentedon Nov 7, 2016
I'm not sure I understand this feature request. (Though it seems there are others who do, so clearly you have something important here.) Do you want mypy to exit 0 if there are errors but they are all errors that would be suppressed by
--silent-imports?If I understand your setup correctly, I think the way you are supposed to use this with
--silent-importsis to explicitly pass the source package's parent directory (presumablysrcin your case) when type-checking the tests. But I can't confirm this without seeing exactly how you are invoking mypy, and what is in the various directories in your project. Is your project open source?ddfisher commentedon Nov 7, 2016
Are you interested in silencing errors from one import in particular? You can do that with
#type: ignore, e.g.:This will cause mypy to ignore the import error on that particular line.
sametmax commentedon Nov 7, 2016
@ddfisher: I can do that for the 10 imports of my 20 files, but that's not really a clean way of doing it, isn't it?
@gvanrossum: i'll give more details in a minute.
sametmax commentedon Nov 7, 2016
So I have code in dirs:
I run mypy by doing:
mypy tests/ --warn-unused-ignores --check-untyped-defsWhen I do, I get type errors related to my use of the code from
wwintestssuch as:"ww.IterableWrapper" expects no type arguments, but 1 givenAnd this is great, this is what I want.
However, doing so will also give me errors caused by 3rd party libraries such as:
error: No library stub file for module 'pytest'Because pytest has no stubs. Or:
error: Cannot find module named 'past.builtins'Because
site-packagesis (as suggested by the mypy doc) not inMYPYPATH.The problems with those errors is that:
MYPYPATH.I can silence those errors with:
mypy tests/ --warn-unused-ignores --check-untyped-defs --silent-importsBut doing so will also prevent mypy from following imports from
src/ww, despitesrcbeing explicitly inMYPYPATH. Suddenly, mypy doesn't check the lines of code intestsusingsrc/wwobjects, and so I can't see any warning about the type errors I made.error: No library stub file for module 'pytest'disapears, but so does"ww.IterableWrapper" expects no type arguments, but 1 given, and half of my code is now unchecked.gvanrossum commentedon Nov 7, 2016
sametmax commentedon Nov 7, 2016
You'd probably want 2 flags,
--silence-missing-stubsand--silence-import-error, so that people can select to ignore missing stubs, but be warned if they are forgetting to include an import, or vice-versa.But a better approach would be the one used in most popular linters such as flake8, pylint, etc: each type of errors have a code, and you can pass the ones you wish to ignore as a coma separated list using either the command line, a code comment or a config file. Each code is also displayed at the begining of each respective warning in the tool output, so that you can grep for it easily.
This would be a more general way of solving this problem:
In the mean time I can just create a wrapper on top of mypy that filter out stdin manually an include it in the source.
P.S: This doesn't seem to work:
gvanrossum commentedon Nov 7, 2016
sametmax commentedon Nov 7, 2016
Let me open the ticket.
sametmax commentedon Nov 7, 2016
Should we close this one in favor of the other? If it's too much work, the second may never come to existence, while the first solution would.
gvanrossum commentedon Nov 8, 2016
In the short term this one is much more actionable (in fact I recommend that you try giving it a shot). Basically there are two places that check for silent_imports, they should probably check different flags (and silent_imports may have to imply both those other flags). Also see almost_silent. All the code is in mypy/build.py.
sametmax commentedon Nov 8, 2016
Ok, I'll have a look if my client give me some time for this.
gvanrossum commentedon Dec 9, 2016
Have a look at PR #2513. I think this will help you distinguish between the two meanings of
--silent-import.gvanrossum commentedon Jan 14, 2017
So the new release (0.470) has these new flags instead of --silent-imports. There's --ignore-missing imports and there's separately --follow-imports={normal,silent,skip,error}. In particular "silent" is a behavior that the old --silent-imports couldn't do. These can also be specified on a per-module basis in the mypy.ini config file.
@sametmax I'm closing this issue now because I don't think we're going to be able to do much more about this. If you still have a scenario that you can't solve using the new flags please open a new issue!