-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support error codes and ignoring only specific errors #7239
Comments
Super excited about this. I'm all for "short error identifiers that are chosen manually". this will be a lot easier to do now that new-semanal is done. That's what I'm doing in this mypy-runner that I wrote: https://github.com/chadrik/mypy-runner/blob/master/mypyrun.py I've avoided broadcasting that project too publicly because I know all the error codes will change once this feature is implemented officially in mypy. IIRC, one of the trickier bits is designing a sane system to allow the error messages to receive format arguments. |
The way I'm thinking about this is that error codes and message formatting are orthogonal, so hopefully this won't be an issue. |
I think this is an important feature that will help many users. I agree with the plan, here are couple comments:
I would rather prefer to show them on the left like this:
I looked at few other tools (Pylint, flake8, Pyre) there is no preferred pattern among other tools, so probably we just need to experiment with this.
Since we already support ignoring all errors in a file using |
This is to support allowing typecheckers to implement ignores for specific errors, using syntax like `# type: ignore=E1000` or `# type: ignore[type-mismatch` or some such. mypy is about to add support for ignoring specific errors following this design: python/mypy#7239 Support for extra text in type comments was implemented in CPython as https://bugs.python.org/issue36878 and in typed_ast as python/typed_ast#116.
This is to support allowing typecheckers to implement ignores for specific errors, using syntax like `# type: ignore=E1000` or `# type: ignore[type-mismatch` or some such. mypy is about to add support for ignoring specific errors following this design: python/mypy#7239 Support for extra text in type comments was implemented in CPython as https://bugs.python.org/issue36878 and in typed_ast as python/typed_ast#116.
This is to support allowing typecheckers to implement ignores for specific errors, using syntax like `# type: ignore=E1000` or `# type: ignore[type-mismatch` or some such. mypy is about to add support for ignoring specific errors following this design: python/mypy#7239 Support for extra text in type comments was implemented in CPython as https://bugs.python.org/issue36878 and in typed_ast as python/typed_ast#116.
This PR adds a foundation for error codes and implements a few error codes. It also adds support for `# type: ignore[code1, ...]` which ignores only specific error codes on a line. Only a few errors include interesting error codes at this point. I'll add support for more error codes in additional PRs. Most errors will implicitly fall back to a `misc` error code. Error codes are only shown if `--show-error-codes` is used. The error codes look like this in mypy output: ``` t.py:3: error: "str" has no attribute "trim" [attr-defined] ``` Error codes are intended to be short but human-readable. The name of an error code refers to the check that produces this error. In the above example we generate a "no attribute" error when we check whether an attribute is defined. Work towards #7239.
* In PEP 484 type comments, allow text after "# type: ignore" This is to support allowing typecheckers to implement ignores for specific errors, using syntax like `# type: ignore=E1000` or `# type: ignore[type-mismatch` or some such. mypy is about to add support for ignoring specific errors following this design: python/mypy#7239 Support for extra text in type comments was implemented in CPython as https://bugs.python.org/issue36878 and in typed_ast as python/typed_ast#116. * add test back
#7334 added a bunch of error codes (enabled by |
On how to format the error line. Please take into consideration that tools for IDE's like ale, SublimeLinter etc. have to parse such a line and break it into message, rule_name and so on. Message can have arbitrary |
@kaste Currently the error code is always at the end of the message, and two spaces separate it from the main message. I think that this should be enough to find it reliably with a regular expression, since the rest of a message should not contain double spaces followed by |
I wonder if maybe a better approach would be to add a flag to mypy that makes it report all the errors in JSON format? That would let linters skip having to manually parse things. |
All linter frameworks do regex based parsing, usually declarative in that the named capturing groups just map to the expected api of that framework. That's really easy. |
Shouldn't each error have its own code (as per every other linting tool)? maybe broad categories of errors have their place too, but eg if I want to ignore the error for recursive types
I only want to ignore that specific error, |
Mypy probably generates hundreds of different errors, and implementing and documenting an error code for all of them is a lot of work. The However, we accept PRs that add new error codes. |
It would be handy to support error codes to allow ignoring only specific error messages on each line. For example, here we only ignore an error about an incompatible override in class
B
:If there are any other errors on that line (such as "invalid type"), they will still be reported. Using the existing blanket
# type: ignore
comment risks ignoring too many errors, resulting in false negatives.For this we need these changes:
# type: ignore[code1, ..., codeN]
.This is potentially a pretty disruptive change, so we need to be careful about how to do this. Here are some ideas. There is an existing PR that implements some of this (#6472). My proposal differs in various details, but the end goal is the same.
Format of error codes. There are various options such as numeric codes, strings derived from messages (as in #6472), and short error identifiers that are chosen manually. I'm leaning towards the final option. I'd like error codes to be pretty short but still understandable, and perhaps even memorable. I'm proposing that most error codes are of the form
foo
,foo-bar
orfoo-bar-lulz
(one to three words separated by dashes).Granularity of error codes. I think that not every error message needs a separate code, since this could mean hundreds of different codes. The number of codes should small enough that the list of all codes is easy enough to browse. I'm thinking of maybe having 20-50 distinct error codes in the long term. We don't need a unique error code for every rare message. We can either use a category code (such as
indexing
for various errors related tox[y]
expressions) or a fallback misc/other category for all unclassified errors. The misc/other category also makes adoption easy -- we can initially have lots of errors in this category, and gradually migrate additional errors to more specific codes.Displaying error codes. We could display error codes like this in mypy output:
The rationale is that the error code is usually not important, so we shouldn't make it too prominent. This is also one of the reasons why the error codes should be pretty short. This way we could probably enable error codes by default, as they are perhaps not too distracting.
Implementing error codes. My idea would be to have error codes totally separate from error messages. Each error reporting call would optionally include the error code (it would default to a generic other/misc category code). Error codes would defined with some metadata in a new error code registry module. The metadata could be used for grouping and help output. Here's a rough idea (this will need some iteration):
The motivation is that no dynamic magic is required, and everything is very explicit.
Errors from plugins. I haven't thought about this much, but plugins should be able to provide a list of error code objects. These could be shown in the error code help output. Maybe plugins should generally define relatively few error codes per plugin. It would be okay to just have a single error code for all errors from a plugin, and plugins can also reuse predefined error codes.
Sample error codes. Here are few actual error codes we might want to define (these are not final and may need iteration):
The idea is that the name of the error code typically refers to what is being checked (whether an attribute is defined; whether an argument is valid in a call; validity of a method override). Short names are preferred, as long as they are sufficiently descriptive. For errors from optional strictness options we name the error code
no-x
, wherex
refers to what is being disallowed.Postponed topics. We could also migrate some existing strictness flags to error codes, and to allow enabling and disabling particular errors globally or per file. These feel less important than being able to ignore specific errors on a line right now, but they would be nice features to have. We'll also want a way to display all available error codes through a command line option, but again this isn't required initially.
Next steps. I'm planning to implement this soon, since we have a use case which requires this feature at Dropbox. I can reuse some code and ideas from #6472 if @chadrik isn't opposed to it.
cc @chadrik
The text was updated successfully, but these errors were encountered: