Skip to content

Commit 42b9328

Browse files
committed
Fix crash during type inference
Fixes #674.
1 parent f2946b2 commit 42b9328

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

mypy/constraints.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class ConstraintBuilderVisitor(TypeVisitor[List[Constraint]]):
127127
"""Visitor class for inferring type constraints."""
128128

129129
# The type that is compared against a template
130+
# TODO: The value may be None. Is that actually correct?
130131
actual = Undefined(Type)
131132

132133
def __init__(self, actual: Type, direction: int) -> None:
@@ -154,7 +155,10 @@ def visit_erased_type(self, template: ErasedType) -> List[Constraint]:
154155
# Non-trivial leaf type
155156

156157
def visit_type_var(self, template: TypeVarType) -> List[Constraint]:
157-
return [Constraint(template.id, self.direction, self.actual)]
158+
if self.actual:
159+
return [Constraint(template.id, self.direction, self.actual)]
160+
else:
161+
return []
158162

159163
# Non-leaf types
160164

mypy/join.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def join_types(s: Type, t: Type) -> Type:
5454
5555
If the join does not exist, return an ErrorType instance.
5656
"""
57-
5857
if isinstance(s, AnyType):
5958
return s
6059

mypy/test/data/check-generics.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,9 @@ def voidify(n: int) -> None: pass
715715
def identity(f: Callable[[A], None]) -> Callable[[A], None]:
716716
return f
717717
identity(voidify)(3)
718+
719+
[case testTypeVariableUnionAndCallableInTypeInference]
720+
from typing import Union, Callable, TypeVar
721+
T = TypeVar('T')
722+
def f(x: T, y: Union[T, Callable[[T], None]]) -> None: pass
723+
f('', '')

0 commit comments

Comments
 (0)