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

[flake8-pyi] Check PEP 695 type aliases for snake-case-type-alias and t-suffixed-type-alias #8966

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@

# check that this edge case doesn't crash
_: TypeAlias = str | int

# PEP 695
type foo_bar = int | str
type FooBar = int | str
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ Snake_case_alias: TypeAlias = int | float # PYI042, since not camel case

# check that this edge case doesn't crash
_: TypeAlias = str | int

# PEP 695
type foo_bar = int | str
type FooBar = int | str
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@

# check that this edge case doesn't crash
_: TypeAlias = str | int

# PEP 695
type _FooT = str | int
type Foo = str | int
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ _PrivateAliasS2: TypeAlias = Annotated[str, "also okay"]

# check that this edge case doesn't crash
_: TypeAlias = str | int

# PEP 695
type _FooT = str | int
type Foo = str | int
8 changes: 8 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,14 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
}
}
Stmt::TypeAlias(ast::StmtTypeAlias { name, .. }) => {
if checker.enabled(Rule::SnakeCaseTypeAlias) {
flake8_pyi::rules::snake_case_type_alias(checker, name);
}
if checker.enabled(Rule::TSuffixedTypeAlias) {
flake8_pyi::rules::t_suffixed_type_alias(checker, name);
}
}
Stmt::Delete(delete @ ast::StmtDelete { targets, range: _ }) => {
if checker.enabled(Rule::GlobalStatement) {
for target in targets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ PYI042.py:20:1: PYI042 Type alias `_snake_case_alias2` should be CamelCase
21 | Snake_case_alias: TypeAlias = int | float # PYI042, since not camel case
|

PYI042.py:27:6: PYI042 Type alias `foo_bar` should be CamelCase
|
26 | # PEP 695
27 | type foo_bar = int | str
| ^^^^^^^ PYI042
28 | type FooBar = int | str
|


Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ PYI042.pyi:20:1: PYI042 Type alias `_snake_case_alias2` should be CamelCase
21 | Snake_case_alias: TypeAlias = int | float # PYI042, since not camel case
|

PYI042.pyi:27:6: PYI042 Type alias `foo_bar` should be CamelCase
|
26 | # PEP 695
27 | type foo_bar = int | str
| ^^^^^^^ PYI042
28 | type FooBar = int | str
|


Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ PYI043.py:12:1: PYI043 Private type alias `_PrivateAliasT3` should not be suffix
14 | ] # PYI043, since this ends in a T
|

PYI043.py:26:6: PYI043 Private type alias `_FooT` should not be suffixed with `T` (the `T` suffix implies that an object is a `TypeVar`)
|
25 | # PEP 695
26 | type _FooT = str | int
| ^^^^^ PYI043
27 | type Foo = str | int
|


Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ PYI043.pyi:12:1: PYI043 Private type alias `_PrivateAliasT3` should not be suffi
14 | ] # PYI043, since this ends in a T
|

PYI043.pyi:26:6: PYI043 Private type alias `_FooT` should not be suffixed with `T` (the `T` suffix implies that an object is a `TypeVar`)
|
25 | # PEP 695
26 | type _FooT = str | int
| ^^^^^ PYI043
27 | type Foo = str | int
|


Loading