8000 stubtest: add tests by hauntsaninja · Pull Request #8380 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

stubtest: add tests #8380

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 22 commits into from
Feb 13, 2020
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
stubtest: fix LiteralType misuse for mypyc
EAFP, since bytes and enums should work, and default value error
messages can be more informative with literal types
  • Loading branch information
hauntsaninja 8000 committed Feb 11, 2020
commit 6d1f84fdccd26d1f36491a34f18054ef3f97fc65
21 changes: 13 additions & 8 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,17 +881,22 @@ def anytype() -> mypy.types.AnyType:

if isinstance(runtime, tuple):
# Special case tuples so we construct a valid mypy.types.TupleType
opt_items = [get_mypy_type_of_runtime_value(v) for v in runtime]
items = [(i if i is not None else anytype()) for i in opt_items]
optional_items = [get_mypy_type_of_runtime_value(v) for v in runtime]
items = [(i if i is not None else anytype()) for i in optional_items]
fallback = mypy.types.Instance(type_info, [anytype()])
return mypy.types.TupleType(items, fallback)

# Technically, Literals are supposed to be only bool, int, str or bytes, but this
# seems to work fine
return mypy.types.LiteralType(
value=runtime,
fallback=mypy.types.Instance(type_info, [anytype() for _ in type_info.type_vars]),
)
fallback = mypy.types.Instance(type_info, [anytype() for _ in type_info.type_vars])
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).
return mypy.types.LiteralType(
value=runtime,
fallback=fallback,
)
except TypeError:
# Ask for forgiveness if we're using mypyc.
return fallback


_all_stubs = {} # type: Dict[str, nodes.MypyFile]
Expand Down
0