Skip to content

Fix a regression in Type[Any] #3012

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 1 commit into from
Mar 16, 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
9 changes: 8 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ def visit_instance(self, left: Instance) -> bool:
item = right.item
if isinstance(item, TupleType):
item = item.fallback
return isinstance(item, Instance) and is_subtype(left, item.type.metaclass_type)
if isinstance(item, Instance):
return is_subtype(left, item.type.metaclass_type)
elif isinstance(item, AnyType):
# Special case: all metaclasses are subtypes of Type[Any]
mro = left.type.mro or []
return any(base.fullname() == 'builtins.type' for base in mro)
else:
return False
else:
return False

Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,20 @@ C1[Type[Any]], C2[type] # both these should not fail
[builtins fixtures/list.pyi]
[out]

[case testTypeEquivalentTypeAnyEdgeCase]
class C:
pass

class M(type):
def __init__(cls, x) -> None:
type.__init__(cls, x)

class Mbad(type):
def __init__(cls, x) -> None:
type.__init__(C(), x) # E: Argument 1 to "__init__" of "type" has incompatible type "C"; expected "type"
[builtins fixtures/primitives.pyi]
[out]

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

Expand Down