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 stub files to be --excluded #10842

Closed
jdferreira opened this issue Jul 20, 2021 · 4 comments
Closed

Allow stub files to be --excluded #10842

jdferreira opened this issue Jul 20, 2021 · 4 comments
Labels

Comments

@jdferreira
Copy link

Feature

Disclaimer: I'm not sure this is a feature request or a bug report.

Allow the user to tell mypy where stubs are located but hide the errors found in there.

Pitch

I've generated thousands of stub files (with stubgen) to easily (if crudely) be able to type-check my code. But those files are not fully compliant with mypy's type checks (things like using modules without type checks, redefining variables with values of different types, functions seemingly missing from the scope, etc.). I believe the modules are correct (to the point of usability, at least), and I'm not interested in tuning those files anyway. When more manual .pyi files are created by those package owners, I'll use them instead, but for now I'm happy with the automatically generated stub.

Given that, I get hundreds of errors in my output that are useless to me, and make it harder to spot my own type errors.

However, if I run mypy --mypy-path .mypy_stubs --exclude .mypy_stubs, these errors still appear in the output. I'd like them to be hidden.

My guess is that mypy needs to check the stub files to be able to check my actual source (so they cannot be actually excluded), but the errors could be hidden from the output. Is this something that is reasonable? I could (in theory) try to look at the code and see if I can help with that, but I don't have a lot of experience contributing to open-source code.

@jdferreira jdferreira changed the title Allow the exclusion of reporting errors in stub files Allow stub files to be --excluded Jul 20, 2021
@hauntsaninja
Copy link
Collaborator

Does follow_imports = silent for the relevant module do what you want? https://mypy.readthedocs.io/en/stable/running_mypy.html#following-imports
E.g. something like:

[mypy-my_stub_package]
follow_imports = silent

@jdferreira
Copy link
Author

It does not.

I've set a minimal repo at https://github.com/jdferreira/mypy-test with to illustrate the issue.

Even with the 'silent' setting, the errors are reported.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jul 20, 2021

Thanks for the repro, the following seemed to work for me:

[[tool.mypy.overrides]]
module = "tqdm.*"
follow_imports = "silent"
follow_imports_for_stubs = true

https://mypy.readthedocs.io/en/stable/config_file.html#confval-follow_imports_for_stubs

@jdferreira
Copy link
Author

This did solve my issue! Thanks a bunch.

For reference, and because my code base uses more than tqdm, I ended up doing

[[tool.mypy.overrides]]
module = [
    'module1', 'module1.*',
    'module2', 'module2.*',
    'module3', 'module3.*',
    ...
]
follow_imports = "silent"
follow_imports_for_stubs = true

Additionally, the documentation says that the follow_imports_for_stubs property "is not supported by the mypy daemon", but I found that it seems to be supported.

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this issue Jul 21, 2021
Helps with python#10842, python#10820 and others

There have been a number of issues recently where having this spelt out
a little more explicitly would help users. The introduction of
`--exclude` also (pretty understandably) confuses users who don't realise
mypy's recursive file discovery is a little separate from its dogged
import following.

I think it could be reasonable to change mypy's behaviour so that
exclude also implies follow_imports=skip (or maybe silent), but it might
be a little finnicky (one is a regex on filenames, the other is patterns
on fully qualified module names). I'm also just wary of attempting to
change this - import following configuration is probably one of the
more complicated and poorly understood parts of mypy's UX - so passing
on that for now.
hauntsaninja added a commit that referenced this issue Aug 1, 2021
Helps with #10842, #10820 and others

There have been a number of issues recently where having this spelt out
a little more explicitly would help users. The introduction of
--exclude also (pretty understandably) confuses users who don't realise
mypy's recursive file discovery is a little separate from its dogged
import following.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants