8000 Treat type equivalent to Type[Any] by ilevkivskyi · Pull Request #2825 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Treat type equivalent to Type[Any] #2825

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 8 commits into from
Mar 14, 2017
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
11 changes: 7 additions & 4 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 @@ -233,8 +238,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 @@ -275,8 +279,7 @@ def visit_type_type(self, left: TypeType) -> bool:
# 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):
if right.type.fullname() in ('builtins.type', 'builtins.object'):
# Treat builtins.type the same as Type[Any];
if right.type.fullname() == 'builtins.object':
# treat builtins.object the same as Any.
return True
item = left.item
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
37 changes: 37 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2126,6 +2126,43 @@ 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 testTypeEquivalentTypeAny2]
from typing import Type, Any, TypeVar, Generic

class C: ...
x = None # type: type
y = None # type: Type[Any]
z = None # type: Type[C]

lst = [x, y, z]
reveal_type(lst) # E: Revealed type is 'builtins.list[builtins.type*]'

T1 = TypeVar('T1', bound=type)
T2 = TypeVar('T2', bound=Type[Any])
class C1(Generic[T1]): ...
class C2(Generic[T2]): ...

C1[Type[Any]], C2[type] # both these should not fail
[builtins fixtures/list.pyi]
[out]

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

Expand Down
0