Skip to content

Commit

Permalink
Fix a false positive for no-member when accessing the _name_
Browse files Browse the repository at this point in the history
…and ``_value_`` sunders of an Enum member value.

Refs pylint-dev/pylint#7402
  • Loading branch information
mbyrnepr2 committed Sep 11, 2023
1 parent cdb452c commit 2a32f96
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Release date: TBA
* Exclude class attributes from the ``__members__`` container of an ``Enum`` class when they are
``nodes.AnnAssign`` nodes with no assigned value.

* Fix a false positive for ``no-member`` when accessing the ``_name_`` and ``_value_`` sunders of an Enum member value.

Refs pylint-dev/pylint#7402

* Remove ``@cached`` and ``@cachedproperty`` decorator (just use ``@cached_property`` from the stdlib).
Expand Down
6 changes: 6 additions & 0 deletions astroid/brain/brain_namedtuple_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,14 @@ class {name}({types}):
def value(self):
return {return_value}
@property
def _value_(self):
return {return_value}
@property
def name(self):
return "{name}"
@property
def _name_(self):
return "{name}"
""".format(
name=target.name,
types=", ".join(node.basenames),
Expand Down
20 changes: 20 additions & 0 deletions tests/brain/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,23 @@ class MyEnum(enum.Enum):
inferred = next(ast_node.infer())
members_names = [const_node.value for const_node, name_obj in inferred.items]
assert members_names == ["FOO", "BAR", "BAZ"]

def test_enum_sunder_names(self) -> None:
"""Test that both `_name_` and `_value_` sunder names exist"""

sunder_name, sunder_value = builder.extract_node(
"""
import enum
class MyEnum(enum.Enum):
APPLE = 42
MyEnum.APPLE._name_ #@
MyEnum.APPLE._value_ #@
"""
)
inferred_name = next(sunder_name.infer())
assert inferred_name.value == "APPLE"

inferred_value = next(sunder_value.infer())
assert inferred_value.value == 42

0 comments on commit 2a32f96

Please sign in to comment.