Detect type hints: PEP484

Bug #1587170 reported by Thomas Kriechbaumer
22
This bug affects 4 people
Affects Status Importance Assigned to Milestone
Pyflakes
New
Undecided
Unassigned

Bug Description

Hi,

thanks for this really great tool!
One issues I stumbled upon was that type hints seem to be ignored / not detected.
For example, we have an import at the top of a file:

    from typing import List

And then later in an __init__ function something like this:

    self.scripts = [] # type: List[script.Script]

Now running pyflakes (through flake8) gives me this error:

    foo.py:4:1: F401 'List' imported but unused

PEP484 defines this kind of type comments.
Is there an option to enable type hint detection?
Or can we have that as feature request? I guess I'm fine with type comments only for the time being, but of course a fully PEP484 compliant solution would be greatly appreciated :)

Thanks!

Revision history for this message
asmeurer (asmeurer) wrote :

Is it really necessary to have the unused imports for the typing comments to work?

Revision history for this message
Ian Cordasco (icordasc) wrote :

asmeurer I've asked repeatedly that it not be necessary. That said, mypy-lang folks continue to insist it is necessary.

Regardless, I don't think pyflakes wants to do PEP 484 work since that's mypy-lang's area of expertise Thomas. Further, pyflakes needs a better AST considering the current one does *not* preserve comments. Without preserving comments, PyFlakes cannot introspect comments for # type: Foo to find that it is being used.

Revision history for this message
Tim Abbott (tabbott) wrote :

https://github.com/dropbox/typed_ast is a fork of the AST module that preserves type comments (and is used by mypy-lang); using that might be an option for allowing pyflakes to support the Python 2 type comment syntax.

Revision history for this message
David (qwertystop) wrote :

Apart from whether or not pyflakes intends to support type hinting, it currently marks it as a syntax error (on arguments and return types):

def echo(arg: str) -> str:
    return arg

It marks invalid syntax at the first colon, and at the dash if the type hint on argument is removed.

Revision history for this message
asmeurer (asmeurer) wrote :

Make sure your pyflakes is running in Python 3. Pyflakes detects syntax errors based on the version of Python it is running from. If you run pyflakes from Python 2 on Python 3-only code, it will complain about syntax errors. The easiest way to do this is to run

python3 -m pyflakes yourfile.py

Revision history for this message
David (qwertystop) wrote :

Oh, oops. Found that just a few minutes ago when I was going to file something similar for keyword-only args. Was running pyflakes at two removes (via prospector, through the "syntastic" vim plugin), and didn't realize that it wasn't automatic for versions.

Revision history for this message
John L. Villalovos (happycamp) wrote :

Please fix the imported but unused errors for the typing library.

I'm running into those when adding type hints to my python code.

Currently doing:

 from typing import Any, Dict, List, Tuple, Union # noqa

As a work-around.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.