8 Sep
2020
8 Sep
'20
4:28 p.m.
I also prefer form (1) to respect the declared type and not use the rhs type. Keeping the declared type can avoid a spurious warning in cases where type invariance matters, e.g., with generics. T = TypeVar('T') class Foo(Generic[T]): def __init__(self, x: T) -> None: self.attr = x def g(x: Foo[Union[int, str]]): return x def f(x: int): y: Union[int, str] = 1 a = Foo(y) g(a) # no warning y = 2 b = Foo(y) reveal_type(b) g(b) # warning