diff --git a/crates/ruff/resources/test/fixtures/pep8_naming/N806.py b/crates/ruff/resources/test/fixtures/pep8_naming/N806.py index faa68b7240..25fdd914c7 100644 --- a/crates/ruff/resources/test/fixtures/pep8_naming/N806.py +++ b/crates/ruff/resources/test/fixtures/pep8_naming/N806.py @@ -2,6 +2,7 @@ import collections from collections import namedtuple from typing import TypeVar from typing import NewType +from typing import NamedTuple, TypedDict GLOBAL: str = "foo" @@ -20,6 +21,10 @@ def assign(): T = TypeVar("T") UserId = NewType("UserId", int) + Employee = NamedTuple('Employee', [('name', str), ('id', int)]) + + Point2D = TypedDict('Point2D', {'in': int, 'x-y': int}) + def aug_assign(rank, world_size): global CURRENT_PORT diff --git a/crates/ruff/resources/test/fixtures/pep8_naming/N815.py b/crates/ruff/resources/test/fixtures/pep8_naming/N815.py index fc2bf1993c..db767423d2 100644 --- a/crates/ruff/resources/test/fixtures/pep8_naming/N815.py +++ b/crates/ruff/resources/test/fixtures/pep8_naming/N815.py @@ -1,5 +1,6 @@ import collections from collections import namedtuple +from typing import NamedTuple, TypedDict class C: @@ -10,3 +11,5 @@ class C: mixed_Case = 0 myObj1 = collections.namedtuple("MyObj1", ["a", "b"]) myObj2 = namedtuple("MyObj2", ["a", "b"]) + Employee = NamedTuple('Employee', [('name', str), ('id', int)]) + Point2D = TypedDict('Point2D', {'in': int, 'x-y': int}) diff --git a/crates/ruff/resources/test/fixtures/pep8_naming/N816.py b/crates/ruff/resources/test/fixtures/pep8_naming/N816.py index b814f5eecf..9c1fd8aabe 100644 --- a/crates/ruff/resources/test/fixtures/pep8_naming/N816.py +++ b/crates/ruff/resources/test/fixtures/pep8_naming/N816.py @@ -1,5 +1,6 @@ import collections from collections import namedtuple +from typing import NamedTuple, TypedDict lower = 0 CONSTANT = 0 @@ -8,3 +9,5 @@ _mixedCase = 0 mixed_Case = 0 myObj1 = collections.namedtuple("MyObj1", ["a", "b"]) myObj2 = namedtuple("MyObj2", ["a", "b"]) +Employee = NamedTuple('Employee', [('name', str), ('id', int)]) +Point2D = TypedDict('Point2D', {'in': int, 'x-y': int}) diff --git a/crates/ruff/src/rules/pep8_naming/helpers.rs b/crates/ruff/src/rules/pep8_naming/helpers.rs index 9def0d0575..5a598711b8 100644 --- a/crates/ruff/src/rules/pep8_naming/helpers.rs +++ b/crates/ruff/src/rules/pep8_naming/helpers.rs @@ -28,6 +28,16 @@ pub fn is_namedtuple_assignment(checker: &Checker, stmt: &Stmt) -> bool { }; checker.resolve_call_path(value).map_or(false, |call_path| { call_path.as_slice() == ["collections", "namedtuple"] + || call_path.as_slice() == ["typing", "NamedTuple"] + }) +} + +pub fn is_typeddict_assignment(checker: &Checker, stmt: &Stmt) -> bool { + let StmtKind::Assign { value, .. } = &stmt.node else { + return false; + }; + checker.resolve_call_path(value).map_or(false, |call_path| { + call_path.as_slice() == ["typing", "TypedDict"] }) } diff --git a/crates/ruff/src/rules/pep8_naming/rules.rs b/crates/ruff/src/rules/pep8_naming/rules.rs index da6f5a720f..23d501c59f 100644 --- a/crates/ruff/src/rules/pep8_naming/rules.rs +++ b/crates/ruff/src/rules/pep8_naming/rules.rs @@ -327,6 +327,7 @@ pub fn non_lowercase_variable_in_function( ) { if name.to_lowercase() != name && !helpers::is_namedtuple_assignment(checker, stmt) + && !helpers::is_typeddict_assignment(checker, stmt) && !helpers::is_type_var_assignment(checker, stmt) { checker.diagnostics.push(Diagnostic::new( diff --git a/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap b/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap index 646481cfdb..c10a336a41 100644 --- a/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap +++ b/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap @@ -1,15 +1,15 @@ --- -source: src/rules/pep8_naming/mod.rs +source: crates/ruff/src/rules/pep8_naming/mod.rs expression: diagnostics --- - kind: NonLowercaseVariableInFunction: name: Camel location: - row: 13 + row: 14 column: 4 end_location: - row: 13 + row: 14 column: 9 fix: ~ parent: ~ @@ -17,10 +17,10 @@ expression: diagnostics NonLowercaseVariableInFunction: name: CONSTANT location: - row: 14 + row: 15 column: 4 end_location: - row: 14 + row: 15 column: 12 fix: ~ parent: ~ diff --git a/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N815_N815.py.snap b/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N815_N815.py.snap index 2267e8df98..5c4b74e2d0 100644 --- a/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N815_N815.py.snap +++ b/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N815_N815.py.snap @@ -1,15 +1,15 @@ --- -source: src/rules/pep8_naming/mod.rs +source: crates/ruff/src/rules/pep8_naming/mod.rs expression: diagnostics --- - kind: MixedCaseVariableInClassScope: name: mixedCase location: - row: 8 + row: 9 column: 4 end_location: - row: 8 + row: 9 column: 13 fix: ~ parent: ~ @@ -17,10 +17,10 @@ expression: diagnostics MixedCaseVariableInClassScope: name: _mixedCase location: - row: 9 + row: 10 column: 4 end_location: - row: 9 + row: 10 column: 14 fix: ~ parent: ~ @@ -28,10 +28,10 @@ expression: diagnostics MixedCaseVariableInClassScope: name: mixed_Case location: - row: 10 + row: 11 column: 4 end_location: - row: 10 + row: 11 column: 14 fix: ~ parent: ~ diff --git a/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N816_N816.py.snap b/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N816_N816.py.snap index 9a8a3c08e3..956b26e032 100644 --- a/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N816_N816.py.snap +++ b/crates/ruff/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N816_N816.py.snap @@ -1,15 +1,15 @@ --- -source: src/rules/pep8_naming/mod.rs +source: crates/ruff/src/rules/pep8_naming/mod.rs expression: diagnostics --- - kind: MixedCaseVariableInGlobalScope: name: mixedCase location: - row: 6 + row: 7 column: 0 end_location: - row: 6 + row: 7 column: 9 fix: ~ parent: ~ @@ -17,10 +17,10 @@ expression: diagnostics MixedCaseVariableInGlobalScope: name: _mixedCase location: - row: 7 + row: 8 column: 0 end_location: - row: 7 + row: 8 column: 10 fix: ~ parent: ~ @@ -28,10 +28,10 @@ expression: diagnostics MixedCaseVariableInGlobalScope: name: mixed_Case location: - row: 8 + row: 9 column: 0 end_location: - row: 8 + row: 9 column: 10 fix: ~ parent: ~