-
Notifications
You must be signed in to change notification settings - Fork 63
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
MyPy Static Type Checking #1115
base: master
Are you sure you want to change the base?
Conversation
Pull Request Test Coverage Report for Build 12554731196Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
@@ -20,6 +20,12 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | |||
### 💫 New checkers | |||
|
|||
- `unmentioned-parameter`: Provide error message when a function parameter is not mentioned by name in the function's docstring. By default, this checker is disabled. | |||
- `incompatible-argument-type`: Provide an error message when a function argument has an incompatible type. |
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.
Please do a merge from master, and then move these entries into the new "Unreleased" section
@@ -2655,6 +2655,72 @@ import python_ta | |||
python_ta.check_all(config={"pycodestyle-ignore": ["E302", "E305"]}) | |||
``` | |||
|
|||
(E9951)= |
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.
Move these into a new error section called "Mypy-based checks".
|
||
### Incompatible Argument Type (E9951) | ||
|
||
This error is identified by the StaticTypeChecker, which uses Mypy to detect issues related to type annotations in Python 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.
Rather than repeat this sentence for each of the new errors, you can include it as preamble in the new section
@@ -11,6 +11,7 @@ dependencies = [ | |||
"click >= 8.0.1, < 9", | |||
"colorama ~= 0.4.6", | |||
"jinja2 ~= 3.1.2", | |||
"mypy >= 1.13", |
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.
use ~= 1.13
instead
### Incompatible Argument Type (E9951) | ||
|
||
This error is identified by the StaticTypeChecker, which uses Mypy to detect issues related to type annotations in Python code. | ||
This error occurs when a function is called with an argument that does not match the expected type for that parameter. |
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.
For each of these, provide a direct link to the mypy error code documentation (https://mypy.readthedocs.io/en/stable/error_code_list.html)
filename = node.stream().name | ||
mypy_options = [ | ||
"--ignore-missing-imports", | ||
"--disable-error-code=call-arg", |
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'm not sure why this is disabled?
] | ||
result, _, _ = api.run([filename] + mypy_options) | ||
for line in result.splitlines(): | ||
if line.endswith("[arg-type]"): |
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.
Overall you can simplify this code as follows:
- Use one regular expression to extract the common elements from each message: the file name, line/col coordinates, error message, and error code. This is a consistent format for all messages.
- Maintain a dictionary mapping error code (e.g.
'incompatible-argument-type'
) to regular expression that extracts the relevant parts of the message (e.g.,arg_num
,func_name
,incomp_type
,exp_type
). - Then you can have just a single block of code here that first parses the common parts, then (if successful) parses the message-specific parts, and then calls
self._add_message
.
Proposed Changes
This PR integrates MYPY static type checking into a Custom PYTA Checker. The following error types have been added:
incompatible-argument-type
Used when a function argument has an incompatible type.
incompatible-assignment
Used when there is an incompatible assignment.
list-item-type-mismatch
Used when a list item has an incompatible type.
unsupported-operand-types
Used when an operation is attempted between incompatible types.
union-attr-error
Used when accessing an attribute that may not exist on a Union type.
dict-item-type-mismatch
Used when a dictionary entry has an incompatible key or value type.
...
)
Type of Change
(Write an
X
or a brief description next to the type or types that best describe your changes.)Checklist
(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the
[ ]
into a[x]
in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)Before opening your pull request:
After opening your pull request:
Questions and Comments
(Include any questions or comments you have regarding your changes.)