diff options
| author | Levi Gruspe <mail.levig@gmail.com> | 2022-09-04 19:10:41 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-04 13:10:41 +0200 |
| commit | 2f766a1dcde49c76de1c826eb8df8cbbeb2e7c71 (patch) | |
| tree | 415a9cf3114b51f6cf832f40b7a31224c90b386b /tests | |
| parent | 89737663f6a8891f5f5ae3e9a7d689cfec569ee8 (diff) | |
| download | pylint-git-2f766a1dcde49c76de1c826eb8df8cbbeb2e7c71.tar.gz | |
Fix #3299 false positives with names in string literal type annotations (#7400)
Don't emit 'unused-variable' or 'unused-import' on names in string literal type annotations (#3299)
Don't treat strings inside typing.Literal as names
Diffstat (limited to 'tests')
9 files changed, 102 insertions, 0 deletions
diff --git a/tests/checkers/unittest_utils.py b/tests/checkers/unittest_utils.py index 8b9189892..f68a48dbb 100644 --- a/tests/checkers/unittest_utils.py +++ b/tests/checkers/unittest_utils.py @@ -489,3 +489,29 @@ def test_deprecation_check_messages() -> None: records[0].message.args[0] == "utils.check_messages will be removed in favour of calling utils.only_required_for_messages in pylint 3.0" ) + + +def test_is_typing_literal() -> None: + code = astroid.extract_node( + """ + from typing import Literal as Lit, Set as Literal + import typing as t + + Literal #@ + Lit #@ + t.Literal #@ + """ + ) + + assert not utils.is_typing_literal(code[0]) + assert utils.is_typing_literal(code[1]) + assert utils.is_typing_literal(code[2]) + + code = astroid.extract_node( + """ + Literal #@ + typing.Literal #@ + """ + ) + assert not utils.is_typing_literal(code[0]) + assert not utils.is_typing_literal(code[1]) diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation.py b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation.py new file mode 100644 index 000000000..389647657 --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation.py @@ -0,0 +1,26 @@ +"""Test if pylint sees names inside string literal type annotations. #3299""" +# pylint: disable=too-few-public-methods + +from argparse import ArgumentParser, Namespace +import os +from os import PathLike +from pathlib import Path +from typing import NoReturn, Set + +# unused-import shouldn't be emitted for Path +example1: Set["Path"] = set() + +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.""" + +class Class: + """unused-import shouldn't be emitted for Namespace""" + cls: "Namespace" diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.py b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.py new file mode 100644 index 000000000..00bf5799f --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.py @@ -0,0 +1,9 @@ +# pylint: disable=missing-docstring + +from typing import TypeAlias + +def unused_variable_should_not_be_emitted(): + """unused-variable shouldn't be emitted for Example.""" + Example: TypeAlias = int + result: set["Example"] = set() + return result diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.rc b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.rc new file mode 100644 index 000000000..68a8c8ef1 --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.10 diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.py b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.py new file mode 100644 index 000000000..497f64937 --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.py @@ -0,0 +1,20 @@ +# pylint: disable=missing-docstring + +from argparse import ArgumentParser # [unused-import] +from argparse import Namespace # [unused-import] +from typing import Literal as Lit +import typing as t + +# str inside Literal shouldn't be treated as names +example1: t.Literal["ArgumentParser", Lit["Namespace", "ArgumentParser"]] + + +def unused_variable_example(): + hello = "hello" # [unused-variable] + world = "world" # [unused-variable] + example2: Lit["hello", "world"] = "hello" + return example2 + + +# pylint shouldn't crash with the following strings in a type annotation context +example3: Lit["", " ", "?"] = "?" diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.rc b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.rc new file mode 100644 index 000000000..85fc502b3 --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.8 diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.txt b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.txt new file mode 100644 index 000000000..082595bff --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.txt @@ -0,0 +1,4 @@ +unused-import:3:0:3:35::Unused ArgumentParser imported from argparse:UNDEFINED +unused-import:4:0:4:30::Unused Namespace imported from argparse:UNDEFINED +unused-variable:13:4:13:9:unused_variable_example:Unused variable 'hello':UNDEFINED +unused-variable:14:4:14:9:unused_variable_example:Unused variable 'world':UNDEFINED diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.py b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.py new file mode 100644 index 000000000..1258844cd --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.py @@ -0,0 +1,11 @@ +# pylint: disable=missing-docstring + +import graphlib +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) diff --git a/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.rc b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.rc new file mode 100644 index 000000000..16b75eea7 --- /dev/null +++ b/tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.9 |
