Pylint: Pylint incorrectly flags as `unused-import` things that are used as variable type annotations

Created on 6 Aug 2016  路  15Comments  路  Source: PyCQA/pylint

Steps to reproduce

from typing import Optional

a = None  # type: Optional[str]

Current behavior

pylint flags Optional as an unused-import

Expected behavior

pylint shouldn't flag Optional as unused-import

pylint --version output

pylint 1.6.4,
astroid 1.4.8
Python 3.5.2 (default, Jun 28 2016, 08:46:01)
[GCC 6.1.1 20160602]

Supporting documentation:
https://www.python.org/dev/peps/pep-0484/

bug

Most helpful comment

We recently switched to typed_ast from Dropbox, which has support for extracting type comments. This is fixed in the master branch, please give it a go when you get a chance!

All 15 comments

Yes, we do not currently process type comments. A planned support for PEP 484 is in our roadmap, but I am not sure how fast we will reach it.

Partially fixed in #1231, but only if they are used as a sting annotations for function attributes.

The full support is in question, as I don't personally see a way how to extract comments from AST(eroid) representation of the module... Any help or advice will be very appreciated.

You can't extract comments from astroid.

Hi. I built a pylint extension inspired by https://gist.github.com/PCManticore/eca887488248b7594d37 to handle precisely this for our code base. With it all the unused imports go away, and it'll even warn you of stuff you haven't imported.

It also adds some inferences for special typing things like NamedTuple, NewType, TypeVar and handles subscripts on Generic types so that things like Employee = NamedTuple('Employee', [('name', str), ('id', int)]) won't generate an Invalid Constant Name error.

So if you think this is valuable I can work on cleaning up the code and making a pull request. I imagine this would be an extension that users can enable if they want to. Also suggestions on the name are welcome. Mine is called mypy_lint_enhancer.py but that's a really terrible name. Maybe extensions/pep484.py or extensions/typing.py.

Cheers!

@euresti That sounds pretty neat! Sure thing, please send a pull request. Depending on the additions, it might be something enabled by default.

so what's the status with this? @euresti code did make it or not?

It never made it in. My code is here:
https://github.com/PyCQA/pylint/pull/1253
and here
https://github.com/PyCQA/astroid/pull/392

I don't use pylint anymore nor type comments anymore though. So I don't really have much need for this. Sorry.

Pyflakes on python3 makes a vastly better experience IMO

Certbot would love to see this!

that would be very handy indeed!

We recently switched to typed_ast from Dropbox, which has support for extracting type comments. This is fixed in the master branch, please give it a go when you get a chance!

@PCManticore thanks, any non-dev release plans for it? :smile:

Probably right after Python 3.7 comes out. We still have some issues to flesh out until then (check the 2.0 milestone from both pylint and astroid).

@gaborbernat Already launched, forgot to reply.

Are there any plans to also fix this for python 2? (pylint 1.9)?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TBoshoven picture TBoshoven  路  3Comments

mrginglymus picture mrginglymus  路  3Comments

PCManticore picture PCManticore  路  3Comments

Hubro picture Hubro  路  3Comments

pylint-bot picture pylint-bot  路  3Comments