What's pylint's TypeVar name specification?

Advertisements

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?

>Solution :

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 CamelCaseWord optionally ending in T, no underscores, and not ending with Type
  • with an optional _co or _contra ending.

A compliant name for your example could be SeqFr, so using CamelCase.

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


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

Leave a ReplyCancel reply