Skip to content

Commit 4e11693

Browse files
github-actions[bot]sodulpre-commit-ci[bot]
authored
[Backport maintenance/2.17.x] Allow integers in TypeAlias names. (#8507)
Co-authored-by: Stephane Odul <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d429822 commit 4e11693

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

doc/data/messages/i/invalid-name/details.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ The following type of names are checked with a predefined pattern:
8888
| ``typevar`` | ``T``, ``_CallableT``, ``_T_co``, ``AnyStr``, | ``DICT_T``, ``CALLABLE_T``, ``ENUM_T``, ``DeviceType``, |
8989
| | ``DeviceTypeT``, ``IPAddressT`` | ``_StrType`` |
9090
+--------------------+-------------------------------------------------------+------------------------------------------------------------+
91-
| ``typealias`` | ``GoodName``, ``_GoodName``, ``IPAddressType`` and | ``BadNameT``, ``badName``, ``TBadName``, ``TypeBadName`` |
92-
| | other PascalCase variants that don't start with ``T``| |
93-
| | or ``Type``. This is to distinguish them from | |
94-
| | ``typevars``. Note that ``TopName`` is allowed but | |
95-
| | ``TTopName`` isn't. | |
91+
| ``typealias`` | ``GoodName``, ``_GoodName``, ``IPAddressType``, | ``BadNameT``, ``badName``, ``TBadName``, ``TypeBadName``, |
92+
| | ``GoodName2`` and other PascalCase variants that | ``_1BadName`` |
93+
| | don't start with ``T`` or ``Type``. This is to | |
94+
| | distinguish them from ``typevars``. Note that | |
95+
| | ``TopName`` is allowed but ``TTopName`` isn't. | |
9696
+--------------------+-------------------------------------------------------+------------------------------------------------------------+
9797

9898
Custom regular expressions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``invalid-name`` now allows for integers in ``typealias`` names:
2+
- now valid: ``Good2Name``, ``GoodName2``.
3+
- still invalid: ``_1BadName``.
4+
5+
Closes #8485

pylint/checkers/base/name_checker/checker.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"typevar": re.compile(
4242
r"^_{0,2}(?!T[A-Z])(?:[A-Z]+|(?:[A-Z]+[a-z]+)+T?(?<!Type))(?:_co(?:ntra)?)?$"
4343
),
44-
"typealias": re.compile(r"^_{0,2}(?!T[A-Z]|Type)[A-Z]+[a-z]+(?:[A-Z][a-z]+)*$"),
44+
"typealias": re.compile(
45+
r"^_{0,2}(?!T[A-Z]|Type)[A-Z]+[a-z0-9]+(?:[A-Z][a-z0-9]+)*$"
46+
),
4547
}
4648

4749
BUILTIN_PROPERTY = "builtins.property"

tests/functional/t/typealias_naming_style_default.py

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
GOODName: TypeAlias = int
1010
GOODNAMEType: TypeAlias = int
1111
TodoType: TypeAlias = int
12+
Good2Name: TypeAlias = int
13+
GoodName2: TypeAlias = int
1214

1315
# Non-PascalCase names
1416
BadNAME: TypeAlias = int # [invalid-name]
@@ -20,6 +22,7 @@
2022
BAD_NAME = Union[int, str] # [invalid-name]
2123
_BAD_NAME = Union[int, str] # [invalid-name]
2224
__BAD_NAME = Union[int, str] # [invalid-name]
25+
_1BadName = Union[int, str] # [invalid-name]
2326
ANOTHERBADNAME = Union[int, str] # [invalid-name]
2427

2528
# Regression tests
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
invalid-name:14:0:14:7::"Type alias name ""BadNAME"" doesn't conform to predefined naming style":HIGH
2-
invalid-name:15:0:15:7::"Type alias name ""badName"" doesn't conform to predefined naming style":HIGH
3-
invalid-name:16:0:16:11::"Type alias name ""AlsoBADName"" doesn't conform to predefined naming style":HIGH
4-
invalid-name:17:0:17:8::"Type alias name ""TBadName"" doesn't conform to predefined naming style":HIGH
5-
invalid-name:18:0:18:8::"Type alias name ""TypeTodo"" doesn't conform to predefined naming style":HIGH
6-
invalid-name:19:0:19:8::"Type alias name ""BadNameT"" doesn't conform to predefined naming style":HIGH
7-
invalid-name:20:0:20:8::"Type alias name ""BAD_NAME"" doesn't conform to predefined naming style":HIGH
8-
invalid-name:21:0:21:9::"Type alias name ""_BAD_NAME"" doesn't conform to predefined naming style":HIGH
9-
invalid-name:22:0:22:10::"Type alias name ""__BAD_NAME"" doesn't conform to predefined naming style":HIGH
10-
invalid-name:23:0:23:14::"Type alias name ""ANOTHERBADNAME"" doesn't conform to predefined naming style":HIGH
1+
invalid-name:16:0:16:7::"Type alias name ""BadNAME"" doesn't conform to predefined naming style":HIGH
2+
invalid-name:17:0:17:7::"Type alias name ""badName"" doesn't conform to predefined naming style":HIGH
3+
invalid-name:18:0:18:11::"Type alias name ""AlsoBADName"" doesn't conform to predefined naming style":HIGH
4+
invalid-name:19:0:19:8::"Type alias name ""TBadName"" doesn't conform to predefined naming style":HIGH
5+
invalid-name:20:0:20:8::"Type alias name ""TypeTodo"" doesn't conform to predefined naming style":HIGH
6+
invalid-name:21:0:21:8::"Type alias name ""BadNameT"" doesn't conform to predefined naming style":HIGH
7+
invalid-name:22:0:22:8::"Type alias name ""BAD_NAME"" doesn't conform to predefined naming style":HIGH
8+
invalid-name:23:0:23:9::"Type alias name ""_BAD_NAME"" doesn't conform to predefined naming style":HIGH
9+
invalid-name:24:0:24:10::"Type alias name ""__BAD_NAME"" doesn't conform to predefined naming style":HIGH
10+
invalid-name:25:0:25:9::"Type alias name ""_1BadName"" doesn't conform to predefined naming style":HIGH
11+
invalid-name:26:0:26:14::"Type alias name ""ANOTHERBADNAME"" doesn't conform to predefined naming style":HIGH

0 commit comments

Comments
 (0)