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

Spurious import warning for type used in nested quoted annotation #447

Closed
PeterJCLaw opened this issue May 28, 2019 · 2 comments · Fixed by #479
Closed

Spurious import warning for type used in nested quoted annotation #447

PeterJCLaw opened this issue May 28, 2019 · 2 comments · Fixed by #479

Comments

@PeterJCLaw
Copy link
Contributor

This is similar to #290 and #313, but seems to still be an issue.

If you import a type and then use it only within a type annotation which includes a nested quoted part, then pyflakes reports the import as unused.

from queue import Queue  # F401: 'queue.Queue' imported but unused
from typing import Optional

def foo(queue: Optional['Queue[int]'] = None) -> None:
    print(queue)

# Revealed type is 'def (queue: Union[queue.Queue[builtins.int], None] =)'
reveal_type(foo)

This can be worked around by quoting the whole of the type annotation:

def foo(queue: 'Optional[Queue[int]]' = None) -> None:
    print(queue)

I'm using pyflakes 2.1.1 on Python 3.5.2 on Linux.

@asottile
Copy link
Member

asottile commented Jun 3, 2019

When fixing this, be careful not to break Literal['foo'] (and any other type annotations which require actual strings)

@asottile
Copy link
Member

asottile commented Oct 5, 2019

I wrote an implementation of this in #479 -- would appreciate anyone to try it out and review the code \o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants