Skip to content

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

Description

@RonnyPfannschmidt

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

Activity

  1. AlexWaygood commented on May 30, 2022

    @AlexWaygood
    Member

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

  2. RonnyPfannschmidt commented on May 30, 2022

    @RonnyPfannschmidt
    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

  3. AlexWaygood commented on May 30, 2022

    @AlexWaygood
    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.

  4. AlexWaygood commented on May 31, 2022

    @AlexWaygood
    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.

  5. tony commented on Jul 11, 2022

    @tony

    @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?

  6. RonnyPfannschmidt commented on Jul 11, 2022

    @RonnyPfannschmidt
    Author

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

  7. tony commented on Jul 12, 2022

    @tony

    @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?

  8. RonnyPfannschmidt commented on Jul 13, 2022

    @RonnyPfannschmidt
    Author

    Yes

  9. kdkavanagh commented on Oct 6, 2025

    @kdkavanagh

    Hi @RonnyPfannschmidt curious if you know if this was ever formally proposed/superseded by something else? Very interested in this functionality

  10. RonnyPfannschmidt commented on Oct 6, 2025

    @RonnyPfannschmidt
    Author

    Nope still missing as fas as i know

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions