-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
283 additions
and
2 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,24 @@ | ||
import typing | ||
from collections.abc import Mapping | ||
from typing import ( | ||
Annotated, | ||
TypeAlias, | ||
Union, | ||
Literal, | ||
) | ||
|
||
just_literals_pipe_union: TypeAlias = ( | ||
Literal[True] | Literal["idk"] | ||
) # not PYI042 (not a stubfile) | ||
PublicAliasT: TypeAlias = str | int | ||
PublicAliasT2: TypeAlias = Union[str, bytes] | ||
_ABCDEFGHIJKLMNOPQRST: TypeAlias = typing.Any | ||
_PrivateAliasS: TypeAlias = Literal["I", "guess", "this", "is", "okay"] | ||
_PrivateAliasS2: TypeAlias = Annotated[str, "also okay"] | ||
|
||
snake_case_alias1: TypeAlias = str | int # not PYI042 (not a stubfile) | ||
_snake_case_alias2: TypeAlias = Literal["whatever"] # not PYI042 (not a stubfile) | ||
Snake_case_alias: TypeAlias = int | float # not PYI042 (not a stubfile) | ||
|
||
# check that this edge case doesn't crash | ||
_: TypeAlias = str | int |
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,24 @@ | ||
import typing | ||
from collections.abc import Mapping | ||
from typing import ( | ||
Annotated, | ||
TypeAlias, | ||
Union, | ||
Literal, | ||
) | ||
|
||
just_literals_pipe_union: TypeAlias = ( | ||
Literal[True] | Literal["idk"] | ||
) # PYI042, since not camel case | ||
PublicAliasT: TypeAlias = str | int | ||
PublicAliasT2: TypeAlias = Union[str, bytes] | ||
_ABCDEFGHIJKLMNOPQRST: TypeAlias = typing.Any | ||
_PrivateAliasS: TypeAlias = Literal["I", "guess", "this", "is", "okay"] | ||
_PrivateAliasS2: TypeAlias = Annotated[str, "also okay"] | ||
|
||
snake_case_alias1: TypeAlias = str | int # PYI042, since not camel case | ||
_snake_case_alias2: TypeAlias = Literal["whatever"] # PYI042, since not camel case | ||
Snake_case_alias: TypeAlias = int | float # PYI042, since not camel case | ||
|
||
# check that this edge case doesn't crash | ||
_: TypeAlias = str | int |
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,23 @@ | ||
import typing | ||
from collections.abc import Mapping | ||
from typing import ( | ||
Annotated, | ||
TypeAlias, | ||
Union, | ||
Literal, | ||
) | ||
|
||
_PrivateAliasT: TypeAlias = str | int # not PYI043 (not a stubfile) | ||
_PrivateAliasT2: TypeAlias = typing.Any # not PYI043 (not a stubfile) | ||
_PrivateAliasT3: TypeAlias = Literal[ | ||
"not", "a", "chance" | ||
] # not PYI043 (not a stubfile) | ||
just_literals_pipe_union: TypeAlias = Literal[True] | Literal["idk"] | ||
PublicAliasT: TypeAlias = str | int | ||
PublicAliasT2: TypeAlias = Union[str, bytes] | ||
_ABCDEFGHIJKLMNOPQRST: TypeAlias = typing.Any | ||
_PrivateAliasS: TypeAlias = Literal["I", "guess", "this", "is", "okay"] | ||
_PrivateAliasS2: TypeAlias = Annotated[str, "also okay"] | ||
|
||
# check that this edge case doesn't crash | ||
_: TypeAlias = str | int |
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,23 @@ | ||
import typing | ||
from collections.abc import Mapping | ||
from typing import ( | ||
Annotated, | ||
TypeAlias, | ||
Union, | ||
Literal, | ||
) | ||
|
||
_PrivateAliasT: TypeAlias = str | int # PYI043, since this ends in a T | ||
_PrivateAliasT2: TypeAlias = typing.Any # PYI043, since this ends in a T | ||
_PrivateAliasT3: TypeAlias = Literal[ | ||
"not", "a", "chance" | ||
] # PYI043, since this ends in a T | ||
just_literals_pipe_union: TypeAlias = Literal[True] | Literal["idk"] | ||
PublicAliasT: TypeAlias = str | int | ||
PublicAliasT2: TypeAlias = Union[str, bytes] | ||
_ABCDEFGHIJKLMNOPQRST: TypeAlias = typing.Any | ||
_PrivateAliasS: TypeAlias = Literal["I", "guess", "this", "is", "okay"] | ||
_PrivateAliasS2: TypeAlias = Annotated[str, "also okay"] | ||
|
||
# check that this edge case doesn't crash | ||
_: TypeAlias = str | int |
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
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
91 changes: 91 additions & 0 deletions
91
crates/ruff/src/rules/flake8_pyi/rules/type_alias_naming.rs
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,91 @@ | ||
use rustpython_parser::ast::{Expr, ExprKind}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[violation] | ||
pub struct SnakeCaseTypeAlias { | ||
pub name: String, | ||
} | ||
|
||
impl Violation for SnakeCaseTypeAlias { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { name } = self; | ||
format!("Type alias `{name}` should be CamelCase") | ||
} | ||
} | ||
|
||
#[violation] | ||
pub struct TSuffixedTypeAlias { | ||
pub name: String, | ||
} | ||
|
||
impl Violation for TSuffixedTypeAlias { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { name } = self; | ||
format!("Private type alias `{name}` should not be suffixed with `T` (the `T` suffix implies that an object is a `TypeVar`)") | ||
} | ||
} | ||
|
||
/// Return `true` if the given name is a `snake_case` type alias. In this context, we match against | ||
/// any name that begins with an optional underscore, followed by at least one lowercase letter. | ||
fn is_snake_case_type_alias(name: &str) -> bool { | ||
let mut chars = name.chars(); | ||
matches!( | ||
(chars.next(), chars.next()), | ||
(Some('_'), Some('0'..='9' | 'a'..='z')) | (Some('0'..='9' | 'a'..='z'), ..) | ||
) | ||
} | ||
|
||
/// Return `true` if the given name is a T-suffixed type alias. In this context, we match against | ||
/// any name that begins with an underscore, and ends in a lowercase letter, followed by `T`, | ||
/// followed by an optional digit. | ||
fn is_t_suffixed_type_alias(name: &str) -> bool { | ||
// A T-suffixed, private type alias must begin with an underscore. | ||
if !name.starts_with('_') { | ||
return false; | ||
} | ||
|
||
// It must end in a lowercase letter, followed by `T`, and (optionally) a digit. | ||
let mut chars = name.chars().rev(); | ||
matches!( | ||
(chars.next(), chars.next(), chars.next()), | ||
(Some('0'..='9'), Some('T'), Some('a'..='z')) | (Some('T'), Some('a'..='z'), _) | ||
) | ||
} | ||
|
||
/// PYI042 | ||
pub fn snake_case_type_alias(checker: &mut Checker, target: &Expr) { | ||
if let ExprKind::Name { id, .. } = target.node() { | ||
if !is_snake_case_type_alias(id) { | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
SnakeCaseTypeAlias { | ||
name: id.to_string(), | ||
}, | ||
target.range(), | ||
)); | ||
} | ||
} | ||
|
||
/// PYI043 | ||
pub fn t_suffixed_type_alias(checker: &mut Checker, target: &Expr) { | ||
if let ExprKind::Name { id, .. } = target.node() { | ||
if !is_t_suffixed_type_alias(id) { | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
TSuffixedTypeAlias { | ||
name: id.to_string(), | ||
}, | ||
target.range(), | ||
)); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI042_PYI042.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 | ||
--- | ||
|
32 changes: 32 additions & 0 deletions
32
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI042_PYI042.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,32 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI042.pyi:10:1: PYI042 Type alias `just_literals_pipe_union` should be CamelCase | ||
| | ||
10 | ) | ||
11 | | ||
12 | just_literals_pipe_union: TypeAlias = ( | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI042 | ||
13 | Literal[True] | Literal["idk"] | ||
14 | ) # PYI042, since not camel case | ||
| | ||
|
||
PYI042.pyi:19:1: PYI042 Type alias `snake_case_alias1` should be CamelCase | ||
| | ||
19 | _PrivateAliasS2: TypeAlias = Annotated[str, "also okay"] | ||
20 | | ||
21 | snake_case_alias1: TypeAlias = str | int # PYI042, since not camel case | ||
| ^^^^^^^^^^^^^^^^^ PYI042 | ||
22 | _snake_case_alias2: TypeAlias = Literal["whatever"] # PYI042, since not camel case | ||
23 | Snake_case_alias: TypeAlias = int | float # PYI042, since not camel case | ||
| | ||
|
||
PYI042.pyi:20:1: PYI042 Type alias `_snake_case_alias2` should be CamelCase | ||
| | ||
20 | snake_case_alias1: TypeAlias = str | int # PYI042, since not camel case | ||
21 | _snake_case_alias2: TypeAlias = Literal["whatever"] # PYI042, since not camel case | ||
| ^^^^^^^^^^^^^^^^^^ PYI042 | ||
22 | Snake_case_alias: TypeAlias = int | float # PYI042, since not camel case | ||
| | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI043_PYI043.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 | ||
--- | ||
|
33 changes: 33 additions & 0 deletions
33
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI043_PYI043.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,33 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI043.pyi:10:1: PYI043 Private type alias `_PrivateAliasT` should not be suffixed with `T` (the `T` suffix implies that an object is a `TypeVar`) | ||
| | ||
10 | ) | ||
11 | | ||
12 | _PrivateAliasT: TypeAlias = str | int # PYI043, since this ends in a T | ||
| ^^^^^^^^^^^^^^ PYI043 | ||
13 | _PrivateAliasT2: TypeAlias = typing.Any # PYI043, since this ends in a T | ||
14 | _PrivateAliasT3: TypeAlias = Literal[ | ||
| | ||
|
||
PYI043.pyi:11:1: PYI043 Private type alias `_PrivateAliasT2` should not be suffixed with `T` (the `T` suffix implies that an object is a `TypeVar`) | ||
| | ||
11 | _PrivateAliasT: TypeAlias = str | int # PYI043, since this ends in a T | ||
12 | _PrivateAliasT2: TypeAlias = typing.Any # PYI043, since this ends in a T | ||
| ^^^^^^^^^^^^^^^ PYI043 | ||
13 | _PrivateAliasT3: TypeAlias = Literal[ | ||
14 | "not", "a", "chance" | ||
| | ||
|
||
PYI043.pyi:12:1: PYI043 Private type alias `_PrivateAliasT3` should not be suffixed with `T` (the `T` suffix implies that an object is a `TypeVar`) | ||
| | ||
12 | _PrivateAliasT: TypeAlias = str | int # PYI043, since this ends in a T | ||
13 | _PrivateAliasT2: TypeAlias = typing.Any # PYI043, since this ends in a T | ||
14 | _PrivateAliasT3: TypeAlias = Literal[ | ||
| ^^^^^^^^^^^^^^^ PYI043 | ||
15 | "not", "a", "chance" | ||
16 | ] # PYI043, since this ends in a T | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.