-
Notifications
You must be signed in to change notification settings - Fork 234
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
PEP 692 follow up: Unpacking compatibility with dataclass/others #1495
Comments
At runtime, I don't know what it would mean if I don't understand what |
In my mind, this would be "unpack person" aka "what is in person". Just like unpacking a typedict conceptually is looking inside the typed dict for what kwargs are acceptable, one can do the same (conceptually) for a dataclass.
Here's a use case I encounter all the time which makes me go to a webpage's docs instead of being able to use my IDE. def plot_point(x: float, y: float, size: float = 1.0, color: str = "red"):
...
def plot_points(xs: list[float], ys: list[float], **kwargs):
for x, y in zip(xs, ys):
plot_point(x, y, **kwargs) I bought up To just stick to matplotlib, given its the most popular visualisation library, |
Sorry, missed this comment:
This would also make me a happy dev! |
This would be very handy when working with data (de)serialization. I'm sure many devs building on Marshmallow or Pydantic feel the same. Being able to easily convert dataclasses to TypedDicts (and back?) is a missing link in the current ecosystem. Would be great to be able to do something like this and keep type annotations:
|
IMO this would be really useful. For instance, pydantic models can have proper |
Specifying that kwargs should match the fields of a given dataclass would be very useful indeed. My last potential usage: @dataclass(kw_only=True, frozen=True)
class CompilationOptions:
...
def updated(self, **update: dict[str, Any]) -> "CompilationOptions": # <- Unpack["CompilationOptions"] would be nice here
"""Create an updated copy of itself."""
return CompilationOptions(**(asdict(self) | update)) (This seems to be a recurrent request, btw. For example: |
Hi,
|
I tried asking for thoughts about this on the Python Typing mailing list a while back (Aug. 2022), didn't get any responses there: |
The
Unpack
addition has been great, however a lot of our code has an existing library of dataclasses instead of typed dictionaries. Here's a dummy example using one of the more common patterns that will suffer from this: factory methods.Right now, the "fix" for us would be to duplicate the dataclass as TypedDict, but code duplication is obviously not ideal. If there's another way, please let me know, otherwise I think a really valuable enhancement to the Unpack method would be to allow it to accept other objects, such as a
dataclass
or a pydanticBaseModel
given the popularity of pydantic and FastAPI.Antoher use case I can think of would be to be able to
Unpack[function]
orUnpack[class]
. Common examples here would includematplotlib
andplotly
, where plotting functions often exposekwargs
which just get passed to a child function. That child function has all the documentation and type hinting you'd need, but its unusable unless its copied into a TypeDict (I believe). For a concrete example, the top levelmaptlotlib
plt.plot()
function takes kwargs which are passed to theLine2D
classThe text was updated successfully, but these errors were encountered: