TypeScript has a Pick type which can take an existing type and create a new one by picking individual attributes. I would like to have a similar functionality in Python, so for example:
from dataclasses import dataclass
from pick import Pick
@dataclasses.dataclass
class Foo:
x: int
y: float
z: str
Bar = Pick(Foo, ('x', 'y'))
foo = Foo(1, 2.0, '3')
bar = Bar(1, 2.0)
I want this to avoid the problem of partial object population without having to explicitly define many different classes which are really just subsets of a larger one. Ideally, this will be type-safe and recognized by mypy.
I looked at other solutions like defining a new class for every subset of attributes, but that produced too many classes which are all similar but just slightly different, making the code harder to read. I also tried having a large class with every field being optional, but that still makes the code hard to read because I have to reason about the lineage of an object to tell whether it will have some field, or if that field is null.
Bonus points if there is also a way to add a field via something like Baz = Add(Foo, {'w': List}); baz(1, 2.0, '3', w=[])