Skip to content

Commit

Permalink
[Backport maintenance/2.17.x] Allow integers in TypeAlias names. (#8507)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephane Odul <1504511+sodul@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 28, 2023
1 parent d429822 commit 4e11693
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
10 changes: 5 additions & 5 deletions doc/data/messages/i/invalid-name/details.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ The following type of names are checked with a predefined pattern:
| ``typevar`` | ``T``, ``_CallableT``, ``_T_co``, ``AnyStr``, | ``DICT_T``, ``CALLABLE_T``, ``ENUM_T``, ``DeviceType``, |
| | ``DeviceTypeT``, ``IPAddressT`` | ``_StrType`` |
+--------------------+-------------------------------------------------------+------------------------------------------------------------+
| ``typealias`` | ``GoodName``, ``_GoodName``, ``IPAddressType`` and | ``BadNameT``, ``badName``, ``TBadName``, ``TypeBadName`` |
| | other PascalCase variants that don't start with ``T``| |
| | or ``Type``. This is to distinguish them from | |
| | ``typevars``. Note that ``TopName`` is allowed but | |
| | ``TTopName`` isn't. | |
| ``typealias`` | ``GoodName``, ``_GoodName``, ``IPAddressType``, | ``BadNameT``, ``badName``, ``TBadName``, ``TypeBadName``, |
| | ``GoodName2`` and other PascalCase variants that | ``_1BadName`` |
| | don't start with ``T`` or ``Type``. This is to | |
| | distinguish them from ``typevars``. Note that | |
| | ``TopName`` is allowed but ``TTopName`` isn't. | |
+--------------------+-------------------------------------------------------+------------------------------------------------------------+

Custom regular expressions
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/fragments/8485.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``invalid-name`` now allows for integers in ``typealias`` names:
- now valid: ``Good2Name``, ``GoodName2``.
- still invalid: ``_1BadName``.

Closes #8485
4 changes: 3 additions & 1 deletion pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"typevar": re.compile(
r"^_{0,2}(?!T[A-Z])(?:[A-Z]+|(?:[A-Z]+[a-z]+)+T?(?<!Type))(?:_co(?:ntra)?)?$"
),
"typealias": re.compile(r"^_{0,2}(?!T[A-Z]|Type)[A-Z]+[a-z]+(?:[A-Z][a-z]+)*$"),
"typealias": re.compile(
r"^_{0,2}(?!T[A-Z]|Type)[A-Z]+[a-z0-9]+(?:[A-Z][a-z0-9]+)*$"
),
}

BUILTIN_PROPERTY = "builtins.property"
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/t/typealias_naming_style_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
GOODName: TypeAlias = int
GOODNAMEType: TypeAlias = int
TodoType: TypeAlias = int
Good2Name: TypeAlias = int
GoodName2: TypeAlias = int

# Non-PascalCase names
BadNAME: TypeAlias = int # [invalid-name]
Expand All @@ -20,6 +22,7 @@
BAD_NAME = Union[int, str] # [invalid-name]
_BAD_NAME = Union[int, str] # [invalid-name]
__BAD_NAME = Union[int, str] # [invalid-name]
_1BadName = Union[int, str] # [invalid-name]
ANOTHERBADNAME = Union[int, str] # [invalid-name]

# Regression tests
Expand Down
21 changes: 11 additions & 10 deletions tests/functional/t/typealias_naming_style_default.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
invalid-name:14:0:14:7::"Type alias name ""BadNAME"" doesn't conform to predefined naming style":HIGH
invalid-name:15:0:15:7::"Type alias name ""badName"" doesn't conform to predefined naming style":HIGH
invalid-name:16:0:16:11::"Type alias name ""AlsoBADName"" doesn't conform to predefined naming style":HIGH
invalid-name:17:0:17:8::"Type alias name ""TBadName"" doesn't conform to predefined naming style":HIGH
invalid-name:18:0:18:8::"Type alias name ""TypeTodo"" doesn't conform to predefined naming style":HIGH
invalid-name:19:0:19:8::"Type alias name ""BadNameT"" doesn't conform to predefined naming style":HIGH
invalid-name:20:0:20:8::"Type alias name ""BAD_NAME"" doesn't conform to predefined naming style":HIGH
invalid-name:21:0:21:9::"Type alias name ""_BAD_NAME"" doesn't conform to predefined naming style":HIGH
invalid-name:22:0:22:10::"Type alias name ""__BAD_NAME"" doesn't conform to predefined naming style":HIGH
invalid-name:23:0:23:14::"Type alias name ""ANOTHERBADNAME"" doesn't conform to predefined naming style":HIGH
invalid-name:16:0:16:7::"Type alias name ""BadNAME"" doesn't conform to predefined naming style":HIGH
invalid-name:17:0:17:7::"Type alias name ""badName"" doesn't conform to predefined naming style":HIGH
invalid-name:18:0:18:11::"Type alias name ""AlsoBADName"" doesn't conform to predefined naming style":HIGH
invalid-name:19:0:19:8::"Type alias name ""TBadName"" doesn't conform to predefined naming style":HIGH
invalid-name:20:0:20:8::"Type alias name ""TypeTodo"" doesn't conform to predefined naming style":HIGH
invalid-name:21:0:21:8::"Type alias name ""BadNameT"" doesn't conform to predefined naming style":HIGH
invalid-name:22:0:22:8::"Type alias name ""BAD_NAME"" doesn't conform to predefined naming style":HIGH
invalid-name:23:0:23:9::"Type alias name ""_BAD_NAME"" doesn't conform to predefined naming style":HIGH
invalid-name:24:0:24:10::"Type alias name ""__BAD_NAME"" doesn't conform to predefined naming style":HIGH
invalid-name:25:0:25:9::"Type alias name ""_1BadName"" doesn't conform to predefined naming style":HIGH
invalid-name:26:0:26:14::"Type alias name ""ANOTHERBADNAME"" doesn't conform to predefined naming style":HIGH

0 comments on commit 4e11693

Please sign in to comment.