Skip to content

Commit

Permalink
[flake8-pyi] Check PEP 695 type aliases for snake-case-type-alias
Browse files Browse the repository at this point in the history
… and `t-suffixed-type-alias` (#8966)

## Summary

Check PEP 695 type alias definitions for `snake-case-type-alias`
(`PYI042`) and `t-suffixed-type-alias` (`PYI043`)

Related to #8771.

## Test Plan

`cargo test`
  • Loading branch information
tjkuson authored Dec 2, 2023
1 parent 20ab14e commit 3fbabfe
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 0 deletions.
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
|


0 comments on commit 3fbabfe

Please sign in to comment.