Skip to content

Commit bf9cb22

Browse files
elazarggvanrossum
authored andcommitted
Support NamedTuple-derived Enum (#2407)
Fix #1745.
1 parent baa4628 commit bf9cb22

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

mypy/subtypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ def visit_instance(self, left: Instance) -> bool:
120120
if left.type.fallback_to_any:
121121
return True
122122
right = self.right
123+
if isinstance(right, TupleType) and right.fallback.type.is_enum:
124+
return is_subtype(left, right.fallback)
123125
if isinstance(right, Instance):
124126
if left.type._promote and is_subtype(left.type._promote,
125127
self.right,

test-data/unit/pythoneval-enum.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,17 @@ takes_int(SomeExtIntEnum.x)
118118
def takes_some_ext_int_enum(s: SomeExtIntEnum):
119119
pass
120120
takes_some_ext_int_enum(SomeExtIntEnum.x)
121+
122+
123+
[case testNamedTupleEnum]
124+
from typing import NamedTuple
125+
from enum import Enum
126+
127+
N = NamedTuple('N', [('bar', int)])
128+
129+
class E(N, Enum):
130+
X = N(1)
131+
132+
def f(x: E) -> None: pass
133+
134+
f(E.X)

0 commit comments

Comments
 (0)