@@ -873,13 +873,7 @@ def g(x: Union[T, List[T]]) -> List[T]: pass
873873def h(x: List[str]) -> None: pass
874874g('a')() # E: "List[str]" not callable
875875
876- # The next line is a case where there are multiple ways to satisfy a constraint
877- # involving a Union. Either T = List[str] or T = str would turn out to be valid,
878- # but mypy doesn't know how to branch on these two options (and potentially have
879- # to backtrack later) and defaults to T = Never. The result is an
880- # awkward error message. Either a better error message, or simply accepting the
881- # call, would be preferable here.
882- g(['a']) # E: Argument 1 to "g" has incompatible type "List[str]"; expected "List[Never]"
876+ g(['a'])
883877
884878h(g(['a']))
885879
@@ -891,6 +885,28 @@ i(b, a, b)
891885i(a, b, b) # E: Argument 1 to "i" has incompatible type "List[int]"; expected "List[str]"
892886[builtins fixtures/list.pyi]
893887
888+ [case testUnionInferenceOfGenericClassAndItsGenericType]
889+ from typing import Generic, TypeVar, Union
890+
891+ T = TypeVar('T')
892+
893+ class GenericClass(Generic[T]):
894+ def __init__(self, value: T) -> None:
895+ self.value = value
896+
897+ def method_with_union(arg: Union[GenericClass[T], T]) -> GenericClass[T]:
898+ if not isinstance(arg, GenericClass):
899+ arg = GenericClass(arg)
900+ return arg
901+
902+ result_1 = method_with_union(GenericClass("test"))
903+ reveal_type(result_1) # N: Revealed type is "__main__.GenericClass[builtins.str]"
904+
905+ result_2 = method_with_union("test")
906+ reveal_type(result_2) # N: Revealed type is "__main__.GenericClass[builtins.str]"
907+
908+ [builtins fixtures/isinstance.pyi]
909+
894910[case testCallableListJoinInference]
895911from typing import Any, Callable
896912
0 commit comments