Skip to content

Exclude unassigned type-annotated class attributes from enum __members__ container #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions tests/brain/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"