diff --git a/ChangeLog b/ChangeLog index c0f5aa339e..c2821671a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,11 @@ Release date: TBA Refs #2137 +* Exclude class attributes from the ``__members__`` container of an ``Enum`` class when they are + ``nodes.AnnAssign`` nodes with no assigned value. + + Refs pylint-dev/pylint#7402 + * Remove ``@cached`` and ``@cachedproperty`` decorator (just use ``@cached_property`` from the stdlib). Closes #1780 diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index 1fd64fe629..7212f89083 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -462,6 +462,8 @@ def name(self): for method in node.mymethods(): fake.locals[method.name] = [method] new_targets.append(fake.instantiate_class()) + if stmt.value is None: + continue dunder_members[local] = fake node.locals[local] = new_targets diff --git a/tests/brain/test_enum.py b/tests/brain/test_enum.py index 3ca09f2ebf..bbdb812cee 100644 --- a/tests/brain/test_enum.py +++ b/tests/brain/test_enum.py @@ -493,3 +493,31 @@ def pear(self): for node in (attribute_nodes[1], name_nodes[1]): with pytest.raises(InferenceError): node.inferred() + + def test_enum_members_uppercase_only(self) -> None: + """Originally reported in https://github.com/pylint-dev/pylint/issues/7402. + ``nodes.AnnAssign`` nodes with no assigned values do not appear inside ``__members__``. + + Test that only enum members `MARS` and `radius` appear in the `__members__` container while + the attribute `mass` does not. + """ + enum_class = astroid.extract_node( + """ + from enum import Enum + class Planet(Enum): #@ + MARS = (1, 2) + radius: int = 1 + mass: int + + def __init__(self, mass, radius): + self.mass = mass + self.radius = radius + + Planet.MARS.value + """ + ) + enum_members = next(enum_class.igetattr("__members__")) + assert len(enum_members.items) == 2 + mars, radius = enum_members.items + assert mars[1].name == "MARS" + assert radius[1].name == "radius"