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

MyPy Static Type Checking #1115

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

CulmoneY
Copy link
Contributor

@CulmoneY CulmoneY commented Nov 20, 2024

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.

...

image image image )

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality) X
🐛 Bug fix (non-breaking change that fixes an issue)
♻️ Refactoring (internal change to codebase, without changing functionality)
🚦 Test update (change that only adds or modifies tests)
📚 Documentation update (change that only updates documentation)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

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:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • I have updated the project Changelog (this is required for all changes).
  • If this is my first contribution, I have added myself to the list of contributors.

After opening your pull request:

  • I have verified that the pre-commit.ci checks have passed.
  • I have verified that the CI tests have passed.
  • I have reviewed the test coverage changes reported by Coveralls.
  • I have requested a review from a project maintainer.

Questions and Comments

(Include any questions or comments you have regarding your changes.)

@CulmoneY CulmoneY marked this pull request as ready for review December 31, 2024 01:03
@coveralls
Copy link
Collaborator

coveralls commented Dec 31, 2024

Pull Request Test Coverage Report for Build 12554731196

Warning: 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

  • 86 of 90 (95.56%) changed or added relevant lines in 2 files are covered.
  • 12 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 92.003%

Changes Missing Coverage Covered Lines Changed/Added Lines %
python_ta/checkers/static_type_checker.py 74 78 94.87%
Files with Coverage Reduction New Missed Lines %
python_ta/checkers/one_iteration_checker.py 1 97.44%
python_ta/cfg/graph.py 4 93.83%
python_ta/patches/transforms.py 7 65.22%
Totals Coverage Status
Change from base Build 11696708213: 0.02%
Covered Lines: 3233
Relevant Lines: 3514

💛 - 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.
Copy link
Contributor

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)=
Copy link
Contributor

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.
Copy link
Contributor

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",
Copy link
Contributor

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.
Copy link
Contributor

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",
Copy link
Contributor

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]"):
Copy link
Contributor

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:

  1. 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.
  2. 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).
  3. 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.

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.

3 participants