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 command line support to enable/disable error codes #9172

Merged

Conversation

cph-w
Copy link
Contributor

@cph-w cph-w commented Jul 19, 2020

Fixes #8975

Add the --disable-error-code flag to disable error codes globally.
Add the --enable-error-code flag to enable error codes globally

This is my first time working on this repo, so please let me know anything additional that needs completing.

@cph-w cph-w changed the title [#8975] Add support for enable/disable error codes [#8975] Add command line support to enable/disable error codes Jul 19, 2020
@@ -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]
Copy link
Contributor Author

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.

@cph-w cph-w changed the title [#8975] Add command line support to enable/disable error codes [#8975] Add command line support to disable error codes Jul 19, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented Jul 22, 2020

Thanks for the PR! It would be nice to also have --enable-error-code available.

This is a sketch of how it could work:

  • Add a boolean attribute to ErrorCode called enabled, for example, that controls whether the error code is enabled by default. It defaults to true. If it's false, the error code is disabled by default.
  • Initially error codes that are disabled by default are in the disabled set.
  • --enable-error-code will remove an error code from the disabled set.

Also, --disable-error-code X followed by --enable-error-code X on the command line results in X being enabled.

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 --disable.. followed by --enable.. to test this.

@cph-w
Copy link
Contributor Author

cph-w commented Jul 22, 2020

@JukkaL Thanks for getting back to me. Your implementation plan is clear and clean, I'll implement this tomorrow.

@cph-w
Copy link
Contributor Author

cph-w commented Jul 23, 2020

@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?

@cph-w
Copy link
Contributor Author

cph-w commented Jul 27, 2020

Implemented a reset for the error codes that get changed, and --enable-error-code. Let me know any feedback or required changes :)

@cph-w cph-w changed the title [#8975] Add command line support to disable error codes [#8975] Add command line support to enable/disable error codes Jul 28, 2020
@cph-w
Copy link
Contributor Author

cph-w commented Jul 29, 2020

Hey @JukkaL , no rush but looking for feedback on this PR when somebody is free or direction for the next steps to contributing this.

Copy link
Collaborator

@JukkaL JukkaL left a 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.

self.code = code
self.description = description
self.category = category
self.default_enabled = default_enabled
self.enabled = default_enabled
Copy link
Collaborator

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

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)" %
Copy link
Collaborator

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)" %
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above.

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

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]
Copy link
Collaborator

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?


This flag allows enabling one or multiple error codes globally.

NOTE: Will override disabled error codes from the --disable-error-code
Copy link
Collaborator

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]:
Copy link
Collaborator

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.

@ofek
Copy link
Sponsor

ofek commented Aug 3, 2020

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
@cph-w cph-w force-pushed the issue#8975_add-support-for-disabling-error-codes branch from e7333a2 to 1382f44 Compare August 3, 2020 20:18
@gvanrossum gvanrossum changed the title [#8975] Add command line support to enable/disable error codes Add command line support to enable/disable error codes Aug 3, 2020
@cph-w
Copy link
Contributor Author

cph-w commented Aug 3, 2020

@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.

@cph-w cph-w requested a review from JukkaL August 8, 2020 14:49
Copy link
Collaborator

@JukkaL JukkaL left a 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.

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

Successfully merging this pull request may close these issues.

Add support for enabling/disabling individual error codes
3 participants