Skip to content

Commit 1cc3be2

Browse files
Check enums created with functional syntax against class name regex (#10661)
1 parent 778b4da commit 1cc3be2

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Check enums created with the ``Enum()`` functional syntax to pass against the
2+
``--class-rgx`` for the :ref:`invalid-name` check, like other enums.
3+
4+
Closes #10660

pylint/checkers/base/name_checker/checker.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from collections.abc import Iterable
1515
from enum import Enum, auto
1616
from re import Pattern
17-
from typing import TYPE_CHECKING
17+
from typing import TYPE_CHECKING, cast
1818

1919
import astroid
2020
from astroid import bases, nodes, util
@@ -484,13 +484,22 @@ def visit_assignname( # pylint: disable=too-many-branches,too-many-statements
484484
)
485485
return
486486

487-
# Check classes (TypeVar's are classes so they need to be excluded first)
488-
elif isinstance(inferred_assign_type, nodes.ClassDef):
489-
self._check_name("class", node.name, node)
490-
491487
elif inferred_assign_type in (None, util.Uninferable):
492488
return
493489

490+
# Check classes (TypeVar's are classes so they need to be excluded first)
491+
elif isinstance(inferred_assign_type, nodes.ClassDef) or (
492+
isinstance(inferred_assign_type, bases.Instance)
493+
and "EnumMeta"
494+
in {
495+
ancestor.name
496+
for ancestor in cast(
497+
InferenceResult, inferred_assign_type
498+
).mro()
499+
}
500+
):
501+
self._check_name("class", node.name, node)
502+
494503
# Don't emit if the name redefines an import in an ImportError except handler
495504
# nor any other reassignment.
496505
elif (

tests/functional/i/invalid/invalid_name.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ def tearDown(self): ... # pylint: disable=invalid-name
117117
class FooBarSubclass(FooBar):
118118
tearDown = FooBar.tearDown
119119
tearDownNotInAncestor = None # [invalid-name]
120+
121+
122+
from enum import Enum
123+
Color = Enum('Color', [('RED', 1), ('GREEN', 2), ('BLUE', 3)])

0 commit comments

Comments
 (0)