Skip to content

Commit

Permalink
Fix crash during type inference
Browse files Browse the repository at this point in the history
Fixes python#674.
  • Loading branch information
JukkaL authored and JamesGuthrie committed May 31, 2015
1 parent 4795f98 commit ba347c8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class ConstraintBuilderVisitor(TypeVisitor[List[Constraint]]):
"""Visitor class for inferring type constraints."""

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

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

def visit_type_var(self, template: TypeVarType) -> List[Constraint]:
return [Constraint(template.id, self.direction, self.actual)]
if self.actual:
return [Constraint(template.id, self.direction, self.actual)]
else:
return []

# Non-leaf types

Expand Down
1 change: 0 additions & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def join_types(s: Type, t: Type) -> Type:
If the join does not exist, return an ErrorType instance.
"""

if isinstance(s, AnyType):
return s

Expand Down
6 changes: 6 additions & 0 deletions mypy/test/data/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,9 @@ def voidify(n: int) -> None: pass
def identity(f: Callable[[A], None]) -> Callable[[A], None]:
return f
identity(voidify)(3)

[case testTypeVariableUnionAndCallableInTypeInference]
from typing import Union, Callable, TypeVar
T = TypeVar('T')
def f(x: T, y: Union[T, Callable[[T], None]]) -> None: pass
f('', '')

0 comments on commit ba347c8

Please sign in to comment.