Skip to content

Commit c1bff2f

Browse files
committed
Consider non-empty enums assignable to Self
1 parent b8ee1f5 commit c1bff2f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

mypy/typeanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
779779
if self.api.type.has_base("builtins.type"):
780780
self.fail("Self type cannot be used in a metaclass", t)
781781
if self.api.type.self_type is not None:
782-
if self.api.type.is_final:
782+
if self.api.type.is_final or self.api.type.is_enum and self.api.type.enum_members:
783783
return fill_typevars(self.api.type)
784784
return self.api.type.self_type.copy_modified(line=t.line, column=t.column)
785785
# TODO: verify this is unreachable and replace with an assert?

test-data/unit/check-selftype.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,3 +2346,15 @@ gc: G[D2]
23462346
reveal_type(gb.test()) # N: Revealed type is "typing.Sequence[__main__.D1]"
23472347
reveal_type(gc.test()) # N: Revealed type is "builtins.list[__main__.D2]"
23482348
[builtins fixtures/list.pyi]
2349+
2350+
[case testEnumImplicitlyFinalForSelfType]
2351+
from enum import Enum
2352+
from typing import Self
2353+
2354+
class Foo(Enum):
2355+
A = 1
2356+
2357+
@classmethod
2358+
def foo(self) -> Self:
2359+
return Foo.A
2360+
[builtins fixtures/classmethod.pyi]

0 commit comments

Comments
 (0)