Skip to content

Commit 150f23c

Browse files
[Backport maintenance/4.0.x] Allow digits in ParamSpec and TypeVarTuple names for invalid-name check (#11095)
Allow digits in ParamSpec and TypeVarTuple names for invalid-name check (#11091) The default paramspec-rgx and typevartuple-rgx patterns rejected names containing digits (e.g. Ec2P, S3Ts), emitting a false invalid-name (C0103). Allow digits in the lowercase segments of both patterns, consistent with the typevar fix from #11078. Closes #11090 (cherry picked from commit fe87ce2) Co-authored-by: Karl Hill <karlhillx@gmail.com>
1 parent 8b6251e commit 150f23c

5 files changed

Lines changed: 40 additions & 27 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Allow digits in ParamSpec and TypeVarTuple names for `invalid-name` check.
2+
3+
The default `paramspec-rgx` and `typevartuple-rgx` patterns rejected names
4+
containing digits (e.g. ``Ec2P``, ``S3Ts``), emitting a false ``invalid-name``
5+
(C0103). Allow digits in the lowercase segments, consistent with the
6+
``typevar`` and ``typealias`` patterns.
7+
8+
Closes #11090

pylint/checkers/base/name_checker/checker.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
"typevar": re.compile(
4343
r"^_{0,2}(?!T[A-Z])(?:[A-Z]+|(?:[A-Z]+[a-z0-9]+)+(?:T)?(?<!Type))(?:_co(?:ntra)?)?$"
4444
),
45-
"paramspec": re.compile(r"^_{0,2}(?:[A-Z]+|(?:[A-Z]+[a-z]+)+(?:P)?(?<!Type))$"),
46-
"typevartuple": re.compile(r"^_{0,2}(?:[A-Z]+|(?:[A-Z]+[a-z]+)+(?:Ts)?(?<!Type))$"),
45+
"paramspec": re.compile(r"^_{0,2}(?:[A-Z]+|(?:[A-Z]+[a-z0-9]+)+(?:P)?(?<!Type))$"),
46+
"typevartuple": re.compile(
47+
r"^_{0,2}(?:[A-Z]+|(?:[A-Z]+[a-z0-9]+)+(?:Ts)?(?<!Type))$"
48+
),
4749
"typealias": re.compile(
4850
r"^_{0,2}(?!T[A-Z]|Type)[A-Z]+[a-z0-9]+(?:[A-Z][a-z0-9]+)*$"
4951
),
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
typevar-name-incorrect-variance:11:0:11:21::"Type variable name does not reflect variance. ""GoodNameWithoutContra"" is contravariant, use ""GoodNameWithoutContra_contra"" instead":INFERENCE
2-
typevar-double-variance:19:0:19:1::TypeVar cannot be both covariant and contravariant:INFERENCE
3-
typevar-name-incorrect-variance:19:0:19:1::Type variable name does not reflect variance:INFERENCE
4-
typevar-double-variance:23:0:23:4::TypeVar cannot be both covariant and contravariant:INFERENCE
5-
typevar-name-incorrect-variance:23:0:23:4::Type variable name does not reflect variance:INFERENCE
6-
typevar-double-variance:24:0:24:8::TypeVar cannot be both covariant and contravariant:INFERENCE
7-
typevar-name-incorrect-variance:24:0:24:8::Type variable name does not reflect variance:INFERENCE
8-
invalid-name:42:0:42:10::"Type variable name ""CALLABLE_T"" doesn't conform to predefined naming style":HIGH
9-
invalid-name:43:0:43:10::"Type variable name ""DeviceType"" doesn't conform to predefined naming style":HIGH
10-
invalid-name:44:0:44:10::"Type variable name ""IPAddressU"" doesn't conform to predefined naming style":HIGH
11-
invalid-name:47:0:47:7::"Type variable name ""TAnyStr"" doesn't conform to predefined naming style":HIGH
12-
invalid-name:50:0:50:7::"Type variable name ""badName"" doesn't conform to predefined naming style":HIGH
13-
invalid-name:51:0:51:10::"Type variable name ""badName_co"" doesn't conform to predefined naming style":HIGH
14-
invalid-name:52:0:52:14::"Type variable name ""badName_contra"" doesn't conform to predefined naming style":HIGH
15-
invalid-name:56:4:56:13::"Type variable name ""a_BadName"" doesn't conform to predefined naming style":HIGH
16-
invalid-name:57:4:57:26::"Type variable name ""a_BadNameWithoutContra"" doesn't conform to predefined naming style":HIGH
17-
typevar-name-incorrect-variance:57:4:57:26::"Type variable name does not reflect variance. ""a_BadNameWithoutContra"" is contravariant, use ""a_BadNameWithoutContra_contra"" instead":INFERENCE
18-
invalid-name:59:13:59:29::"Type variable name ""a_BadName_contra"" doesn't conform to predefined naming style":HIGH
19-
invalid-name:68:0:68:7::"Type variable name ""badName"" doesn't conform to predefined naming style":HIGH
20-
typevar-double-variance:69:0:69:4::TypeVar cannot be both covariant and contravariant:INFERENCE
21-
typevar-name-incorrect-variance:69:0:69:4::Type variable name does not reflect variance:INFERENCE
22-
invalid-name:76:0:76:7::"Parameter specification variable name ""badName"" doesn't conform to predefined naming style":HIGH
23-
invalid-name:82:0:82:7::"Parameter specification variable name ""badName"" doesn't conform to predefined naming style":HIGH
24-
invalid-name:96:0:96:7::"Type variable tuple name ""badName"" doesn't conform to predefined naming style":HIGH
1+
typevar-name-incorrect-variance:11:0:11:21::"Type variable name does not reflect variance. ""GoodNameWithoutContra"" is contravariant, use ""GoodNameWithoutContra_contra"" instead":INFERENCE
2+
typevar-double-variance:19:0:19:1::TypeVar cannot be both covariant and contravariant:INFERENCE
3+
typevar-name-incorrect-variance:19:0:19:1::Type variable name does not reflect variance:INFERENCE
4+
typevar-double-variance:23:0:23:4::TypeVar cannot be both covariant and contravariant:INFERENCE
5+
typevar-name-incorrect-variance:23:0:23:4::Type variable name does not reflect variance:INFERENCE
6+
typevar-double-variance:24:0:24:8::TypeVar cannot be both covariant and contravariant:INFERENCE
7+
typevar-name-incorrect-variance:24:0:24:8::Type variable name does not reflect variance:INFERENCE
8+
invalid-name:42:0:42:10::"Type variable name ""CALLABLE_T"" doesn't conform to predefined naming style":HIGH
9+
invalid-name:43:0:43:10::"Type variable name ""DeviceType"" doesn't conform to predefined naming style":HIGH
10+
invalid-name:44:0:44:10::"Type variable name ""IPAddressU"" doesn't conform to predefined naming style":HIGH
11+
invalid-name:47:0:47:7::"Type variable name ""TAnyStr"" doesn't conform to predefined naming style":HIGH
12+
invalid-name:50:0:50:7::"Type variable name ""badName"" doesn't conform to predefined naming style":HIGH
13+
invalid-name:51:0:51:10::"Type variable name ""badName_co"" doesn't conform to predefined naming style":HIGH
14+
invalid-name:52:0:52:14::"Type variable name ""badName_contra"" doesn't conform to predefined naming style":HIGH
15+
invalid-name:56:4:56:13::"Type variable name ""a_BadName"" doesn't conform to predefined naming style":HIGH
16+
invalid-name:57:4:57:26::"Type variable name ""a_BadNameWithoutContra"" doesn't conform to predefined naming style":HIGH
17+
typevar-name-incorrect-variance:57:4:57:26::"Type variable name does not reflect variance. ""a_BadNameWithoutContra"" is contravariant, use ""a_BadNameWithoutContra_contra"" instead":INFERENCE
18+
invalid-name:59:13:59:29::"Type variable name ""a_BadName_contra"" doesn't conform to predefined naming style":HIGH
19+
invalid-name:68:0:68:7::"Type variable name ""badName"" doesn't conform to predefined naming style":HIGH
20+
typevar-double-variance:69:0:69:4::TypeVar cannot be both covariant and contravariant:INFERENCE
21+
typevar-name-incorrect-variance:69:0:69:4::Type variable name does not reflect variance:INFERENCE
22+
invalid-name:76:0:76:7::"Parameter specification variable name ""badName"" doesn't conform to predefined naming style":HIGH
23+
invalid-name:83:0:83:7::"Parameter specification variable name ""badName"" doesn't conform to predefined naming style":HIGH
24+
invalid-name:98:0:98:7::"Type variable tuple name ""badName"" doesn't conform to predefined naming style":HIGH

tests/functional/t/type/typevar_naming_style_default.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@
7474
_P = ParamSpec("_P")
7575
GoodNameP = ParamSpec("GoodNameP")
7676
badName = ParamSpec("badName") # [invalid-name]
77+
Ec2P = ParamSpec("Ec2P")
7778

7879
# -- typing_extensions.ParamSpec --
7980
P = te.ParamSpec("P")
8081
_P = te.ParamSpec("_P")
8182
GoodNameP = te.ParamSpec("GoodNameP")
8283
badName = te.ParamSpec("badName") # [invalid-name]
84+
Ec2P = te.ParamSpec("Ec2P")
8385

8486

8587
# # -- typing.TypeVarTuple (TODO 3.11+) --
@@ -94,3 +96,4 @@
9496
_Ts = te.TypeVarTuple("_Ts")
9597
GoodNameTs = te.TypeVarTuple("GoodNameTs")
9698
badName = te.TypeVarTuple("badName") # >=3.11:[invalid-name]
99+
S3Ts = te.TypeVarTuple("S3Ts")

tests/functional/t/type/typevar_naming_style_default.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ invalid-name:68:0:68:7::"Type variable name ""badName"" doesn't conform to prede
2020
typevar-double-variance:69:0:69:4::TypeVar cannot be both covariant and contravariant:INFERENCE
2121
typevar-name-incorrect-variance:69:0:69:4::Type variable name does not reflect variance:INFERENCE
2222
invalid-name:76:0:76:7::"Parameter specification variable name ""badName"" doesn't conform to predefined naming style":HIGH
23-
invalid-name:82:0:82:7::"Parameter specification variable name ""badName"" doesn't conform to predefined naming style":HIGH
23+
invalid-name:83:0:83:7::"Parameter specification variable name ""badName"" doesn't conform to predefined naming style":HIGH

0 commit comments

Comments
 (0)