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

Avoid triggering N806 on TypeAlias assignments #7119

Merged
merged 1 commit into from
Sep 4, 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
10 changes: 5 additions & 5 deletions crates/ruff/resources/test/fixtures/pep8_naming/N806.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import collections
from collections import namedtuple
from typing import TypeVar
from typing import NewType
from typing import NamedTuple, TypedDict
from typing import TypeAlias, TypeVar, NewType, NamedTuple, TypedDict

GLOBAL: str = "foo"

Expand All @@ -21,9 +19,11 @@ def assign():
T = TypeVar("T")
UserId = NewType("UserId", int)

Employee = NamedTuple('Employee', [('name', str), ('id', int)])
Employee = NamedTuple("Employee", [("name", str), ("id", int)])

Point2D = TypedDict('Point2D', {'in': int, 'x-y': int})
Point2D = TypedDict("Point2D", {"in": int, "x-y": int})

IntOrStr: TypeAlias = int | str


def aug_assign(rank, world_size):
Expand Down
28 changes: 18 additions & 10 deletions crates/ruff/src/rules/pep8_naming/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(super) fn is_acronym(name: &str, asname: &str) -> bool {
name.chars().filter(|c| c.is_uppercase()).join("") == asname
}

/// Returns `true` if the statement is an assignment to a named tuple.
pub(super) fn is_named_tuple_assignment(stmt: &Stmt, semantic: &SemanticModel) -> bool {
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
return false;
Expand All @@ -30,35 +31,42 @@ pub(super) fn is_named_tuple_assignment(stmt: &Stmt, semantic: &SemanticModel) -
return false;
};
semantic.resolve_call_path(func).is_some_and(|call_path| {
matches!(
call_path.as_slice(),
["collections", "namedtuple"] | ["typing", "NamedTuple"]
)
matches!(call_path.as_slice(), ["collections", "namedtuple"])
|| semantic.match_typing_call_path(&call_path, "NamedTuple")
})
}

/// Returns `true` if the statement is an assignment to a `TypedDict`.
pub(super) fn is_typed_dict_assignment(stmt: &Stmt, semantic: &SemanticModel) -> bool {
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
return false;
};
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else {
return false;
};
semantic
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["typing", "TypedDict"]))
semantic.match_typing_expr(func, "TypedDict")
}

/// Returns `true` if the statement is an assignment to a `TypeVar` or `NewType`.
pub(super) fn is_type_var_assignment(stmt: &Stmt, semantic: &SemanticModel) -> bool {
let Stmt::Assign(ast::StmtAssign { value, .. }) = stmt else {
return false;
};
let Expr::Call(ast::ExprCall { func, .. }) = value.as_ref() else {
return false;
};
semantic
.resolve_call_path(func)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["typing", "TypeVar" | "NewType"]))
semantic.resolve_call_path(func).is_some_and(|call_path| {
semantic.match_typing_call_path(&call_path, "TypeVar")
|| semantic.match_typing_call_path(&call_path, "NewType")
})
}

/// Returns `true` if the statement is an assignment to a `TypeAlias`.
pub(super) fn is_type_alias_assignment(stmt: &Stmt, semantic: &SemanticModel) -> bool {
let Stmt::AnnAssign(ast::StmtAnnAssign { annotation, .. }) = stmt else {
return false;
};
semantic.match_typing_expr(annotation, "TypeAlias")
}

pub(super) fn is_typed_dict_class(arguments: Option<&Arguments>, semantic: &SemanticModel) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub(crate) fn non_lowercase_variable_in_function(checker: &mut Checker, expr: &E
if helpers::is_named_tuple_assignment(parent, checker.semantic())
|| helpers::is_typed_dict_assignment(parent, checker.semantic())
|| helpers::is_type_var_assignment(parent, checker.semantic())
|| helpers::is_type_alias_assignment(parent, checker.semantic())
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
source: crates/ruff/src/rules/pep8_naming/mod.rs
---
N806.py:14:5: N806 Variable `Camel` in function should be lowercase
N806.py:12:5: N806 Variable `Camel` in function should be lowercase
|
12 | GLOBAL = "bar"
13 | lower = 0
14 | Camel = 0
10 | GLOBAL = "bar"
11 | lower = 0
12 | Camel = 0
| ^^^^^ N806
15 | CONSTANT = 0
16 | _ = 0
13 | CONSTANT = 0
14 | _ = 0
|

N806.py:15:5: N806 Variable `CONSTANT` in function should be lowercase
N806.py:13:5: N806 Variable `CONSTANT` in function should be lowercase
|
13 | lower = 0
14 | Camel = 0
15 | CONSTANT = 0
11 | lower = 0
12 | Camel = 0
13 | CONSTANT = 0
| ^^^^^^^^ N806
16 | _ = 0
14 | _ = 0
|