Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TypeVar name regex #7322

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/data/messages/i/invalid-name/details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ The following type of names are checked with a predefined pattern:
| Name type | Good names | Bad names |
+====================+===================================================+============================================================+
| ``typevar`` | ``T``, ``_CallableT``, ``_T_co``, ``AnyStr``, | ``DICT_T``, ``CALLABLE_T``, ``ENUM_T``, ``DeviceType``, |
| | ``DeviceTypeT``, ``IPAddressT`` | ``_StrType`` |
| | ``DeviceTypeT``, ``IPAddressT`` | ``_StrType``, ``TAnyStr`` |
+--------------------+---------------------------------------------------+------------------------------------------------------------+

Custom regular expressions
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/7322.false-positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve default TypeVar name regex. Disallow names prefixed with ``T``.
E.g. use ``AnyStrT`` instead of ``TAnyStr``.
cdce8p marked this conversation as resolved.
Show resolved Hide resolved

Refs #7322
2 changes: 1 addition & 1 deletion pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# Default patterns for name types that do not have styles
DEFAULT_PATTERNS = {
"typevar": re.compile(
r"^_{0,2}(?:[^\W\da-z_]+|(?:[^\W\da-z_]+[^\WA-Z_]+)+T?(?<!Type))(?:_co(?:ntra)?)?$"
r"^_{0,2}(?!T[A-Z])(?:[A-Z]+|(?:[A-Z]+[a-z]+)+T?(?<!Type))(?:_co(?:ntra)?)?$"
)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/functional/a/assigning/assigning_non_slot.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ def test(self):
TypeVar,
)

TYPE = TypeVar('TYPE')
TypeT = TypeVar('TypeT')


class Cls(Generic[TYPE]):
class Cls(Generic[TypeT]):
""" Simple class with slots """
__slots__ = ['value']

Expand Down
5 changes: 5 additions & 0 deletions tests/functional/t/typevar_naming_style_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@
AnyStr = TypeVar("AnyStr")
DeviceTypeT = TypeVar("DeviceTypeT")
HVACModeT = TypeVar("HVACModeT")
TodoT = TypeVar("TodoT")
TypeT = TypeVar("TypeT")
_IPAddress = TypeVar("_IPAddress")
CALLABLE_T = TypeVar("CALLABLE_T") # [invalid-name]
DeviceType = TypeVar("DeviceType") # [invalid-name]
IPAddressU = TypeVar("IPAddressU") # [invalid-name]

# Wrong prefix
TAnyStr = TypeVar("TAnyStr") # [invalid-name]

# camelCase names with prefix
badName = TypeVar("badName") # [invalid-name]
badName_co = TypeVar("badName_co", covariant=True) # [invalid-name]
Expand Down
21 changes: 11 additions & 10 deletions tests/functional/t/typevar_naming_style_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ typevar-double-variance:23:0:23:4::TypeVar cannot be both covariant and contrava
typevar-name-incorrect-variance:23:0:23:4::Type variable name does not reflect variance:INFERENCE
typevar-double-variance:24:0:24:8::TypeVar cannot be both covariant and contravariant:INFERENCE
typevar-name-incorrect-variance:24:0:24:8::Type variable name does not reflect variance:INFERENCE
invalid-name:35:0:35:10::"Type variable name ""CALLABLE_T"" doesn't conform to predefined naming style":HIGH
invalid-name:36:0:36:10::"Type variable name ""DeviceType"" doesn't conform to predefined naming style":HIGH
invalid-name:37:0:37:10::"Type variable name ""IPAddressU"" doesn't conform to predefined naming style":HIGH
invalid-name:40:0:40:7::"Type variable name ""badName"" doesn't conform to predefined naming style":HIGH
invalid-name:41:0:41:10::"Type variable name ""badName_co"" doesn't conform to predefined naming style":HIGH
invalid-name:42:0:42:14::"Type variable name ""badName_contra"" doesn't conform to predefined naming style":HIGH
invalid-name:46:4:46:13::"Type variable name ""a_BadName"" doesn't conform to predefined naming style":HIGH
invalid-name:47:4:47:26::"Type variable name ""a_BadNameWithoutContra"" doesn't conform to predefined naming style":HIGH
typevar-name-incorrect-variance:47:4:47:26::"Type variable name does not reflect variance. ""a_BadNameWithoutContra"" is contravariant, use ""a_BadNameWithoutContra_contra"" instead":INFERENCE
invalid-name:49:13:49:29::"Type variable name ""a_BadName_contra"" doesn't conform to predefined naming style":HIGH
invalid-name:37:0:37:10::"Type variable name ""CALLABLE_T"" doesn't conform to predefined naming style":HIGH
invalid-name:38:0:38:10::"Type variable name ""DeviceType"" doesn't conform to predefined naming style":HIGH
invalid-name:39:0:39:10::"Type variable name ""IPAddressU"" doesn't conform to predefined naming style":HIGH
invalid-name:42:0:42:7::"Type variable name ""TAnyStr"" doesn't conform to predefined naming style":HIGH
invalid-name:45:0:45:7::"Type variable name ""badName"" doesn't conform to predefined naming style":HIGH
invalid-name:46:0:46:10::"Type variable name ""badName_co"" doesn't conform to predefined naming style":HIGH
invalid-name:47:0:47:14::"Type variable name ""badName_contra"" doesn't conform to predefined naming style":HIGH
invalid-name:51:4:51:13::"Type variable name ""a_BadName"" doesn't conform to predefined naming style":HIGH
invalid-name:52:4:52:26::"Type variable name ""a_BadNameWithoutContra"" doesn't conform to predefined naming style":HIGH
typevar-name-incorrect-variance:52:4:52:26::"Type variable name does not reflect variance. ""a_BadNameWithoutContra"" is contravariant, use ""a_BadNameWithoutContra_contra"" instead":INFERENCE
invalid-name:54:13:54:29::"Type variable name ""a_BadName_contra"" doesn't conform to predefined naming style":HIGH