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

Pylint incorrectly flags as unused-import things that are used in string literal type annotations #3299

Closed
sanyassh opened this issue Dec 10, 2019 · 3 comments · Fixed by #7400
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@sanyassh
Copy link

Steps to reproduce

pylint_example.py:

class Class:
    pass

pylint_example2.py:

from pylint_example import Class


class Class2:
    cls: 'Class'

pylint pylint_example2.py

************* Module pylint_example2
pylint_example2.py:4:0: R0903: Too few public methods (0/2) (too-few-public-methods)
pylint_example2.py:1:0: W0611: Unused Class imported from pylint_example (unused-import)

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

Current behavior

String literal in type annotation is considered "unused" and warning is emitted.

Expected behavior

There should not be any warning about unused import.

pylint --version output

pylint 2.4.4
astroid 2.3.3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0]

@PCManticore PCManticore added Bug 🪲 Good first issue Friendly and approachable by new contributors labels Dec 12, 2019
@jnsnow
Copy link

jnsnow commented Feb 23, 2020

I have some additional information related to this issue.

Even though there are workarounds by using e.g.

from __future__ import annotations

there are some type definitions that actually still require you to use string quoting, because typeshed expresses some types as subscriptable generics, e.g. os.PathLike must be expressed as 'os.PathLike[str]' -- the quotes are a requirement, because this type cannot be subscripted at runtime.

Sadly, every other CQA tool also fails here: pyflakes and pycharm both do not understand this annotation and all three tools have different silencing methods.

@vbraun
Copy link

vbraun commented Apr 23, 2020

Flake8 can do it for the record, and does not emit an F401 on the example:

# flake8 pylint_example2.py
# echo $?
0

@Pierre-Sassoulas Pierre-Sassoulas added the Help wanted 🙏 Outside help would be appreciated, good for new contributors label Mar 2, 2021
@Pierre-Sassoulas Pierre-Sassoulas pinned this issue Apr 13, 2021
@Pierre-Sassoulas Pierre-Sassoulas unpinned this issue Jul 1, 2021
@dmtucker
Copy link

#4315 also reproduces when the type is a string.

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Dec 12, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Bug 🪲 labels Jul 5, 2022
lggruspe added a commit to lggruspe/pylint that referenced this issue Sep 2, 2022
lggruspe added a commit to lggruspe/pylint that referenced this issue Sep 2, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.2 milestone Sep 3, 2022
Pierre-Sassoulas pushed a commit that referenced this issue Sep 4, 2022
…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
Pierre-Sassoulas pushed a commit to Pierre-Sassoulas/pylint that referenced this issue Sep 6, 2022
… annotations (pylint-dev#7400)

Don't emit 'unused-variable' or 'unused-import' on names in string literal type annotations (pylint-dev#3299)
Don't treat strings inside typing.Literal as names
Pierre-Sassoulas pushed a commit that referenced this issue Sep 6, 2022
…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
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.15.3, 2.15.1 Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants