What's pylint's TypeVar name specification?

Question:

Pylint gives a warning whenever something like this happens:

import typing

SEQ_FR = typing.TypeVar("SEQ_FR")
#^^^^^ gets underlined with the warning

The warning is like this: Type variable name "SEQ_FR" doesn't conform to predefined naming style. pylint(invalid-name)

I tried searching through Pylint’s documentations with no luck on finding the exact regex / specifications used. Doesn’t seem like I can pass a custom regex onto Pylint for this as well, unlike regular variables, methods, functions, classes, etc.

What is the specification used by Pylint to flag TypeVar variables as valid or invalid names?

Asked By: TNTzx

||

Answers:

You can find the rule used in the Pylint messages documentation; this error is named invalid-name, so the specific documentation can be found on the invalid-name / C0103 page, which has a TypeVar rule in the Predefined Naming Patterns section:

Name type: typevar
Good Names: T, _CallableT, _T_co, AnyStr, DeviceTypeT, IPAddressT
Bad Names: DICT_T, CALLABLE_T, ENUM_T, DeviceType, _StrType, TAnyStr

It doesn’t document the exact regex rule here, but Pylint will actually include the regex used in the error message when you use the --include-naming-hint=y command-line switch *):

Type variable name "SEQ_FR" doesn't conform to predefined naming style ('^_{0,2}(?!T[A-Z])(?:[A-Z]+|(?:[A-Z]+[a-z]+)+T?(?<!Type))(?:_co(?:ntra)?)?$' pattern) (invalid-name)

Alternatively, you can find the regex for typevars in the source code.

Breaking the pattern down, typevar names are compliant when following these rules:

  • Optionally start with 0-2 underscores
  • not starting with T<capital letter>
  • either
    • all capital letters and no underscores,
    • or a PascalCaseWord ✝) optionally ending in T, no underscores, and not ending with Type
  • with an optional _co or _contra ending.

Put differently, typevar names must be either PascalCase ✝) or all-caps, are optionally protected (_) or private (__), are optionally marked as covariant (_co) or contravariant (_contra), and should not end in Type.

A compliant name for your example could be SeqFr or SeqFrT; the T suffix is meant to make it clear the SnakeCaseT name is a typevar.

Alternatively, you can specify your own regex with the --typevar-rgx=<regex> command-line switch *).

Note: As Pierre Sassoulas (the maintainer of Pylint) pointed out in a comment: There is no PEP-8 convention (yet) for typevar naming; instead the PyLint team captured the rules observed in Python projects and the type hinting documentation. The exact rule is therefore still subject to change if an official convention were to be created.


*) The Visual Studio Code settings for the Python extension include a python.linting.pylintArgs option that takes a list of command-line switches.

✝) PascalCase is also known as CapitalizedWords, UpperCamelCase or StudlyCase. Don’t confuse this with camelCase (initial letter lowercase) or snake_case (all lowercase with underscores). I regularly do! When in doubt, Wikipedia has a handly table of multi-word formats.

Answered By: Martijn Pieters
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.