Skip to content

Commit

Permalink
Work in progress for fixing python#462
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed Oct 25, 2014
1 parent 3d16889 commit 140e949
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
12 changes: 8 additions & 4 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,14 @@ def infer_function_type_arguments_using_context(
erased_ctx = replace_func_type_vars(ctx, ErasedType())
ret_type = callable.ret_type
if isinstance(ret_type, TypeVar):
if ret_type.values:
# The return type is a type variable with values, but we can't easily restrict
# type inference to conform to the valid values. Give up and just use function
# arguments for type inference.
if ret_type.values or (not isinstance(ret_type, Instance) or
not cast(Instance, ret_type).args):
# The return type is a type variable. If it has values, we can't easily restrict
# type inference to conform to the valid values. If it's unrestricted, we could
# infer a too general type for the type variable if we use context. Give up and
# just use function arguments for type inference.
#
# See also github issues #462 and #360.
ret_type = NoneTyp()
args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)
# Only substite non-None and non-erased types.
Expand Down
9 changes: 9 additions & 0 deletions mypy/test/data/check-inference-context.test
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,12 @@ f([1])
f('')
f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "Union[List[None], str]"
[builtins fixtures/isinstancelist.py]

[case testIgnoringInferenceContext]
from typing import typevar, List
T = typevar('T')
def f(x: List[T]) -> T: pass
def g(y: object) -> None: pass
a = [1]
g(f(a))
[builtins fixtures/list.py]
2 changes: 1 addition & 1 deletion mypy/test/data/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ t = typevar('t')
ab, ac, b, c = Undefined, Undefined, Undefined, Undefined # type: (A[B], A[C], B, C)
b = f(ab)
c = f(ac)
b = f(ac) # E: Argument 1 to "f" has incompatible type A[C]; expected A[B]
b = f(ac) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
b = f(b)
c = f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "C")
@overload
Expand Down
14 changes: 7 additions & 7 deletions mypy/test/data/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ b = Undefined # type: B
c = Undefined # type: C
o = Undefined # type: object

a = f(o) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
b = f(b, a) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
b = f(a, b) # E: Argument 1 to "f" has incompatible type "A"; expected "B"
a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
b = f(b, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
b = f(a, b) # E: Incompatible types in assignment (expression has type "A", variable has type "B")

o = f()
a = f(a)
Expand All @@ -139,10 +139,10 @@ T = typevar('T')
a = Undefined # type: A
o = Undefined # type: object

a = f(o) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
a = f(a, o) # E: Argument 2 to "f" has incompatible type "object"; expected "A"
a = f(a, a, o) # E: Argument 3 to "f" has incompatible type "object"; expected "A"
a = f(a, a, a, o) # E: Argument 4 to "f" has incompatible type "object"; expected "A"
a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(a, a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(a, a, a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")

a = f(a)
a = f(a, a)
Expand Down

0 comments on commit 140e949

Please sign in to comment.