mirror of https://github.com/astral-sh/ruff
[`pep8-naming`][`N806`] Don't mark `TypeVar` & `NewType` Assignment as Errors (#2085)
closes https://github.com/charliermarsh/ruff/issues/1985
This commit is contained in:
parent
1beedf20f9
commit
75e16c0ce5
|
|
@ -1,5 +1,7 @@
|
||||||
import collections
|
import collections
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
from typing import TypeVar
|
||||||
|
from typing import NewType
|
||||||
|
|
||||||
GLOBAL: str = "foo"
|
GLOBAL: str = "foo"
|
||||||
|
|
||||||
|
|
@ -11,5 +13,9 @@ def f():
|
||||||
Camel = 0
|
Camel = 0
|
||||||
CONSTANT = 0
|
CONSTANT = 0
|
||||||
_ = 0
|
_ = 0
|
||||||
|
|
||||||
MyObj1 = collections.namedtuple("MyObj1", ["a", "b"])
|
MyObj1 = collections.namedtuple("MyObj1", ["a", "b"])
|
||||||
MyObj2 = namedtuple("MyObj12", ["a", "b"])
|
MyObj2 = namedtuple("MyObj12", ["a", "b"])
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
UserId = NewType('UserId', int)
|
||||||
|
|
|
||||||
|
|
@ -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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{is_acronym, is_camelcase, is_mixed_case};
|
use super::{is_acronym, is_camelcase, is_mixed_case};
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,10 @@ pub fn non_lowercase_variable_in_function(
|
||||||
stmt: &Stmt,
|
stmt: &Stmt,
|
||||||
name: &str,
|
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(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
violations::NonLowercaseVariableInFunction(name.to_string()),
|
violations::NonLowercaseVariableInFunction(name.to_string()),
|
||||||
Range::from_located(expr),
|
Range::from_located(expr),
|
||||||
|
|
|
||||||
|
|
@ -5,20 +5,20 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
NonLowercaseVariableInFunction: Camel
|
NonLowercaseVariableInFunction: Camel
|
||||||
location:
|
location:
|
||||||
row: 11
|
row: 13
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 11
|
row: 13
|
||||||
column: 9
|
column: 9
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
NonLowercaseVariableInFunction: CONSTANT
|
NonLowercaseVariableInFunction: CONSTANT
|
||||||
location:
|
location:
|
||||||
row: 12
|
row: 14
|
||||||
column: 4
|
column: 4
|
||||||
end_location:
|
end_location:
|
||||||
row: 12
|
row: 14
|
||||||
column: 12
|
column: 12
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue