fromtypingimportTypedDict, Required, NotRequiredclassD(TypedDict):
x: Required[int]
y: NotRequired[int]
deftest(d: D, s: str) ->None:
reveal_type(d.get("x")) # int | None reveal_type(d.get("x", 1)) # int reveal_type(d.get("x", s)) # int | strreveal_type(d.get("y")) # int | None reveal_type(d.get("y", 1)) # intreveal_type(d.get("y", s)) # int | str
Both d.get("x") and d.get("x", s) could give the more precise int: Since the key "x' is required, the default is irrelevant. This is how pyright handles it as well.
Moreover, it would be nice if reveal_type(d.get) would give an appropriate overload signature.
Activity
More precise return types for `TypedDict.get` (#19897)