Pylint: typing module leads to lots of invalid-name warnings

Created on 25 Jan 2017  路  9Comments  路  Source: PyCQA/pylint

Here's an example:

from typing import Union, Dict

MyDict = Dict[str, str]
MyOptions = Union[int, str]

There are lots more, Generic[], Callable[], Tuple[], Optional[], NewType(), NamedTuple()

Current behavior

C: 3, 0: Invalid constant name "MyDict" (invalid-name)
C: 4, 0: Invalid constant name "MyOptions" (invalid-name)

Expected behavior

No errors?

pylint --version output

pylint 1.6.5,
astroid 1.4.9
Python 2.7.12 (default, Jun 29 2016, 14:05:02)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]

I have some ideas about how to fix this. One involves adding a brain to astroid so that Dict[X] and Union[X] return ClassDefs but I'm not sure if that's the correct thing to do.

Most helpful comment

Yes, I agree with @euresti. These should be considered classes and class-rgx should be used for validating the name. I think making them classes from astroid.brain would help, what do you think, @euresti ? Can you try a proof-of-concept to see that this works?

All 9 comments

This is behaving as expected, and should be closed.

The problem isn't with Dict or Union. Because MyDict and MyOptions are defined at module scope, pylint assumes they are constants, and that they should follow the constant regex: (([A-Z_][A-Z0-9_]*)|(__.*__))$. You should either change the variables to MYDICT and MYOPTIONS, or change the regex by using const-rgx either on the commandline, or in your pylintrc.

I'm not so sure. These types are more like Foo = namedtuple('Foo', 'a b c') which pylint (I guess technically astroid) recognizes as a class assignment and so uses the class-rgx.

Yes, I agree with @euresti. These should be considered classes and class-rgx should be used for validating the name. I think making them classes from astroid.brain would help, what do you think, @euresti ? Can you try a proof-of-concept to see that this works?

Would it be possible to also add support for mypy's TypedDict?

FWIW PEP 8 has separate naming guidelines for type variables (short names, _co and _contra suffixes), I suggest implementing them different from other things in pylint as well. https://www.python.org/dev/peps/pep-0008/#type-variable-names

I can't reproduce this with the latest master of pylint and astroid. Please could someone confirm.

@AWhetter Yup, can't reproduce this either!

I'm seeing this on pylint 1.9.5. Does anyone what what version this was fixed in?

pylint 1.9.5, 
astroid 1.6.6
Python 2.7.15+ (default, Oct  7 2019, 17:39:04) 
[GCC 7.4.0]

@stephen-smith This was fixed in the 2.0 branch of releases, it's not fixed in 1.9.X. It's not going to be backported, so you'd have to upgrade to 2.X once moving from Python 2.7.

Was this page helpful?
0 / 5 - 0 ratings