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 'unused import' in conjunction with quoted generic stub types #515

Closed
jnsnow opened this issue Feb 23, 2020 · 1 comment
Closed

Comments

@jnsnow
Copy link

jnsnow commented Feb 23, 2020

This one's a mouthful, but I did verify that this is still an issue with 2.1.1-23-g0af480e -- current master as of writing.

Consider this example:

from __future__ import annotations
from os import PathLike
from typing import Union

_PathType = Union[str, 'PathLike[str]']

def foo(arg: _PathType) -> str:
    return str(arg)

First, note that PathLike[str] is a case where MyPy uses a Generic type stub, but that the runtime object here is not subscriptable. In these cases, such types must be string quoted, even when using delayed annotations support from PEP 563.

See here for more information on this circumstance: https://mypy.readthedocs.io/en/stable/common_issues.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime

The warning produced is:

example.py:2:1 'os.PathLike' imported but unused

Next, even when moving this import below a TYPE_CHECKING stanza, pyflakes still warns.

Lastly, this is an error in pylint (pylint-dev/pylint#3299) and pycharm (As of 2019.3.2) as well. Worse, each tool has separate pragmas to silence the error.

This error appears related to #247 but that was about PEP 484 type hint comments, not string quoted type hints. The issue is possibly distinct from #510 because PEP 563 delayed annotations have no effect here: quoted types are a requisite; but these two reports may share a root cause. Pull #479 does not resolve this problem.

Hopefully that's good homework. :)
--js

@asottile
Copy link
Member

#510 (comment) includes this problem as well -- will probably solve both of them at the same time 👍

thanks for the thorough issue (let's continue the discussion there!)

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

No branches or pull requests

2 participants