-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add command line support to enable/disable error codes #9172
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,14 +164,18 @@ def __init__(self, | |
show_error_codes: bool = False, | ||
pretty: bool = False, | ||
read_source: Optional[Callable[[str], Optional[List[str]]]] = None, | ||
show_absolute_path: bool = False) -> None: | ||
show_absolute_path: bool = False, | ||
enabled_error_codes: Optional[Set[ErrorCode]] = None, | ||
disabled_error_codes: Optional[Set[ErrorCode]] = None) -> None: | ||
self.show_error_context = show_error_context | ||
self.show_column_numbers = show_column_numbers | ||
self.show_error_codes = show_error_codes | ||
self.show_absolute_path = show_absolute_path | ||
self.pretty = pretty | ||
# We use fscache to read source code when showing snippets. | ||
self.read_source = read_source | ||
self.enabled_error_codes = enabled_error_codes or set() | ||
self.disabled_error_codes = disabled_error_codes or set() | ||
self.initialize() | ||
|
||
def initialize(self) -> None: | ||
|
@@ -195,7 +199,9 @@ def copy(self) -> 'Errors': | |
self.show_error_codes, | ||
self.pretty, | ||
self.read_source, | ||
self.show_absolute_path) | ||
self.show_absolute_path, | ||
self.enabled_error_codes, | ||
self.disabled_error_codes) | ||
new.file = self.file | ||
new.import_ctx = self.import_ctx[:] | ||
new.function_or_member = self.function_or_member[:] | ||
|
@@ -351,15 +357,25 @@ 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 self.is_error_code_enabled(info.code) is False: | ||
return True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can pass the enabled and disabled error codes to This way there will be less global mutable state, and it will be easier to have per-file enabled/disabled sets in the future. |
||
elif line not in ignores: | ||
return False | ||
elif not ignores[line]: | ||
# Empty list means that we ignore all errors | ||
return True | ||
elif info.code: | ||
elif info.code and self.is_error_code_enabled(info.code) is True: | ||
return info.code.code in ignores[line] | ||
return False | ||
|
||
def is_error_code_enabled(self, error_code: ErrorCode) -> bool: | ||
if error_code in self.disabled_error_codes: | ||
return False | ||
elif error_code in self.enabled_error_codes: | ||
return True | ||
else: | ||
return error_code.default_enabled | ||
|
||
def clear_errors_in_targets(self, path: str, targets: Set[str]) -> None: | ||
"""Remove errors in specific fine-grained targets within a file.""" | ||
if path in self.error_info_map: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1530,3 +1530,48 @@ def f(a = None): | |
no_implicit_optional = True | ||
\[mypy-m] | ||
no_implicit_optional = False | ||
|
||
[case testDisableErrorCode] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for invalid error codes? |
||
# flags: --disable-error-code attr-defined | ||
x = 'should be fine' | ||
x.trim() | ||
|
||
[case testDisableDifferentErrorCode] | ||
# flags: --disable-error-code name-defined --show-error-code | ||
x = 'should not be fine' | ||
x.trim() # E: "str" has no attribute "trim" [attr-defined] | ||
|
||
[case testDisableMultipleErrorCode] | ||
# flags: --disable-error-code attr-defined --disable-error-code return-value --show-error-code | ||
x = 'should be fine' | ||
x.trim() | ||
|
||
def bad_return_type() -> str: | ||
return None | ||
|
||
bad_return_type('no args taken!') # E: Too many arguments for "bad_return_type" [call-arg] | ||
|
||
[case testEnableErrorCode] | ||
# flags: --disable-error-code attr-defined --enable-error-code attr-defined --show-error-code | ||
x = 'should be fine' | ||
x.trim() # E: "str" has no attribute "trim" [attr-defined] | ||
|
||
[case testEnableDifferentErrorCode] | ||
# flags: --disable-error-code attr-defined --enable-error-code name-defined --show-error-code | ||
x = 'should not be fine' | ||
x.trim() # E: "str" has no attribute "trim" [attr-defined] | ||
|
||
[case testEnableMultipleErrorCode] | ||
# flags: \ | ||
--disable-error-code attr-defined \ | ||
--disable-error-code return-value \ | ||
--disable-error-code call-arg \ | ||
--enable-error-code attr-defined \ | ||
--enable-error-code return-value --show-error-code | ||
x = 'should be fine' | ||
x.trim() # E: "str" has no attribute "trim" [attr-defined] | ||
|
||
def bad_return_type() -> str: | ||
return None # E: Incompatible return value type (got "None", expected "str") [return-value] | ||
|
||
bad_return_type('no args taken!') |
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.