Skip to content

Allow type aliases in runtime context #8779

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 2 commits into from
May 5, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3125,9 +3125,8 @@ class LongName(Generic[T]): ...
else:
if alias_definition:
return AnyType(TypeOfAny.special_form)
# This type is invalid in most runtime contexts.
self.msg.alias_invalid_in_runtime_context(item, ctx)
return AnyType(TypeOfAny.from_error)
# This type is invalid in most runtime contexts, give it an 'object' type.
return self.named_type('builtins.object')

def apply_type_arguments_to_callable(self, tp: Type, args: List[Type], ctx: Context) -> Type:
"""Apply type arguments to a generic callable type coming from a type object.
Expand Down
7 changes: 5 additions & 2 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,11 @@ def object_or_any_from_type(typ: ProperType) -> ProperType:
elif isinstance(typ, TypeVarType) and isinstance(typ.upper_bound, ProperType):
return object_or_any_from_type(typ.upper_bound)
elif isinstance(typ, UnionType):
joined = join_type_list([it for it in typ.items if isinstance(it, ProperType)])
return object_or_any_from_type(joined)
for item in typ.items:
if isinstance(item, ProperType):
candidate = object_or_any_from_type(item)
if isinstance(candidate, Instance):
return candidate
return AnyType(TypeOfAny.implementation_artifact)


Expand Down
8 changes: 0 additions & 8 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,6 @@ def incompatible_type_application(self, expected_arg_count: int,
self.fail('Type application has too few types ({} expected)'
.format(expected_arg_count), context)

def alias_invalid_in_runtime_context(self, item: ProperType, ctx: Context) -> None:
kind = (' to Callable' if isinstance(item, CallableType) else
' to Tuple' if isinstance(item, TupleType) else
' to Union' if isinstance(item, UnionType) else
' to Literal' if isinstance(item, LiteralType) else
'')
self.fail('The type alias{} is invalid in runtime context'.format(kind), ctx)

def could_not_infer_type_arguments(self, callee_type: CallableType, n: int,
context: Context) -> None:
callee_name = callable_name(callee_type)
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -1025,13 +1025,13 @@ CA = Callable[[T], int]
TA = Tuple[T, int]
UA = Union[T, int]

cs = CA + 1 # E: The type alias to Callable is invalid in runtime context
cs = CA + 1 # E: Unsupported left operand type for + ("object")
reveal_type(cs) # N: Revealed type is 'Any'

ts = TA() # E: The type alias to Tuple is invalid in runtime context
ts = TA() # E: "object" not callable
reveal_type(ts) # N: Revealed type is 'Any'

us = UA.x # E: The type alias to Union is invalid in runtime context
us = UA.x # E: "object" has no attribute "x"
reveal_type(us) # N: Revealed type is 'Any'

xx = CA[str] + 1 # E: Type application is only supported for generic classes
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -1815,13 +1815,13 @@ Alias = Literal[3]

isinstance(3, Literal[3]) # E: Cannot use isinstance() with Literal type
isinstance(3, Alias) # E: Cannot use isinstance() with Literal type \
# E: The type alias to Literal is invalid in runtime context
# E: Argument 2 to "isinstance" has incompatible type "object"; expected "Union[type, Tuple[Any, ...]]"
isinstance(3, Renamed[3]) # E: Cannot use isinstance() with Literal type
isinstance(3, indirect.Literal[3]) # E: Cannot use isinstance() with Literal type

issubclass(int, Literal[3]) # E: Cannot use issubclass() with Literal type
issubclass(int, Alias) # E: Cannot use issubclass() with Literal type \
# E: The type alias to Literal is invalid in runtime context
# E: Argument 2 to "issubclass" has incompatible type "object"; expected "Union[type, Tuple[Any, ...]]"
issubclass(int, Renamed[3]) # E: Cannot use issubclass() with Literal type
issubclass(int, indirect.Literal[3]) # E: Cannot use issubclass() with Literal type
[builtins fixtures/isinstancelist.pyi]
Expand Down Expand Up @@ -1855,7 +1855,7 @@ Alias = Literal[3]
Literal[3]() # E: The type "Type[Literal]" is not generic and not indexable
Renamed[3]() # E: The type "Type[Literal]" is not generic and not indexable
indirect.Literal[3]() # E: The type "Type[Literal]" is not generic and not indexable
Alias() # E: The type alias to Literal is invalid in runtime context
Alias() # E: "object" not callable

# TODO: Add appropriate error messages to the following lines
Literal()
Expand Down