-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-pyi
] Implement PYI026 (#5844)
## Summary Checks for `typehint.TypeAlias` annotation in type aliases. See [original source](https://github.com/PyCQA/flake8-pyi/blob/main/pyi.py#L1085). ``` $ flake8 --select Y026 crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:4:1: Y026 Use typing_extensions.TypeAlias for type aliases, e.g. "NewAny: TypeAlias = Any" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:5:1: Y026 Use typing_extensions.TypeAlias for type aliases, e.g. "OptinalStr: TypeAlias = typing.Optional[str]" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:6:1: Y026 Use typing_extensions.TypeAlias for type aliases, e.g. "Foo: TypeAlias = Literal['foo']" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:7:1: Y026 Use typing_extensions.TypeAlias for type aliases, e.g. "IntOrStr: TypeAlias = int | str" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:8:1: Y026 Use typing_extensions.TypeAlias for type aliases, e.g. "AliasNone: TypeAlias = None" ``` ``` $ ./target/debug/ruff --select PYI026 crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi --no-cache crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:4:1: PYI026 Use `typing.TypeAlias` for type aliases in `NewAny`, e.g. "NewAny: typing.TypeAlias = Any" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:5:1: PYI026 Use `typing.TypeAlias` for type aliases in `OptinalStr`, e.g. "OptinalStr: typing.TypeAlias = typing.Optional[str]" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:6:1: PYI026 Use `typing.TypeAlias` for type aliases in `Foo`, e.g. "Foo: typing.TypeAlias = Literal["foo"]" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:7:1: PYI026 Use `typing.TypeAlias` for type aliases in `IntOrStr`, e.g. "IntOrStr: typing.TypeAlias = int | str" crates/ruff/resources/test/fixtures/flake8_pyi/PYI026.pyi:8:1: PYI026 Use `typing.TypeAlias` for type aliases in `AliasNone`, e.g. "AliasNone: typing.TypeAlias = None" Found 5 errors. ``` ref: #848 ## Test Plan Snapshots, manual runs of flake8.
- Loading branch information
1 parent
963f240
commit a37d915
Showing
9 changed files
with
296 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import typing | ||
from typing import TypeAlias, Literal, Any | ||
|
||
NewAny = Any | ||
OptionalStr = typing.Optional[str] | ||
Foo = Literal["foo"] | ||
IntOrStr = int | str | ||
AliasNone = None | ||
|
||
NewAny: typing.TypeAlias = Any | ||
OptionalStr: TypeAlias = typing.Optional[str] | ||
Foo: typing.TypeAlias = Literal["foo"] | ||
IntOrStr: TypeAlias = int | str | ||
IntOrFloat: Foo = int | float | ||
AliasNone: typing.TypeAlias = None | ||
|
||
# these are ok | ||
VarAlias = str | ||
AliasFoo = Foo |
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,18 @@ | ||
from typing import Literal, Any | ||
|
||
NewAny = Any | ||
OptionalStr = typing.Optional[str] | ||
Foo = Literal["foo"] | ||
IntOrStr = int | str | ||
AliasNone = None | ||
|
||
NewAny: typing.TypeAlias = Any | ||
OptionalStr: TypeAlias = typing.Optional[str] | ||
Foo: typing.TypeAlias = Literal["foo"] | ||
IntOrStr: TypeAlias = int | str | ||
IntOrFloat: Foo = int | float | ||
AliasNone: typing.TypeAlias = None | ||
|
||
# these are ok | ||
VarAlias = str | ||
AliasFoo = Foo |
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
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
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI026_PYI026.py.snap
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,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
|
117 changes: 117 additions & 0 deletions
117
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI026_PYI026.pyi.snap
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,117 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI026.pyi:3:1: PYI026 [*] Use `typing.TypeAlias` for type alias, e.g., `NewAny: typing.TypeAlias = Any` | ||
| | ||
1 | from typing import Literal, Any | ||
2 | | ||
3 | NewAny = Any | ||
| ^^^^^^ PYI026 | ||
4 | OptionalStr = typing.Optional[str] | ||
5 | Foo = Literal["foo"] | ||
| | ||
= help: Add `typing.TypeAlias` annotation | ||
|
||
ℹ Suggested fix | ||
1 |-from typing import Literal, Any | ||
1 |+from typing import Literal, Any, TypeAlias | ||
2 2 | | ||
3 |-NewAny = Any | ||
3 |+NewAny: TypeAlias = Any | ||
4 4 | OptionalStr = typing.Optional[str] | ||
5 5 | Foo = Literal["foo"] | ||
6 6 | IntOrStr = int | str | ||
|
||
PYI026.pyi:4:1: PYI026 [*] Use `typing.TypeAlias` for type alias, e.g., `OptionalStr: typing.TypeAlias = typing.Optional[str]` | ||
| | ||
3 | NewAny = Any | ||
4 | OptionalStr = typing.Optional[str] | ||
| ^^^^^^^^^^^ PYI026 | ||
5 | Foo = Literal["foo"] | ||
6 | IntOrStr = int | str | ||
| | ||
= help: Add `typing.TypeAlias` annotation | ||
|
||
ℹ Suggested fix | ||
1 |-from typing import Literal, Any | ||
1 |+from typing import Literal, Any, TypeAlias | ||
2 2 | | ||
3 3 | NewAny = Any | ||
4 |-OptionalStr = typing.Optional[str] | ||
4 |+OptionalStr: TypeAlias = typing.Optional[str] | ||
5 5 | Foo = Literal["foo"] | ||
6 6 | IntOrStr = int | str | ||
7 7 | AliasNone = None | ||
|
||
PYI026.pyi:5:1: PYI026 [*] Use `typing.TypeAlias` for type alias, e.g., `Foo: typing.TypeAlias = Literal["foo"]` | ||
| | ||
3 | NewAny = Any | ||
4 | OptionalStr = typing.Optional[str] | ||
5 | Foo = Literal["foo"] | ||
| ^^^ PYI026 | ||
6 | IntOrStr = int | str | ||
7 | AliasNone = None | ||
| | ||
= help: Add `typing.TypeAlias` annotation | ||
|
||
ℹ Suggested fix | ||
1 |-from typing import Literal, Any | ||
1 |+from typing import Literal, Any, TypeAlias | ||
2 2 | | ||
3 3 | NewAny = Any | ||
4 4 | OptionalStr = typing.Optional[str] | ||
5 |-Foo = Literal["foo"] | ||
5 |+Foo: TypeAlias = Literal["foo"] | ||
6 6 | IntOrStr = int | str | ||
7 7 | AliasNone = None | ||
8 8 | | ||
|
||
PYI026.pyi:6:1: PYI026 [*] Use `typing.TypeAlias` for type alias, e.g., `IntOrStr: typing.TypeAlias = int | str` | ||
| | ||
4 | OptionalStr = typing.Optional[str] | ||
5 | Foo = Literal["foo"] | ||
6 | IntOrStr = int | str | ||
| ^^^^^^^^ PYI026 | ||
7 | AliasNone = None | ||
| | ||
= help: Add `typing.TypeAlias` annotation | ||
|
||
ℹ Suggested fix | ||
1 |-from typing import Literal, Any | ||
1 |+from typing import Literal, Any, TypeAlias | ||
2 2 | | ||
3 3 | NewAny = Any | ||
4 4 | OptionalStr = typing.Optional[str] | ||
5 5 | Foo = Literal["foo"] | ||
6 |-IntOrStr = int | str | ||
6 |+IntOrStr: TypeAlias = int | str | ||
7 7 | AliasNone = None | ||
8 8 | | ||
9 9 | NewAny: typing.TypeAlias = Any | ||
|
||
PYI026.pyi:7:1: PYI026 [*] Use `typing.TypeAlias` for type alias, e.g., `AliasNone: typing.TypeAlias = None` | ||
| | ||
5 | Foo = Literal["foo"] | ||
6 | IntOrStr = int | str | ||
7 | AliasNone = None | ||
| ^^^^^^^^^ PYI026 | ||
8 | | ||
9 | NewAny: typing.TypeAlias = Any | ||
| | ||
= help: Add `typing.TypeAlias` annotation | ||
|
||
ℹ Suggested fix | ||
1 |-from typing import Literal, Any | ||
1 |+from typing import Literal, Any, TypeAlias | ||
2 2 | | ||
3 3 | NewAny = Any | ||
4 4 | OptionalStr = typing.Optional[str] | ||
5 5 | Foo = Literal["foo"] | ||
6 6 | IntOrStr = int | str | ||
7 |-AliasNone = None | ||
7 |+AliasNone: TypeAlias = None | ||
8 8 | | ||
9 9 | NewAny: typing.TypeAlias = Any | ||
10 10 | OptionalStr: TypeAlias = typing.Optional[str] | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.