Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10696.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed false positive for ``invalid-name`` with ``typing.Annotated``.

Closes #10696
34 changes: 23 additions & 11 deletions pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,17 +488,7 @@ def visit_assignname( # pylint: disable=too-many-branches,too-many-statements
return

# Check classes (TypeVar's are classes so they need to be excluded first)
elif isinstance(inferred_assign_type, nodes.ClassDef) or (
isinstance(inferred_assign_type, bases.Instance)
and {"EnumMeta", "TypedDict"}.intersection(
{
ancestor.name
for ancestor in cast(
InferenceResult, inferred_assign_type
).mro()
}
)
):
elif self._should_check_class_regex(inferred_assign_type):
self._check_name("class", node.name, node)

# Don't emit if the name redefines an import in an ImportError except handler
Expand Down Expand Up @@ -576,6 +566,28 @@ def _meets_exception_for_non_consts(
regexp = self._name_regexps["variable"]
return regexp.match(name) is not None

def _should_check_class_regex(
self, inferred_assign_type: InferenceResult | None
) -> bool:
if isinstance(inferred_assign_type, nodes.ClassDef):
return True
if isinstance(inferred_assign_type, bases.Instance) and {
"EnumMeta",
"TypedDict",
}.intersection(
{
ancestor.name
for ancestor in cast(InferenceResult, inferred_assign_type).mro()
}
):
return True
if (
isinstance(inferred_assign_type, nodes.FunctionDef)
and inferred_assign_type.qname() == "typing.Annotated"
):
return True
return False

def _recursive_check_names(self, args: list[nodes.AssignName]) -> None:
"""Check names in a possibly recursive list <arg>."""
for arg in args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ def A(): # [invalid-name]
VERSION = version("ty") # uninferable
except PackageNotFoundError:
VERSION = "0.0.0"


from typing import Annotated
IntWithAnnotation = Annotated[int, "anything"]
Loading