I would like to combine TypedDict with with Union. So that a function is able to return either A or B. Mypy is able to correctly detect a TypedDict return type directly. But when a TypedDict is nested in an Union it doesn't work.
from typing_extensions import TypedDict
from typing import Union
class A(TypedDict):
a: str
class B(TypedDict):
b: str
def works() -> A:
return {'a': 'value'}
# Works as expected
def problem() -> Union[A, B]:
return {'a': 'value'}
# mypy_error: Incompatible return value type (got "Dict[str, str]", expected "Union[A, B]")
# Reports an error while it should be valid
def workaround() -> Union[A, B]:
x: A = {'a': 'value'}
return x
# This works again but is not very elegant
A possible workaround would be to assign to temporary returned type hinted variable (see workaround()). Is there a more elegant way of doing this?
Note: Python 3.7