-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 command line support to enable/disable error codes #9172
Add command line support to enable/disable error codes #9172
Conversation
@@ -3,19 +3,25 @@ | |||
These can be used for filtering specific errors. | |||
""" | |||
|
|||
from typing import List | |||
from typing import Dict, List, Set | |||
from typing_extensions import Final | |||
|
|||
|
|||
# All created error codes are implicitly stored in this list. | |||
all_error_codes = [] # type: List[ErrorCode] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This list seemed so far unused, and for my use case was not the ideal data structure, so I added the below dict. If it's preferred I can change to adding the error codes here and iterating through.
Thanks for the PR! It would be nice to also have This is a sketch of how it could work:
Also, Initially we wouldn't have any error codes that are disabled by default, but you can test this manually by making some error code disabled by default. Tests can use |
@JukkaL Thanks for getting back to me. Your implementation plan is clear and clean, I'll implement this tomorrow. |
@JukkaL I have what looks like a working implementation for this now, but I have failing tests. When I disable error codes in a test it looks like possibly that state is persisting between cases that run subsequently. Do you have an established/preferred way way to clean something like this up? |
Implemented a reset for the error codes that get changed, and |
Hey @JukkaL , no rush but looking for feedback on this PR when somebody is free or direction for the next steps to contributing this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates! This is almost there, but I'd like to get rid of error code object mutation.
Sorry for the slow response, I'll try to respond faster on the next iteration.
mypy/errorcodes.py
Outdated
self.code = code | ||
self.description = description | ||
self.category = category | ||
self.default_enabled = default_enabled | ||
self.enabled = default_enabled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like having mutable state here. I think that it's better to filter error codes based on the enabled/disabled sets (my other comments contain additional context).
@@ -351,7 +351,9 @@ def add_error_info(self, info: ErrorInfo) -> None: | |||
self._add_error_info(file, info) | |||
|
|||
def is_ignored_error(self, line: int, info: ErrorInfo, ignores: Dict[int, List[str]]) -> bool: | |||
if line not in ignores: | |||
if info.code and info.code.enabled is False: | |||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can pass the enabled and disabled error codes to Errors
, and we can use them together with the default_enabled
attribute to decide whether to ignore them or not.
This way there will be less global mutable state, and it will be easier to have per-file enabled/disabled sets in the future.
mypy/main.py
Outdated
invalid_error_codes = process_error_codes(disabled_codes, enable=False) | ||
|
||
if invalid_error_codes: | ||
parser.error("Disabled error codes provided invalid values: (%s)" % |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error message is a bit verbose. What about "Invalid error code(s): %s"
?
mypy/main.py
Outdated
invalid_error_codes = process_error_codes(enabled_codes, enable=True) | ||
|
||
if invalid_error_codes: | ||
parser.error("Enabled error codes provided invalid values: (%s)" % |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to above.
mypy/test/testcheck.py
Outdated
options.disable_error_code).union(set(options.enable_error_code)) | ||
for code in altered_error_codes: | ||
error_code = errorcodes.error_codes[code] | ||
error_code.enabled = error_code.default_enabled |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once error code objects aren't mutated, this can be removed.
@@ -1501,3 +1501,48 @@ class ShouldNotBeFine(x): ... # E: Class cannot subclass 'x' (has type 'Any') | |||
disallow_subclassing_any = True | |||
\[mypy-m] | |||
disallow_subclassing_any = False | |||
|
|||
[case testDisableErrorCode] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests for invalid error codes?
docs/source/command_line.rst
Outdated
|
||
This flag allows enabling one or multiple error codes globally. | ||
|
||
NOTE: Will override disabled error codes from the --disable-error-code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nist: For consistency, don't use all caps (above we use "Note: ..."). Use a full sentence, such as "This will ...".
mypy/main.py
Outdated
@@ -984,6 +1012,20 @@ def process_cache_map(parser: argparse.ArgumentParser, | |||
options.cache_map[source] = (meta_file, data_file) | |||
|
|||
|
|||
def process_error_codes(codes: Iterable[str], enable: bool) -> List[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When error code code objects aren't mutated, this can go away.
Need rebase btw |
Add ability to enable error codes globally from the command line Add ability to disable error codes globally from the command line Enabling error codes will always override disabling error codes Update documentation to include new flags
e7333a2
to
1382f44
Compare
@JukkaL Switched back to passing sets of error codes to Errors, addressed your other comments. Let me know if there's anything else. Sorry for the short delay getting back to you, was moving house at the weekend and running without internet, will try to respond quickly to any further required iteration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates! Looks good. This will be a useful feature and let us add optional checks easily, without having to add a large number of individual command-line flags.
Fixes #8975
Add the
--disable-error-code
flag to disable error codes globally.Add the
--enable-error-code
flag to enable error codes globallyThis is my first time working on this repo, so please let me know anything additional that needs completing.