Skip to content

--silent-imports prevent my code from being checked #2411

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

Closed
sametmax opened this issue Nov 7, 2016 · 13 comments
Closed

--silent-imports prevent my code from being checked #2411

sametmax opened this issue Nov 7, 2016 · 13 comments

Comments

@sametmax
Copy link

sametmax commented Nov 7, 2016

I have a tests directory containing unit tests for code in a src dir.

Currently I can either:

  • use --silent-imports and gets no warning for missing stubs in 3rd party library. But then any line using the code from src is not going to be checked.
  • or not silent imports, but have my tox commands fails because missing stubs will make mypy return an error code.

I need a way to silence import error, but still have my imports to be followed and checked.

@gvanrossum
Copy link
Member

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?

@ddfisher
Copy link
Collaborator

ddfisher commented Nov 7, 2016

Are you interested in silencing errors from one import in particular? You can do that with #type: ignore, e.g.:

import module_without_stubs  # type: ignore

This will cause mypy to ignore the import error on that particular line.

@sametmax
Copy link
Author

sametmax commented 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
Copy link
Author

sametmax commented Nov 7, 2016

So I have code in dirs:

  • src/ww
  • tests

I run mypy by doing:

mypy tests/ --warn-unused-ignores --check-untyped-defs

When I do, I get type errors related to my use of the code from ww in tests such as:

"ww.IterableWrapper" expects no type arguments, but 1 given

And 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-packages is (as suggested by the mypy doc) not in MYPYPATH.

The problems with those errors is that:

  • I can't fix them. I won't write all stubs for pytest, nor include each dependencies of my project in MYPYPATH.
  • Those errors remains when I fixed all errors that matter to me, such as real type errors on my part;
  • mypy return an error code, making my CI process fails.

I can silence those errors with:

mypy tests/ --warn-unused-ignores --check-untyped-defs --silent-imports

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.

@gvanrossum
Copy link
Member

gvanrossum commented Nov 7, 2016 via email

@sametmax
Copy link
Author

sametmax commented Nov 7, 2016

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.

P.S: This doesn't seem to work:

$ mypy tests --silent-imports src/ww  

usage: mypy [-h] [-v] [-V] [--python-version x.y] [--platform PLATFORM] [-2]
            [-s] [--almost-silent] [--disallow-untyped-calls]
            [--disallow-untyped-defs] [--check-untyped-defs]
            [--disallow-subclassing-any] [--warn-incomplete-stub]
            [--warn-redundant-casts] [--warn-unused-ignores]
            [--hide-error-context] [--fast-parser] [-i] [--cache-dir DIR]
            [--strict-optional]
            [--strict-optional-whitelist [GLOB [GLOB ...]]] [--pdb]
            [--show-traceback] [--stats] [--inferstats]
            [--custom-typing MODULE] [--scripts-are-modules]
            [--config-file CONFIG_FILE] [--show-column-numbers]
            [--xslt-html-report DIR] [--txt-report DIR]
            [--xslt-txt-report DIR] [--linecount-report DIR]
            [--html-report DIR] [--memory-xml-report DIR]
            [--old-html-report DIR] [--xml-report DIR]
            [--linecoverage-report DIR] [-m MODULE] [-c PROGRAM_TEXT]
            [-p PACKAGE]
            [files [files ...]]
mypy: error: unrecognized arguments: src/ww

@gvanrossum
Copy link
Member

gvanrossum commented Nov 7, 2016 via email

@sametmax
Copy link
Author

sametmax commented Nov 7, 2016

Let me open the ticket.

@sametmax
Copy link
Author

sametmax commented 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
Copy link
Member

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
Copy link
Author

sametmax commented Nov 8, 2016

Ok, I'll have a look if my client give me some time for this.

@gvanrossum
Copy link
Member

Have a look at PR #2513. I think this will help you distinguish between the two meanings of --silent-import.

@gvanrossum
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants