In the following example, why does the type of the variable foo
change when it's value is set to an integer but the same doesn't occur for the type of bar["foo"]
?
import typing as tp
foo: tp.Union[float, int]
D = tp.TypedDict("D", {"foo": tp.Union[float, int]})
foo = 123.45
bar = D(foo=123.45)
reveal_type(foo) # main.py:9: note: Revealed type is 'builtins.float'
reveal_type(bar["foo"]) # main.py:10: note: Revealed type is 'builtins.float'
foo = int(foo)
bar["foo"] = int(bar["foo"])
reveal_type(foo) # main.py:15: note: Revealed type is 'builtins.int'
reveal_type(bar["foo"]) # main.py:16: note: Revealed type is 'builtins.float'