-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3299 false positives with names in string literal type annotatio…
…ns (#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
- Loading branch information
Showing
12 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fix false positive for ``unused-variable`` and ``unused-import`` when a name is only used in a string literal type annotation. | ||
|
||
Closes #3299 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/functional/u/unused/unused_name_in_string_literal_type_annotation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
9 changes: 9 additions & 0 deletions
9
tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
2 changes: 2 additions & 0 deletions
2
tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py310.rc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[testoptions] | ||
min_pyver=3.10 |
20 changes: 20 additions & 0 deletions
20
tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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["", " ", "?"] = "?" |
2 changes: 2 additions & 0 deletions
2
tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.rc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[testoptions] | ||
min_pyver=3.8 |
4 changes: 4 additions & 0 deletions
4
tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py38.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
11 changes: 11 additions & 0 deletions
11
tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
2 changes: 2 additions & 0 deletions
2
tests/functional/u/unused/unused_name_in_string_literal_type_annotation_py39.rc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[testoptions] | ||
min_pyver=3.9 |