Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 7 additions & 5 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def is_subtype(left: Type, right: Type,
return any(is_subtype(left, item, type_parameter_checker,
ignore_pos_arg_names=ignore_pos_arg_names)
for item in right.items)
# Treat builtins.type the same as Type[Any]
elif is_named_instance(left, 'builtins.type'):
return is_subtype(TypeType(AnyType()), right)
elif is_named_instance(right, 'builtins.type'):
return is_subtype(left, TypeType(AnyType()))
else:
return left.accept(SubtypeVisitor(right, type_parameter_checker,
ignore_pos_arg_names=ignore_pos_arg_names))
Expand Down Expand Up @@ -228,8 +233,7 @@ def visit_overloaded(self, left: Overloaded) -> bool:
right = self.right
if isinstance(right, Instance):
return is_subtype(left.fallback, right)
elif isinstance(right, CallableType) or is_named_instance(
right, 'builtins.type'):
elif isinstance(right, CallableType):
for item in left.items():
if is_subtype(item, right, self.check_type_parameter,
ignore_pos_arg_names=self.ignore_pos_arg_names):
Expand Down Expand Up @@ -269,9 +273,7 @@ def visit_type_type(self, left: TypeType) -> bool:
if isinstance(right, CallableType):
# This is unsound, we don't check the __init__ signature.
return right.is_type_obj() and is_subtype(left.item, right.ret_type)
if (isinstance(right, Instance) and
right.type.fullname() in ('builtins.type', 'builtins.object')):
# Treat builtins.type the same as Type[Any];
if is_named_instance(right, 'builtins.object'):
# treat builtins.object the same as Any.
return True
return False
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testsubtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_var_arg_callable_subtyping_9(self) -> None:
self.fx.callable_var_arg(0, self.fx.b, self.fx.d))

def test_type_callable_subtyping(self) -> None:
self.assert_strict_subtype(
self.assert_subtype(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why this change? It doesn't seem right to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, nevermind -- I see the preexisting comment about it in visit_type_type.

self.fx.callable_type(self.fx.d, self.fx.a), self.fx.type_type)

self.assert_strict_subtype(
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,23 @@ def foo(c: Type[C], d: Type[D]) -> None:
[out]
main:7: error: Revealed type is 'builtins.list[Type[__main__.B]]'

[case testTypeEquivalentTypeAny]
from typing import Type, Any

a = None # type: Type[Any]
b = a # type: type

x = None # type: type
y = x # type: Type[Any]

class C: ...

p = None # type: type
q = p # type: Type[C]

[builtins fixtures/list.pyi]
[out]

[case testTypeMatchesOverloadedFunctions]
from typing import Type, overload, Union

Expand Down