Skip to content
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

typecheck/Enum: fix crash in Enum attributes check #6822

Merged
merged 3 commits into from
Jun 6, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Release date: TBA

Closes #6800

* Fixed a crash when linting ``__new__()`` methods that return a call expression.

Closes #6805

* Don't crash if we can't find the user's home directory.

Closes #6802
Expand Down
8 changes: 4 additions & 4 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,10 @@ def _emit_no_member(
except astroid.MroError:
return False
if metaclass:
if _enum_has_attribute(owner, node):
return False
# Renamed in Python 3.10 to `EnumType`
return metaclass.qname() in {"enum.EnumMeta", "enum.EnumType"}
if metaclass.qname() in {"enum.EnumMeta", "enum.EnumType"}:
return not _enum_has_attribute(owner, node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outside the scope of this PR, since status quo, but this looks fragile against importing enum under an alias, which @DanielNoord was just looking into at pylint-dev/astroid#1587. Probably worth a follow-up issue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should probably revisit this when we bump to 2.12.

return False
return False
if not has_known_bases(owner):
return False
Expand Down Expand Up @@ -583,7 +583,7 @@ def _enum_has_attribute(
(c.value for c in dunder_new.get_children() if isinstance(c, nodes.Return)),
None,
)
if returned_obj_name is not None:
if isinstance(returned_obj_name, nodes.Name):
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
# Find all attribute assignments to the returned object
enum_attributes |= _get_all_attribute_assignments(
dunder_new, returned_obj_name.name
Expand Down
43 changes: 43 additions & 0 deletions tests/functional/e/enum_self_defined_member_6805.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Tests for self-defined Enum members (https://github.com/PyCQA/pylint/issues/6805)"""
# pylint: disable=missing-docstring
# pylint: disable=too-few-public-methods
from enum import IntEnum


class Foo(type):
pass


class Parent:
def __new__(cls, *_args, **_kwargs):
return object.__new__(cls)


class NotEnumHasDynamicGetAttrMetaclass(metaclass=Foo):
def __new__(cls):
return Parent.__new__(cls)

def __getattr__(self, item):
return item

def magic(self):
return self.dynamic


NotEnumHasDynamicGetAttrMetaclass().magic()


class Day(IntEnum):
MONDAY = (1, "Mon")
TUESDAY = (2, "Tue")
WEDNESDAY = (3, "Wed")
THURSDAY = (4, "Thu")
FRIDAY = (5, "Fri")
SATURDAY = (6, "Sat")
SUNDAY = (7, "Sun")

def __new__(cls, value, _abbr=None):
return int.__new__(cls, value)


print(Day.FRIDAY.foo) # [no-member]
1 change: 1 addition & 0 deletions tests/functional/e/enum_self_defined_member_6805.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no-member:43:6:43:20::Instance of 'FRIDAY' has no 'foo' member:INFERENCE