Skip to content

Commit 63cd8a1

Browse files
mbyrnepr2pre-commit-ci[bot]jacobtylerwallsdependabot[bot]
authored
Exclude unassigned type-annotated class attributes from enum __members__ container (#2263)
* Exclude type-annotated class attributes, which have no assigned value, from the ``__members__`` container of an ``Enum`` class. Refs pylint-dev/pylint#7402 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix exising test. The value if now `Uninferable` in the case of an annotated attribute of an `enum.Enum` class with no assigned value. * Update astroid/brain/brain_namedtuple_enum.py Co-authored-by: Jacob Walls <[email protected]> * Update tests. * Update test: Use `infer()` instead of `inferred()`. * Update type annotations of PEP 695 nodes (#2264) These attributes cannot be none in real-world situations, see python/cpython#106145. * Update sphinx requirement from ~=7.0 to ~=7.1 (#2265) Updates the requirements on [sphinx](https://github.com/sphinx-doc/sphinx) to permit the latest version. - [Release notes](https://github.com/sphinx-doc/sphinx/releases) - [Changelog](https://github.com/sphinx-doc/sphinx/blob/master/CHANGES) - [Commits](sphinx-doc/sphinx@v7.0.0...v7.1.1) --- updated-dependencies: - dependency-name: sphinx dependency-type: direct:production ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Ensure a node is inferred in the case when there is only one member. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Undo unintended changes. --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jacob Walls <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 38fc4aa commit 63cd8a1

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ Release date: TBA
1919

2020
Refs #2137
2121

22+
* Exclude class attributes from the ``__members__`` container of an ``Enum`` class when they are
23+
``nodes.AnnAssign`` nodes with no assigned value.
24+
25+
Refs pylint-dev/pylint#7402
26+
2227
* Remove ``@cached`` and ``@cachedproperty`` decorator (just use ``@cached_property`` from the stdlib).
2328

2429
Closes #1780

astroid/brain/brain_namedtuple_enum.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ def name(self):
462462
for method in node.mymethods():
463463
fake.locals[method.name] = [method]
464464
new_targets.append(fake.instantiate_class())
465+
if stmt.value is None:
466+
continue
465467
dunder_members[local] = fake
466468
node.locals[local] = new_targets
467469

tests/brain/test_enum.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,31 @@ def pear(self):
493493
for node in (attribute_nodes[1], name_nodes[1]):
494494
with pytest.raises(InferenceError):
495495
node.inferred()
496+
497+
def test_enum_members_uppercase_only(self) -> None:
498+
"""Originally reported in https://github.com/pylint-dev/pylint/issues/7402.
499+
``nodes.AnnAssign`` nodes with no assigned values do not appear inside ``__members__``.
500+
501+
Test that only enum members `MARS` and `radius` appear in the `__members__` container while
502+
the attribute `mass` does not.
503+
"""
504+
enum_class = astroid.extract_node(
505+
"""
506+
from enum import Enum
507+
class Planet(Enum): #@
508+
MARS = (1, 2)
509+
radius: int = 1
510+
mass: int
511+
512+
def __init__(self, mass, radius):
513+
self.mass = mass
514+
self.radius = radius
515+
516+
Planet.MARS.value
517+
"""
518+
)
519+
enum_members = next(enum_class.igetattr("__members__"))
520+
assert len(enum_members.items) == 2
521+
mars, radius = enum_members.items
522+
assert mars[1].name == "MARS"
523+
assert radius[1].name == "radius"

0 commit comments

Comments
 (0)