Skip to content

Commit 5913cb7

Browse files
huwcbjonesPierre-Sassoulas
authored andcommitted
Fix a crash when linting __new__() methods that return a call (#6822)
Only check for Enum attributes when metaclass is an Enum metaclass
1 parent 4621ad5 commit 5913cb7

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

doc/whatsnew/2/2.14/full.rst

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Release date: TBA
1414

1515
Closes #6800
1616

17+
* Fixed a crash when linting ``__new__()`` methods that return a call expression.
18+
19+
Closes #6805
20+
1721
* Don't crash if we can't find the user's home directory.
1822

1923
Closes #6802

pylint/checkers/typecheck.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ def _emit_no_member(
451451
except astroid.MroError:
452452
return False
453453
if metaclass:
454-
if _enum_has_attribute(owner, node):
455-
return False
456454
# Renamed in Python 3.10 to `EnumType`
457-
return metaclass.qname() in {"enum.EnumMeta", "enum.EnumType"}
455+
if metaclass.qname() in {"enum.EnumMeta", "enum.EnumType"}:
456+
return not _enum_has_attribute(owner, node)
457+
return False
458458
return False
459459
if not has_known_bases(owner):
460460
return False
@@ -583,7 +583,7 @@ def _enum_has_attribute(
583583
(c.value for c in dunder_new.get_children() if isinstance(c, nodes.Return)),
584584
None,
585585
)
586-
if returned_obj_name is not None:
586+
if isinstance(returned_obj_name, nodes.Name):
587587
# Find all attribute assignments to the returned object
588588
enum_attributes |= _get_all_attribute_assignments(
589589
dunder_new, returned_obj_name.name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Tests for self-defined Enum members (https://github.com/PyCQA/pylint/issues/6805)"""
2+
# pylint: disable=missing-docstring
3+
# pylint: disable=too-few-public-methods
4+
from enum import IntEnum
5+
6+
7+
class Foo(type):
8+
pass
9+
10+
11+
class Parent:
12+
def __new__(cls, *_args, **_kwargs):
13+
return object.__new__(cls)
14+
15+
16+
class NotEnumHasDynamicGetAttrMetaclass(metaclass=Foo):
17+
def __new__(cls):
18+
return Parent.__new__(cls)
19+
20+
def __getattr__(self, item):
21+
return item
22+
23+
def magic(self):
24+
return self.dynamic
25+
26+
27+
NotEnumHasDynamicGetAttrMetaclass().magic()
28+
29+
30+
class Day(IntEnum):
31+
MONDAY = (1, "Mon")
32+
TUESDAY = (2, "Tue")
33+
WEDNESDAY = (3, "Wed")
34+
THURSDAY = (4, "Thu")
35+
FRIDAY = (5, "Fri")
36+
SATURDAY = (6, "Sat")
37+
SUNDAY = (7, "Sun")
38+
39+
def __new__(cls, value, _abbr=None):
40+
return int.__new__(cls, value)
41+
42+
43+
print(Day.FRIDAY.foo) # [no-member]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no-member:43:6:43:20::Instance of 'FRIDAY' has no 'foo' member:INFERENCE

0 commit comments

Comments
 (0)