-
-
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
DRAFT: Returning TypedDict for dataclasses.asdict #8339
Conversation
Maybe this would be expanding the scope too much, but there have been many times I've wanted something similar. If you could implement this as a generic type, such as
Something like TypeScript's mapped types would be even better but that's rocket science compared to this. |
@intgr It's out of scope for this PR, but I have to agree with you and I've also really wanted to make it easier to type these kinds of things. It deserves an issue where it can be discussed and maybe on https://github.com/python/typing/ instead? The problem I encountered is basically that at runtime |
You should be able to support For named tuples and tuple subclasses, you can perhaps use the Recursion is currently not possible, since general recursive types aren't supported. One idea would be to detect recursive references and replace them with |
e2daec8
to
c26d6d7
Compare
Hi @JukkaL. Thanks for the pointers. There is a problem I ran into with this, though. The type vars on the list/dict can be constrained or have variance in the subclass such that, once the type var is converted from a dataclass to a TypedDict, the resultant type violates the constraints/variance of the original TypeVar. I am not sure how I can get mypy to throw errors in that situation (or if we should just fallback to I am still not sure how to support subclasses of I also managed to implement detection of recursion, where an error is thrown and it falls back to producing TODO before ready for review:
|
…nstances of the supertype (list/dict). Currently, I cannot figure out how to throw variance/constraint errors when they are violated as a result of transforming the type.
Going to make a new PR |
I wanted to implement dataclasses.asdict returning TypedDicts. I ran into issues supporting the recursion that it does. Does anyone have suggestions as to the problems I commented in the tests? Thanks
Edit: Link to comment: https://github.com/python/mypy/pull/8339/files#diff-5b6d315f9321d8b1943195ed39e18f14R1084
Edit: Relates to #5152