Skip to content

Commit 3471156

Browse files
committed
Stubtest: check _value_ for ellipsis-valued stub enum members
1 parent 6a97dc6 commit 3471156

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

mypy/stubtest.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,7 @@ def verify_var(
12551255
yield Error(object_path, "is read-only at runtime but not in the stub", stub, runtime)
12561256

12571257
runtime_type = get_mypy_type_of_runtime_value(runtime, type_context=stub.type)
1258+
note = ""
12581259
if (
12591260
runtime_type is not None
12601261
and stub.type is not None
@@ -1273,11 +1274,20 @@ def verify_var(
12731274
isinstance(proper_type, mypy.types.Instance)
12741275
and proper_type.type.fullname in mypy.types.ELLIPSIS_TYPE_NAMES
12751276
):
1276-
should_error = False
1277+
value_t = stub.info.get("_value_")
1278+
if value_t is None or value_t.type is None:
1279+
should_error = False
1280+
elif runtime_type is not None and is_subtype_helper(runtime_type, value_t.type):
1281+
should_error = False
1282+
elif runtime_type is not None:
1283+
note = " (incompatible '_value_')"
12771284

12781285
if should_error:
12791286
yield Error(
1280-
object_path, f"variable differs from runtime type {runtime_type}", stub, runtime
1287+
object_path,
1288+
f"variable differs from runtime type {runtime_type}{note}",
1289+
stub,
1290+
runtime,
12811291
)
12821292

12831293

mypy/test/teststubtest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,30 @@ class HasEmptySlots:
14741474
""",
14751475
error=None,
14761476
)
1477+
yield Case(
1478+
stub="""
1479+
class HasCompatibleValue(enum.Enum):
1480+
_value_: str
1481+
FOO = ...
1482+
""",
1483+
runtime="""
1484+
class HasCompatibleValue(enum.Enum):
1485+
FOO = "foo"
1486+
""",
1487+
error=None,
1488+
)
1489+
yield Case(
1490+
stub="""
1491+
class HasIncompatibleValue(enum.Enum):
1492+
_value_: int
1493+
FOO = ...
1494+
""",
1495+
runtime="""
1496+
class HasIncompatibleValue(enum.Enum):
1497+
FOO = "foo"
1498+
""",
1499+
error="HasIncompatibleValue.FOO",
1500+
)
14771501

14781502
@collect_cases
14791503
def test_decorator(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)