-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
(feat) Add type annotations checker #10749
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Alvaro Frias <[email protected]>
Signed-off-by: Alvaro Frias <[email protected]>
Signed-off-by: Alvaro Frias <[email protected]>
Signed-off-by: Alvaro Frias <[email protected]>
Signed-off-by: Alvaro Frias <[email protected]>
Pierre-Sassoulas
left a comment
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.
Thank you for this PR, this looks pretty refined already.
| @@ -0,0 +1,10 @@ | |||
| def greet(name): # [missing-param-type-annotation] | |||
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.
Even the bad.py should have return type, right ?
| @@ -0,0 +1,6 @@ | |||
| def calculate_sum(numbers): # [missing-return-type-annotation] | |||
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.
Even the bad py should have param type, right ? :)
| with ``--enable=missing-return-type-annotation``. | ||
|
|
||
| The check automatically skips: | ||
|
|
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.
Maybe we can also add function and methods starting with test_ for return-type ? I always found adding -> None in all tests to be rather pointless. But then mypy would disagree and we probably want to agree with mypy. (Btw just gave me an idea of a checker to check that function starting with test_ should not return anything).
| @utils.only_required_for_messages( | ||
| "missing-return-type-annotation", "missing-param-type-annotation" | ||
| ) | ||
| def visit_asyncfunctiondef(self, node: nodes.AsyncFunctionDef) -> None: |
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.
| def visit_asyncfunctiondef(self, node: nodes.AsyncFunctionDef) -> None: | |
| visit_asyncfunctiondef = visit_functiondef |
Sorry I can't select the whole function on mobile (or don't know how to)
| ): | ||
| return | ||
|
|
||
| # Emit the message |
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.
| # Emit the message |
| Args: | ||
| node: The function definition node to check | ||
| """ | ||
| # Skip abstract methods |
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.
| # Skip abstract methods |
Let's remove the other llm 'what should the llm write next' style comments too.
|
|
||
| @pytest.fixture | ||
| def msgid(): | ||
| def msgid() -> str: |
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 wonder why mypy didn't warn for those.
| end_col_offset=7, | ||
| ) | ||
| ): | ||
| self.checker.visit_functiondef(node) |
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.
Great tests (probably). Functional tests are a lot easier to review and write (checking the line number in particular which I didn't do here). Those are faster to run though so let's not rewrite everything unless another maintainer thinks we should.
Type of Changes
Description
This PR implements a new type annotation checker for Pylint that helps enforce the presence of type annotations in Python code. As discussed in #3853, type annotations improve code readability and enable better static analysis.
What's New
Two New Convention-Level Checkers
C2901:
missing-return-type-annotationDetects functions and methods without return type annotations.
C2902:
missing-param-type-annotationDetects function/method parameters without type annotations.
Key Features
*args,**kwargs)selfandclsparameters (automatically skipped)__init__methods (return type check skipped)@abstractmethod,@propertydecorators@typing.overloadstub definitionsFuture Enhancements
Following Issue discussion and the Google Python Style Guide model, which requires annotations only for public APIs, it could be added different checks for private/public methods:
Other possible future enhancements can be Variable Annotations, and configurable options in .pylintrc
Closes #3853