From 222f787630db3dedb2dc342dcb4d522b9556ebe3 Mon Sep 17 00:00:00 2001 From: Alex Frieder Date: Sun, 29 Jan 2017 03:44:05 -0500 Subject: [PATCH 1/3] Fix crash on reassigning partial type --- mypy/applytype.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mypy/applytype.py b/mypy/applytype.py index d97670034a0bc..997d7ffd3d4dc 100644 --- a/mypy/applytype.py +++ b/mypy/applytype.py @@ -3,7 +3,7 @@ import mypy.subtypes from mypy.sametypes import is_same_type from mypy.expandtype import expand_type -from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType +from mypy.types import Type, TypeVarId, TypeVarType, CallableType, AnyType, PartialType from mypy.messages import MessageBuilder from mypy.nodes import Context @@ -34,14 +34,15 @@ def apply_generic_arguments(callable: CallableType, types: List[Type], for v1 in type.values): continue for value in values: - if mypy.subtypes.is_subtype(type, value): + if isinstance(type, PartialType) or mypy.subtypes.is_subtype(type, value): types[i] = value break else: msg.incompatible_typevar_value(callable, i + 1, type, context) upper_bound = callable.variables[i].upper_bound - if type and not mypy.subtypes.satisfies_upper_bound(type, upper_bound): + if (type and not isinstance(type, PartialType) and + not mypy.subtypes.satisfies_upper_bound(type, upper_bound)): msg.incompatible_typevar_value(callable, i + 1, type, context) # Create a map from type variable id to target type. From 16dd662b741c2c1980b5d3825325afa0f201f4c1 Mon Sep 17 00:00:00 2001 From: Alex Frieder Date: Sun, 29 Jan 2017 03:44:50 -0500 Subject: [PATCH 2/3] Add test for reassigning partial type --- test-data/unit/check-inference.test | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 799fb3525d703..9e983b3b004c8 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -935,6 +935,14 @@ if A: class A: pass class B: pass +[case testAssigningAnyStrToNone] +from typing import Tuple, TypeVar +AnyStr = TypeVar('AnyStr', str, bytes) + +def f(x: AnyStr) -> Tuple[AnyStr]: pass +x = None +(x,) = f('') + -- Inferring attribute types -- ------------------------- From d2f1284e37a4665fee95e05ac41131cdef23cab1 Mon Sep 17 00:00:00 2001 From: Alex Frieder Date: Wed, 1 Feb 2017 12:08:20 -0500 Subject: [PATCH 3/3] Add reveal_type to test --- test-data/unit/check-inference.test | 1 + 1 file changed, 1 insertion(+) diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 9e983b3b004c8..e6b043923a671 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -942,6 +942,7 @@ AnyStr = TypeVar('AnyStr', str, bytes) def f(x: AnyStr) -> Tuple[AnyStr]: pass x = None (x,) = f('') +reveal_type(x) # E: Revealed type is 'builtins.str' -- Inferring attribute types