Skip to content
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

unpack dataclasses/types or subsets of them to act as typed dict #12906

Closed
RonnyPfannschmidt opened this issue May 30, 2022 · 8 comments
Closed

Comments

@RonnyPfannschmidt
Copy link

Feature

often times helper methods take all the fields or subsets of the fields of a type/dataclass

in coordination with #4441

@dataclass
class MyModel:
   id: uuid
   name: str
   hostname: str
   memory_size: int

and i want to declare helpers like

# bad names
Magic = SubsetTypedDict(MyModel, exclude=["id"], total=False)

def wait_for_update(model_or_id: Model|uuid, **kw: Magic) -> Model:
   ...

** context **

spin out of #4441, based on my comment and the suggestion in

@AlexWaygood
Copy link
Member

There's already a PR to make this happen: #8583

@RonnyPfannschmidt
Copy link
Author

@AlexWaygood i strongly disagree

my understanding is that the pr makes mypy pretends that the return value of the as_dict methods is a matching typed dict
it does not in any way give access to that type for other usage in the actual code

@AlexWaygood
Copy link
Member

I see, apologies. Your proposed new way of constructing a TypedDict (a SubsetTypedDict), however, looks like it would probably require a PEP. It should probably be discussed over at the https://github.com/python/typing issue tracker rather than here, since it would impact the typing ecosystem as a whole rather than mypy.

@AlexWaygood
Copy link
Member

I'm closing, because I don't think this is the appropriate forum for this kind of feature request, but please feel free to open a new issue at https://github.com/python/typing or to start a discussion on the typing-sig mailing list.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2022
@tony
Copy link

tony commented Jul 11, 2022

@RonnyPfannschmidt I propose as a next step, writing this out on the typing-sig list and seeing what the reception is

I think the conversation it'd generate (whether there's overlap with someone else or a PEP) would be fruitful. Would you like to write a message there?

@RonnyPfannschmidt
Copy link
Author

Unfortunately my bandwidth for this is currently zero, and likely to stay like that for the next few months

@tony
Copy link

tony commented Jul 12, 2022

@RonnyPfannschmidt No problem. Is it okay if in the future if someone stumbles upon this they can cite this as an idea on the typing-sig list if they want to?

@RonnyPfannschmidt
Copy link
Author

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants