-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add beginnings of error code support (#7267)
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.
- Loading branch information
Showing
13 changed files
with
390 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Classification of possible errors mypy can detect. | ||
These can be used for filtering specific errors. | ||
""" | ||
|
||
from typing import List | ||
from typing_extensions import Final | ||
|
||
|
||
# All created error codes are implicitly stored in this list. | ||
all_error_codes = [] # type: List[ErrorCode] | ||
|
||
|
||
class ErrorCode: | ||
def __init__(self, code: str, description: str, category: str) -> None: | ||
self.code = code | ||
self.description = description | ||
self.category = category | ||
|
||
def __str__(self) -> str: | ||
return '<ErrorCode {}>'.format(self.code) | ||
|
||
|
||
ATTR_DEFINED = ErrorCode( | ||
'attr-defined', "Check that attribute exists", 'General') # type: Final | ||
NAME_DEFINED = ErrorCode( | ||
'name-defined', "Check that name is defined", 'General') # type: Final | ||
CALL_ARG = ErrorCode( | ||
'call-arg', "Check number, names and kinds of arguments in calls", 'General') # type: Final | ||
ARG_TYPE = ErrorCode( | ||
'arg-type', "Check argument types in calls", 'General') # type: Final | ||
VALID_TYPE = ErrorCode( | ||
'valid-type', "Check that type (annotation) is valid", 'General') # type: Final | ||
MISSING_ANN = ErrorCode( | ||
'var-annotated', "Require variable annotation if type can't be inferred", | ||
'General') # type: Final | ||
OVERRIDE = ErrorCode( | ||
'override', "Check that method override is compatible with base class", | ||
'General') # type: Final | ||
RETURN_VALUE = ErrorCode( | ||
'return-value', "Check that return value is compatible with signature", | ||
'General') # type: Final | ||
ASSIGNMENT = ErrorCode( | ||
'assignment', "Check that assigned value is compatible with target", 'General') # type: Final | ||
|
||
SYNTAX = ErrorCode( | ||
'syntax', "Report syntax errors", 'General') # type: Final | ||
|
||
MISC = ErrorCode( | ||
'misc', "Miscenallenous other checks", 'General') # type: Final |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.