Skip to content

Commit

Permalink
Exclude unassigned type-annotated class attributes from enum __member…
Browse files Browse the repository at this point in the history
…s__ 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 <jacobtylerwalls@gmail.com>

* 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] <support@github.com>
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] <support@github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Aug 5, 2023
1 parent 38fc4aa commit 63cd8a1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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"

0 comments on commit 63cd8a1

Please sign in to comment.