Skip to content
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

Erase typevars of generic types when used in Type[C] #3833

Merged
merged 1 commit into from Aug 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/checkexpr.py
Expand Up @@ -154,6 +154,10 @@ def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
elif isinstance(node, TypeInfo):
# Reference to a type object.
result = type_object_type(node, self.named_type)
if isinstance(self.type_context[-1], TypeType):
# This is the type in a Type[] expression, so substitute type
# variables with Any.
result = erasetype.erase_typevars(result)
elif isinstance(node, MypyFile):
# Reference to a module object.
try:
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-classes.test
Expand Up @@ -3373,6 +3373,17 @@ reveal_type(f(e1t)) # E: Revealed type is '__main__.A'

reveal_type(f('')) # E: Revealed type is 'builtins.str'

[case testTypeCErasesGenericsFromC]
from typing import Generic, Type, TypeVar

K = TypeVar('K')
V = TypeVar('V')
class ExampleDict(Generic[K, V]): ...

D = TypeVar('D')
def mkdict(dict_type: Type[D]) -> D: ...
reveal_type(mkdict(ExampleDict)) # E: Revealed type is '__main__.ExampleDict*[Any, Any]'

-- Synthetic types crashes
-- -----------------------

Expand Down