fix(pep8-naming): typing.NamedTuple and typing.TypedDict treatment (#2611)

This commit is contained in:
Ville Skyttä 2023-02-07 00:11:37 +02:00 committed by GitHub
parent 2dd04dd6a3
commit 60ee1d2c17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 19 deletions

View file

@ -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

View file

@ -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})

View file

@ -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})

View file

@ -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"]
})
}

View file

@ -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(

View file

@ -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: ~

View file

@ -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: ~

View file

@ -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: ~