Skip to content

Commit 4077dc6

Browse files
authored
stubtest: fix edge case for bytes enum subclasses (python#15943)
1 parent 0b303b5 commit 4077dc6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

mypy/stubtest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,10 +1552,10 @@ def anytype() -> mypy.types.AnyType:
15521552
fallback = mypy.types.Instance(type_info, [anytype() for _ in type_info.type_vars])
15531553

15541554
value: bool | int | str
1555-
if isinstance(runtime, bytes):
1556-
value = bytes_to_human_readable_repr(runtime)
1557-
elif isinstance(runtime, enum.Enum) and isinstance(runtime.name, str):
1555+
if isinstance(runtime, enum.Enum) and isinstance(runtime.name, str):
15581556
value = runtime.name
1557+
elif isinstance(runtime, bytes):
1558+
value = bytes_to_human_readable_repr(runtime)
15591559
elif isinstance(runtime, (bool, int, str)):
15601560
value = runtime
15611561
else:

mypy/test/teststubtest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,26 @@ def spam(x=Flags4(0)): pass
10681068
""",
10691069
error="spam",
10701070
)
1071+
yield Case(
1072+
stub="""
1073+
from typing_extensions import Final, Literal
1074+
class BytesEnum(bytes, enum.Enum):
1075+
a: bytes
1076+
FOO: Literal[BytesEnum.a]
1077+
BAR: Final = BytesEnum.a
1078+
BAZ: BytesEnum
1079+
EGGS: bytes
1080+
""",
1081+
runtime="""
1082+
class BytesEnum(bytes, enum.Enum):
1083+
a = b'foo'
1084+
FOO = BytesEnum.a
1085+
BAR = BytesEnum.a
1086+
BAZ = BytesEnum.a
1087+
EGGS = BytesEnum.a
1088+
""",
1089+
error=None,
1090+
)
10711091

10721092
@collect_cases
10731093
def test_decorator(self) -> Iterator[Case]:

0 commit comments

Comments
 (0)