-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot turn existing dictionary into TypedDict #8890
Comments
While this is difficult in principle if This gap makes it quite tough to do more complicated manipulations involving |
Indeed, even the following doesn't work: class D2(D):
c: str
d5a = D2(d0.copy(), c="bar")
d5b = D2(dict(d0.copy(), c="bar"))
d6a = D2(d0, c="bar")
d6b = D2(dict(d0, c="bar")) |
If there's any workarounds for this - or even best practices - (perhaps not even using the constructor and using a |
I just stumbled over the same problem and used this as a solution: import typing
class D(TypedDict):
a: str
b: int
d_untyped = {"a": "foo", "b": 1}
d_typed = typing.cast(D, d_untyped) See also this blog post: https://adamj.eu/tech/2021/07/06/python-type-hints-how-to-use-typing-cast/ Don´t know, if that is the way to go for now and would be interested in other opinions, too :) |
…it accepts another instance of the same TypedDict (or another TypedDict that is type compatible) as an argument. This makes pyright compatible with mypy in this regard. See python/mypy#8890 for details.
I expected any of these forms to be allowed by MyPy, but they aren't:
I checked that
cast(D, ...)
works, but I wonder if this is the right way to proceed, or a workaround.The text was updated successfully, but these errors were encountered: