-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-annotations
] Fix return type annotations to handle shadowed builtin symbols (ANN201
, ANN202
, ANN204
, ANN205
, ANN206
)
#20612
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
crates/ruff_linter/resources/test/fixtures/flake8_annotations/shadowed_builtins.py
This file contains hidden or 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,21 @@ | ||
from collections import UserString as str | ||
from typing import override | ||
|
||
def foo(): | ||
return "!" | ||
|
||
def _foo(): | ||
return "!" | ||
|
||
class C: | ||
@override | ||
def __str__(self): | ||
return "!" | ||
|
||
@staticmethod | ||
def foo(): | ||
return "!" | ||
|
||
@classmethod | ||
def bar(cls): | ||
return "!" |
This file contains hidden or 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 hidden or 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 hidden or 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
124 changes: 124 additions & 0 deletions
124
...notations/snapshots/ruff_linter__rules__flake8_annotations__tests__shadowed_builtins.snap
This file contains hidden or 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,124 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_annotations/mod.rs | ||
--- | ||
ANN201 [*] Missing return type annotation for public function `foo` | ||
--> shadowed_builtins.py:4:5 | ||
| | ||
2 | from typing import override | ||
3 | | ||
4 | def foo(): | ||
| ^^^ | ||
5 | return "!" | ||
| | ||
help: Add return type annotation: `builtins.str` | ||
1 | from collections import UserString as str | ||
2 | from typing import override | ||
3 + import builtins | ||
4 | | ||
- def foo(): | ||
5 + def foo() -> builtins.str: | ||
6 | return "!" | ||
7 | | ||
8 | def _foo(): | ||
note: This is an unsafe fix and may change runtime behavior | ||
|
||
ANN202 [*] Missing return type annotation for private function `_foo` | ||
--> shadowed_builtins.py:7:5 | ||
| | ||
5 | return "!" | ||
6 | | ||
7 | def _foo(): | ||
| ^^^^ | ||
8 | return "!" | ||
| | ||
help: Add return type annotation: `builtins.str` | ||
1 | from collections import UserString as str | ||
2 | from typing import override | ||
3 + import builtins | ||
4 | | ||
5 | def foo(): | ||
6 | return "!" | ||
7 | | ||
- def _foo(): | ||
8 + def _foo() -> builtins.str: | ||
9 | return "!" | ||
10 | | ||
11 | class C: | ||
note: This is an unsafe fix and may change runtime behavior | ||
|
||
ANN204 [*] Missing return type annotation for special method `__str__` | ||
--> shadowed_builtins.py:12:9 | ||
| | ||
10 | class C: | ||
11 | @override | ||
12 | def __str__(self): | ||
| ^^^^^^^ | ||
13 | return "!" | ||
| | ||
help: Add return type annotation: `str` | ||
1 | from collections import UserString as str | ||
2 | from typing import override | ||
3 + import builtins | ||
4 | | ||
5 | def foo(): | ||
6 | return "!" | ||
-------------------------------------------------------------------------------- | ||
10 | | ||
11 | class C: | ||
12 | @override | ||
- def __str__(self): | ||
13 + def __str__(self) -> builtins.str: | ||
14 | return "!" | ||
15 | | ||
16 | @staticmethod | ||
note: This is an unsafe fix and may change runtime behavior | ||
|
||
ANN205 [*] Missing return type annotation for staticmethod `foo` | ||
--> shadowed_builtins.py:16:9 | ||
| | ||
15 | @staticmethod | ||
16 | def foo(): | ||
| ^^^ | ||
17 | return "!" | ||
| | ||
help: Add return type annotation: `builtins.str` | ||
1 | from collections import UserString as str | ||
2 | from typing import override | ||
3 + import builtins | ||
4 | | ||
5 | def foo(): | ||
6 | return "!" | ||
-------------------------------------------------------------------------------- | ||
14 | return "!" | ||
15 | | ||
16 | @staticmethod | ||
- def foo(): | ||
17 + def foo() -> builtins.str: | ||
18 | return "!" | ||
19 | | ||
20 | @classmethod | ||
note: This is an unsafe fix and may change runtime behavior | ||
|
||
ANN206 [*] Missing return type annotation for classmethod `bar` | ||
--> shadowed_builtins.py:20:9 | ||
| | ||
19 | @classmethod | ||
20 | def bar(cls): | ||
| ^^^ | ||
21 | return "!" | ||
| | ||
help: Add return type annotation: `builtins.str` | ||
1 | from collections import UserString as str | ||
2 | from typing import override | ||
3 + import builtins | ||
4 | | ||
5 | def foo(): | ||
6 | return "!" | ||
-------------------------------------------------------------------------------- | ||
18 | return "!" | ||
19 | | ||
20 | @classmethod | ||
- def bar(cls): | ||
21 + def bar(cls) -> builtins.str: | ||
22 | return "!" | ||
note: This is an unsafe fix and may change runtime behavior |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a bit awkward because we just did basically the same
match
inside ofsimple_magic_return_type
. I tried movingsimple_magic_return_type
into this file and making it return aPythonType
directly, though, and then we don't have a nice way to get back to the string representation. So I think this is fine as-is.