8000 stubtest: fix literal type construction by sobolevn · Pull Request #11931 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

stubtest: fix literal type construction #11931

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

Merged
merged 7 commits into from
Jan 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Lets try another solution
  • Loading branch information
sobolevn committed Jan 7, 2022
commit a1a4c96df5540648e1793932293775f31c59996c
11 changes: 2 additions & 9 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,15 +942,6 @@ def is_subtype_helper(left: mypy.types.Type, right: mypy.types.Type) -> bool:
):
# Pretend Literal[0, 1] is a subtype of bool to avoid unhelpful errors.
Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, I am almost sure that it does not work anymore, because bool() now is Union[Literal[False], Literal[True]].

I can address this in a new PR.

return True
if (
isinstance(left, mypy.types.LiteralType)
and isinstance(left.value, bytes)
and isinstance(right, mypy.types.LiteralType)
and isinstance(right.value, str)
and right.fallback.type.fullname == 'builtins.bytes'
):
# This is a representation mismatch.
return bytes_to_human_readable_repr(left.value) == right.value
with mypy.state.strict_optional_set(True):
return mypy.subtypes.is_subtype(left, right)

Expand Down Expand Up @@ -1038,6 +1029,8 @@ def anytype() -> mypy.types.AnyType:
return mypy.types.TupleType(items, fallback)

fallback = mypy.types.Instance(type_info, [anytype() for _ in type_info.type_vars])
if isinstance(runtime, bytes):
runtime = bytes_to_human_readable_repr(runtime)
try:
# Literals are supposed to be only bool, int, str, bytes or enums, but this seems to work
# well (when not using mypyc, for which bytes and enums are also problematic).
Expand Down
0