From 75e16c0ce508978ab547f0d14d70c0926065f518 Mon Sep 17 00:00:00 2001 From: Maksudul Haque Date: Sun, 22 Jan 2023 23:54:13 +0600 Subject: [PATCH] [`pep8-naming`][`N806`] Don't mark `TypeVar` & `NewType` Assignment as Errors (#2085) closes /~https://github.com/charliermarsh/ruff/issues/1985 --- resources/test/fixtures/pep8_naming/N806.py | 6 ++++++ src/rules/pep8_naming/helpers.rs | 10 ++++++++++ src/rules/pep8_naming/rules.rs | 5 ++++- .../ruff__rules__pep8_naming__tests__N806_N806.py.snap | 8 ++++---- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/resources/test/fixtures/pep8_naming/N806.py b/resources/test/fixtures/pep8_naming/N806.py index 861bce4baaa8a..a1183dc97f7dc 100644 --- a/resources/test/fixtures/pep8_naming/N806.py +++ b/resources/test/fixtures/pep8_naming/N806.py @@ -1,5 +1,7 @@ import collections from collections import namedtuple +from typing import TypeVar +from typing import NewType GLOBAL: str = "foo" @@ -11,5 +13,9 @@ def f(): Camel = 0 CONSTANT = 0 _ = 0 + MyObj1 = collections.namedtuple("MyObj1", ["a", "b"]) MyObj2 = namedtuple("MyObj12", ["a", "b"]) + + T = TypeVar("T") + UserId = NewType('UserId', int) diff --git a/src/rules/pep8_naming/helpers.rs b/src/rules/pep8_naming/helpers.rs index 38641f8503d10..6abd5e1e80e9d 100644 --- a/src/rules/pep8_naming/helpers.rs +++ b/src/rules/pep8_naming/helpers.rs @@ -31,6 +31,16 @@ pub fn is_namedtuple_assignment(checker: &Checker, stmt: &Stmt) -> bool { }) } +pub fn is_type_var_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", "TypeVar"] + || call_path.as_slice() == ["typing", "NewType"] + }) +} + #[cfg(test)] mod tests { use super::{is_acronym, is_camelcase, is_mixed_case}; diff --git a/src/rules/pep8_naming/rules.rs b/src/rules/pep8_naming/rules.rs index 38c432b0deb41..4eb39c721845d 100644 --- a/src/rules/pep8_naming/rules.rs +++ b/src/rules/pep8_naming/rules.rs @@ -126,7 +126,10 @@ pub fn non_lowercase_variable_in_function( stmt: &Stmt, name: &str, ) { - if name.to_lowercase() != name && !helpers::is_namedtuple_assignment(checker, stmt) { + if name.to_lowercase() != name + && !helpers::is_namedtuple_assignment(checker, stmt) + && !helpers::is_type_var_assignment(checker, stmt) + { checker.diagnostics.push(Diagnostic::new( violations::NonLowercaseVariableInFunction(name.to_string()), Range::from_located(expr), diff --git a/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap b/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap index 5f099257c17a6..3c3d60643832b 100644 --- a/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap +++ b/src/rules/pep8_naming/snapshots/ruff__rules__pep8_naming__tests__N806_N806.py.snap @@ -5,20 +5,20 @@ expression: diagnostics - kind: NonLowercaseVariableInFunction: Camel location: - row: 11 + row: 13 column: 4 end_location: - row: 11 + row: 13 column: 9 fix: ~ parent: ~ - kind: NonLowercaseVariableInFunction: CONSTANT location: - row: 12 + row: 14 column: 4 end_location: - row: 12 + row: 14 column: 12 fix: ~ parent: ~