Skip to content

Commit

Permalink
[pep8-naming][N806] Don't mark TypeVar & NewType Assignment a…
Browse files Browse the repository at this point in the history
…s Errors (#2085)

closes #1985
  • Loading branch information
saadmk11 committed Jan 22, 2023
1 parent 1beedf2 commit 75e16c0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions resources/test/fixtures/pep8_naming/N806.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import collections
from collections import namedtuple
from typing import TypeVar
from typing import NewType

GLOBAL: str = "foo"

Expand All @@ -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)
10 changes: 10 additions & 0 deletions src/rules/pep8_naming/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
5 changes: 4 additions & 1 deletion src/rules/pep8_naming/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
Expand Down

0 comments on commit 75e16c0

Please sign in to comment.