Replies: 1 comment 2 replies
-
No, there's nothing in the Python type standards that enable this. A TypedDict has very specific semantics as specified in PEP 589. If you simply "cast" a dataclass to a TypedDict, those semantic rules will be violated. For example, TypedDict doesn't allow the use of member access expressions (e.g. If you want to create a TypedDict from a class, you'd need to programmatically (at runtime) transform the dataclass object into a new TypedDict object. @dataclass
class Params:
a: int = 42
class ParamsDict(TypedDict):
a: int
def convert_params_to_dict(val: Params) -> ParamsDict:
return {"a": val.a} There's also currently no way to specify that a |
Beta Was this translation helpful? Give feedback.
-
Is it possible to express the concept of "a TypedDict containing the same fields as <some other class>" to Pyright?
For example:
(I can't use a straight TypedDict, because they don't support defaults. Besides, it's be nice to be able to use .field syntax for such objects, i.e.
params.a = 43
, rather thanparams['a'] = 43
).Beta Was this translation helpful? Give feedback.
All reactions