-
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
15 changed files
with
253 additions
and
0 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,21 @@ | ||
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 | ||
|
||
# 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,21 @@ | ||
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 | ||
|
||
# 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,21 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
use once_cell::sync::Lazy; | ||
use regex::Regex; | ||
use rustpython_parser::ast::{Expr, ExprKind}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
static CAMEL_CASE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^_?[a-z]").unwrap()); | ||
|
||
// PYI043: Error for alias names in "T" | ||
// (plus possibly a single digit afterwards), but only if: | ||
// | ||
// - The name starts with "_" | ||
// - The penultimate character in the name is an ASCII-lowercase letter | ||
static T_SUFFIXED_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^_.*[a-z]T\d?$").unwrap()); | ||
|
||
#[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)") | ||
} | ||
} | ||
|
||
pub fn snake_case_type_alias(checker: &mut Checker, target: &Expr) { | ||
if let ExprKind::Name { id, .. } = target.node() { | ||
if !CAMEL_CASE_REGEX.is_match(id) { | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
SnakeCaseTypeAlias { | ||
name: id.to_string(), | ||
}, | ||
target.range(), | ||
)); | ||
} | ||
} | ||
|
||
pub fn t_suffixed_type_alias(checker: &mut Checker, target: &Expr) { | ||
if let ExprKind::Name { id, .. } = target.node() { | ||
if !T_SUFFIXED_REGEX.is_match(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 = Literal[True] | Literal['idk'] # PYI042, since not camel case | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI042 | ||
13 | PublicAliasT: TypeAlias = str | int | ||
14 | PublicAliasT2: TypeAlias = Union[str, bytes] | ||
| | ||
|
||
PYI042.pyi:17:1: PYI042 Type alias `snake_case_alias1` should be CamelCase | ||
| | ||
17 | _PrivateAliasS2: TypeAlias = Annotated[str, "also okay"] | ||
18 | | ||
19 | snake_case_alias1: TypeAlias = str | int # PYI042, since not camel case | ||
| ^^^^^^^^^^^^^^^^^ PYI042 | ||
20 | _snake_case_alias2: TypeAlias = Literal["whatever"] # PYI042, since not camel case | ||
| | ||
|
||
PYI042.pyi:18:1: PYI042 Type alias `_snake_case_alias2` should be CamelCase | ||
| | ||
18 | snake_case_alias1: TypeAlias = str | int # PYI042, since not camel case | ||
19 | _snake_case_alias2: TypeAlias = Literal["whatever"] # PYI042, since not camel case | ||
| ^^^^^^^^^^^^^^^^^^ PYI042 | ||
20 | | ||
21 | # check that this edge case doesn't crash | ||
| | ||
|
||
|
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["not", "a", "chance"] # PYI043, since this ends in a T | ||
| | ||
|
||
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["not", "a", "chance"] # PYI043, since this ends in a T | ||
14 | just_literals_pipe_union: TypeAlias = Literal[True] | Literal['idk'] | ||
| | ||
|
||
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["not", "a", "chance"] # PYI043, since this ends in a T | ||
| ^^^^^^^^^^^^^^^ PYI043 | ||
15 | just_literals_pipe_union: TypeAlias = Literal[True] | Literal['idk'] | ||
16 | PublicAliasT: TypeAlias = str | int | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.