Skip to content

Commit a1463ca

Browse files
Check enums created with functional syntax against class name regex
1 parent 54d83b4 commit a1463ca

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-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: 16 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,24 @@ 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(
492+
cast(InferenceResult, inferred_assign_type), nodes.ClassDef
493+
) or (
494+
isinstance(inferred_assign_type, bases.Instance)
495+
and "EnumMeta"
496+
in {
497+
ancestor.name
498+
for ancestor in cast(
499+
InferenceResult, inferred_assign_type
500+
).mro()
501+
}
502+
):
503+
self._check_name("class", node.name, node)
504+
494505
# Don't emit if the name redefines an import in an ImportError except handler
495506
# nor any other reassignment.
496507
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)