@@ -690,6 +690,60 @@ l = lb # E: Incompatible types in assignment (expression has type List[bool], va
690
690
[builtins fixtures/for.py]
691
691
692
692
693
+ -- Generic function inference with unions
694
+ -- --------------------------------------
695
+
696
+
697
+ [case testUnionInference]
698
+ from typing import TypeVar, Union, List
699
+ T = TypeVar('T')
700
+ U = TypeVar('U')
701
+ def f(x: Union[T, int], y: T) -> T: pass
702
+ f(1, 'a')() # E: "str" not callable
703
+ f('a', 1)() # E: "object" not callable
704
+
705
+ def g(x: Union[T, List[T]]) -> List[T]: pass
706
+ def h(x: List[str]) -> None: pass
707
+ g('a')() # E: List[str] not callable
708
+ g(['a']) # E: Argument 1 to "g" has incompatible type List[str]; expected "Union[None, List[None]]"
709
+ h(g(['a']))
710
+
711
+ def i(x: Union[List[T], List[U]], y: List[T], z: List[U]) -> None: pass
712
+ a = [1]
713
+ b = ['b']
714
+ i(a, a, b)
715
+ i(b, a, b)
716
+ i(a, b, b) # E: Argument 1 to "i" has incompatible type List[int]; expected "Union[List[str], List[str]]"
717
+ [builtins fixtures/list.py]
718
+
719
+
720
+ [case testUnionInferenceWithTypeVarValues]
721
+ from typing import TypeVar, Union
722
+ AnyStr = TypeVar('AnyStr', bytes, str)
723
+ def f(x: Union[AnyStr, int], *a: AnyStr) -> None: pass
724
+ f('foo')
725
+ f('foo', 'bar')
726
+ f('foo', b'bar') # E: Type argument 1 of "f" has incompatible value "object"
727
+ f(1)
728
+ f(1, 'foo')
729
+ f(1, 'foo', b'bar') # E: Type argument 1 of "f" has incompatible value "object"
730
+ [builtins fixtures/primitives.py]
731
+
732
+
733
+ [case testUnionTwoPassInference-skip]
734
+ from typing import TypeVar, Union
735
+ T = TypeVar('T')
736
+ U = TypeVar('U')
737
+ def j(x: Union[List[T], List[U]], y: List[T]) -> List[U]: pass
738
+
739
+ a = [1]
740
+ b = ['b']
741
+ # We could infer: Since List[str] <: List[T], we must have T = str.
742
+ # Then since List[int] <: Union[List[str], List[U]], and List[int] is
743
+ # not a subtype of List[str], we must have U = int.
744
+ j(a, b)
745
+
746
+
693
747
-- Literal expressions
694
748
-- -------------------
695
749
0 commit comments