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

Add support for per-module enabling/disabling of error codes #9440

Closed
hauntsaninja opened this issue Sep 13, 2020 · 6 comments · Fixed by #13502
Closed

Add support for per-module enabling/disabling of error codes #9440

hauntsaninja opened this issue Sep 13, 2020 · 6 comments · Fixed by #13502
Labels
feature needs discussion topic-configuration Configuration files and flags topic-error-reporting How we report errors

Comments

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Sep 13, 2020

Feature

#9172 implemented global error code disabling/enabling as discussed in #8975. It seems like it was always the intention to consider per-module configuration (#8975 (comment)), but we don't currently have an open issue for it.

Pitch

My personal use case is for tests. Typing tests is often just a chore, but it's nice to have whatever type safety you can get for free. I'd want to be able to do:

[mypy]
strict = True

[mypy-tests.*]
allow_untyped_defs = True
allow_untyped_calls = True
disable_error_codes = ["var-annotated"]

(For this use case, I think there's actually a fair argument to be made that we should suppress var-annotated aka "needs type annotation" errors when checking untyped defs. If this strikes you as reasonable, I can open another issue. Edit: see #8558 for this)

@gvanrossum
Copy link
Member

Seems reasonable.

You have a typo, the option is actually called disable_error_code. This triggered surprise in me, because at first I thought you were talking about show_error_codes. Is it too late to rename that flag to ignore_error_code? I guess a problem with that is that the inverse would be dont-ignore-error-code, which is kind of awkward.

@mcous
Copy link

mcous commented Nov 29, 2020

Second this request. For me, I'd really like to disable func-returns-value in my tests but keep it active in source

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 3, 2020

This sounds like a useful feature. Another potential use case would be migrating legacy code -- you might initially ignore some error codes in code that wasn't originally written with type checking in mind, but you'd want to enable all error codes in new files.

@chadrik
Copy link
Contributor

chadrik commented Dec 8, 2020

Another potential use case would be migrating legacy code

Exactly this. I wrote a tool called mypy-runner for filtering errors using error codes (first using my own error codes, then the official ones once that feature arrived) as well regexes. I'd love to not have to use my tool at all! One of the big downsides of the runner-based approach is that there's a major performance penalty: dmypy rechecks files with errors (regardless of modified timestamps), and from mypy's perspective all those files still have errors, so reruns of dmypy on our very large codebase are slow.

@nipunn1313
Copy link
Contributor

nipunn1313 commented Feb 4, 2021

Just ran into a use case for this! I'm migrating our (large) codebase to mypy-protobuf 2.0 - and it requires some backward incompatible changes to enums in pyi stubs. Ideally, I'm able to do this in many smaller diffs.

However, if a migrated module tries to import from a non-migrated module, it hits a name-defined error.

Ideally I can disable that error specifically - and reenable it later when the non-migrated module gets migrated.

Will be using ignore_errors for now - but it seems like an excessive hammer

@levsa
Copy link

levsa commented Mar 30, 2021

This would be useful to turn off override errors when using fixtures in tests, like:

class A:
     @pytest.fixture
     def b():
        pass

     @pytest.fixture
     def a(b):
          pass


class B(A):
     @pytest.fixture
     def c():
        pass

     @pytest.fixture
     def a(b, c):
          pass

@JelleZijlstra JelleZijlstra added topic-configuration Configuration files and flags topic-error-reporting How we report errors labels Mar 19, 2022
ilevkivskyi added a commit that referenced this issue Sep 9, 2022
Fixes #9440

This is a bit non-trivial because I decided to make per-module code act as overrides over main section error codes. This looks more natural no me, rather that putting an adjusted list in each section.

I also fix the inline `# mypy: ...` comment error codes, that are currently just ignored. The logic is naturally like this:
* Command line and/or config main section set global codes
* Config sections _adjust_ them per glob/module
* Inline comments adjust them again

So one can e.g. enable code globally, disable it for all tests in config, and then re-enable locally by an inline comment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature needs discussion topic-configuration Configuration files and flags topic-error-reporting How we report errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants