Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stubtest: fix another enum-related edge case #15943

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1551,10 +1551,10 @@ def anytype() -> mypy.types.AnyType:
fallback = mypy.types.Instance(type_info, [anytype() for _ in type_info.type_vars])

value: bool | int | str
if isinstance(runtime, bytes):
value = bytes_to_human_readable_repr(runtime)
elif isinstance(runtime, enum.Enum) and isinstance(runtime.name, str):
if isinstance(runtime, enum.Enum) and isinstance(runtime.name, str):
value = runtime.name
elif isinstance(runtime, bytes):
value = bytes_to_human_readable_repr(runtime)
elif isinstance(runtime, (bool, int, str)):
value = runtime
else:
Expand Down
20 changes: 20 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,26 @@ def spam(x=Flags4(0)): pass
""",
error="spam",
)
yield Case(
stub="""
from typing_extensions import Final, Literal
class BytesEnum(bytes, enum.Enum):
a: bytes
FOO: Literal[BytesEnum.a]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's somewhat unlikely to come up in practice, but stubtest emits a false positive on this line using mypy master

BAR: Final = BytesEnum.a
BAZ: BytesEnum
EGGS: bytes
""",
runtime="""
class BytesEnum(bytes, enum.Enum):
a = b'foo'
FOO = BytesEnum.a
BAR = BytesEnum.a
BAZ = BytesEnum.a
EGGS = BytesEnum.a
""",
error=None,
)

@collect_cases
def test_decorator(self) -> Iterator[Case]:
Expand Down