Skip to content

Commit 54e296b

Browse files
committed
Restrict ellipsis replacement to stubs
1 parent b333645 commit 54e296b

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

mypy/plugins/enums.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ def _infer_value_type_with_auto_fallback(
9191
# Enums in stubs may have ... instead of actual values. If `_value_` is annotated
9292
# (manually or inherited from IntEnum, for example), it is a more reasonable guess
9393
# than literal ellipsis type.
94-
if isinstance(proper_type, Instance) and proper_type.type.fullname in ELLIPSIS_TYPE_NAMES:
94+
source_module = ctx.api.modules[ctx.type.type.fullname.rsplit(".", 1)[0]]
95+
if (
96+
source_module.is_stub
97+
and isinstance(proper_type, Instance)
98+
and proper_type.type.fullname in ELLIPSIS_TYPE_NAMES
99+
):
95100
if (
96101
isinstance(ctx.type, Instance)
97102
and (value_type := ctx.type.type.get("_value_"))

test-data/unit/check-enum.test

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,50 +2534,86 @@ def check(thing: Things) -> None:
25342534
return None # E: Statement is unreachable
25352535
[builtins fixtures/enum.pyi]
25362536

2537-
[case testSunderValueType]
2537+
[case testSunderValueTypeEllipsis]
2538+
from foo.bar import (
2539+
Basic, FromStub, InheritedInt, InheritedStr, InheritedFlag, InheritedIntFlag
2540+
)
2541+
2542+
reveal_type(Basic.FOO) # N: Revealed type is "Literal[foo.bar.Basic.FOO]?"
2543+
reveal_type(Basic.FOO.value) # N: Revealed type is "Literal[1]?"
2544+
reveal_type(Basic.FOO._value_) # N: Revealed type is "builtins.int"
2545+
2546+
reveal_type(FromStub.FOO) # N: Revealed type is "Literal[foo.bar.FromStub.FOO]?"
2547+
reveal_type(FromStub.FOO.value) # N: Revealed type is "builtins.int"
2548+
reveal_type(FromStub.FOO._value_) # N: Revealed type is "builtins.int"
2549+
2550+
reveal_type(InheritedInt.FOO) # N: Revealed type is "Literal[foo.bar.InheritedInt.FOO]?"
2551+
reveal_type(InheritedInt.FOO.value) # N: Revealed type is "builtins.int"
2552+
reveal_type(InheritedInt.FOO._value_) # N: Revealed type is "builtins.int"
2553+
2554+
reveal_type(InheritedStr.FOO) # N: Revealed type is "Literal[foo.bar.InheritedStr.FOO]?"
2555+
reveal_type(InheritedStr.FOO.value) # N: Revealed type is "builtins.str"
2556+
reveal_type(InheritedStr.FOO._value_) # N: Revealed type is "builtins.str"
2557+
2558+
reveal_type(InheritedFlag.FOO) # N: Revealed type is "Literal[foo.bar.InheritedFlag.FOO]?"
2559+
reveal_type(InheritedFlag.FOO.value) # N: Revealed type is "builtins.int"
2560+
reveal_type(InheritedFlag.FOO._value_) # N: Revealed type is "builtins.int"
2561+
2562+
reveal_type(InheritedIntFlag.FOO) # N: Revealed type is "Literal[foo.bar.InheritedIntFlag.FOO]?"
2563+
reveal_type(InheritedIntFlag.FOO.value) # N: Revealed type is "builtins.int"
2564+
reveal_type(InheritedIntFlag.FOO._value_) # N: Revealed type is "builtins.int"
2565+
2566+
[file foo/__init__.pyi]
2567+
[file foo/bar/__init__.pyi]
25382568
from enum import Enum, IntEnum, StrEnum, Flag, IntFlag
25392569

25402570
class Basic(Enum):
25412571
_value_: int
25422572
FOO = 1
25432573

2544-
reveal_type(Basic.FOO) # N: Revealed type is "Literal[__main__.Basic.FOO]?"
2545-
reveal_type(Basic.FOO.value) # N: Revealed type is "Literal[1]?"
2546-
reveal_type(Basic.FOO._value_) # N: Revealed type is "builtins.int"
2547-
25482574
class FromStub(Enum):
25492575
_value_: int
25502576
FOO = ...
25512577

2552-
reveal_type(FromStub.FOO) # N: Revealed type is "Literal[__main__.FromStub.FOO]?"
2553-
reveal_type(FromStub.FOO.value) # N: Revealed type is "builtins.int"
2554-
reveal_type(FromStub.FOO._value_) # N: Revealed type is "builtins.int"
2555-
25562578
class InheritedInt(IntEnum):
25572579
FOO = ...
25582580

2559-
reveal_type(InheritedInt.FOO) # N: Revealed type is "Literal[__main__.InheritedInt.FOO]?"
2560-
reveal_type(InheritedInt.FOO.value) # N: Revealed type is "builtins.int"
2561-
reveal_type(InheritedInt.FOO._value_) # N: Revealed type is "builtins.int"
2562-
25632581
class InheritedStr(StrEnum):
25642582
FOO = ...
25652583

2566-
reveal_type(InheritedStr.FOO) # N: Revealed type is "Literal[__main__.InheritedStr.FOO]?"
2567-
reveal_type(InheritedStr.FOO.value) # N: Revealed type is "builtins.str"
2568-
reveal_type(InheritedStr.FOO._value_) # N: Revealed type is "builtins.str"
2569-
25702584
class InheritedFlag(Flag):
25712585
FOO = ...
25722586

2573-
reveal_type(InheritedFlag.FOO) # N: Revealed type is "Literal[__main__.InheritedFlag.FOO]?"
2574-
reveal_type(InheritedFlag.FOO.value) # N: Revealed type is "builtins.int"
2575-
reveal_type(InheritedFlag.FOO._value_) # N: Revealed type is "builtins.int"
2576-
25772587
class InheritedIntFlag(IntFlag):
25782588
FOO = ...
2589+
[builtins fixtures/enum.pyi]
25792590

2580-
reveal_type(InheritedIntFlag.FOO) # N: Revealed type is "Literal[__main__.InheritedIntFlag.FOO]?"
2581-
reveal_type(InheritedIntFlag.FOO.value) # N: Revealed type is "builtins.int"
2582-
reveal_type(InheritedIntFlag.FOO._value_) # N: Revealed type is "builtins.int"
2591+
[case testSunderValueTypeEllipsisNonStub]
2592+
from enum import Enum, StrEnum
2593+
2594+
class Basic(Enum):
2595+
_value_: int
2596+
FOO = 1
2597+
2598+
reveal_type(Basic.FOO) # N: Revealed type is "Literal[__main__.Basic.FOO]?"
2599+
reveal_type(Basic.FOO.value) # N: Revealed type is "Literal[1]?"
2600+
reveal_type(Basic.FOO._value_) # N: Revealed type is "builtins.int"
2601+
2602+
# TODO: this and below should produce diagnostics, Ellipsis is not assignable to int
2603+
# Now we do not check members against _value_ at all.
2604+
2605+
class FromStub(Enum):
2606+
_value_: int
2607+
FOO = ...
2608+
2609+
reveal_type(FromStub.FOO) # N: Revealed type is "Literal[__main__.FromStub.FOO]?"
2610+
reveal_type(FromStub.FOO.value) # N: Revealed type is "builtins.ellipsis"
2611+
reveal_type(FromStub.FOO._value_) # N: Revealed type is "builtins.int"
2612+
2613+
class InheritedStr(StrEnum):
2614+
FOO = ...
2615+
2616+
reveal_type(InheritedStr.FOO) # N: Revealed type is "Literal[__main__.InheritedStr.FOO]?"
2617+
reveal_type(InheritedStr.FOO.value) # N: Revealed type is "builtins.ellipsis"
2618+
reveal_type(InheritedStr.FOO._value_) # N: Revealed type is "builtins.ellipsis"
25832619
[builtins fixtures/enum.pyi]

0 commit comments

Comments
 (0)