Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3299 false positives with names in string literal type annotations #7400

Merged
merged 12 commits into from
Sep 4, 2022
4 changes: 3 additions & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import TYPE_CHECKING, Any, NamedTuple

import astroid
from astroid import nodes
from astroid import extract_node, nodes
from astroid.typing import InferenceResult

from pylint.checkers import BaseChecker, utils
Expand Down Expand Up @@ -2922,6 +2922,8 @@ def visit_const(self, node: nodes.Const) -> None:
if not utils.is_node_in_type_annotation_context(node):
return
if not node.value.isidentifier():
annotation = extract_node(node.value)
self._store_type_annotation_node(annotation)
return

# Check if parent's or grandparent's first child is typing.Literal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
"""Test if pylint sees names inside string literal type annotations. #3299"""
from argparse import ArgumentParser, Namespace
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
import os
from os import PathLike
from pathlib import Path
from typing import Set
from typing import NoReturn, Set

# unused-import shouldn't be emitted for Path
example1: Set["Path"] = set()

def example2(_: "ArgumentParser"):
"""unused-import shouldn't be emitted for ArgumentParser"""
def example2(_: "ArgumentParser") -> "NoReturn":
"""unused-import shouldn't be emitted for ArgumentParser or NoReturn."""
while True:
pass

def example3(_: "os.PathLike[str]") -> None:
"""unused-import shouldn't be emitted for os."""

def example4(_: "PathLike[str]") -> None:
"""unused-import shouldn't be emitted for PathLike."""

# pylint: disable=too-few-public-methods
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
class Class:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# pylint: disable=missing-docstring
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved
import graphlib
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
from graphlib import TopologicalSorter

def example(
sorter1: "graphlib.TopologicalSorter[int]",
sorter2: "TopologicalSorter[str]",
) -> None:
"""unused-import shouldn't be emitted for graphlib or TopologicalSorter."""
print(sorter1, sorter2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.9