| ▲ | dmurray 2 days ago |
| Auto-generate a TypedDict type while type checking, while doing nothing at runtime. I expect this is not a very big ask and the various typecheckers will have versions of this soon after release. |
|
| ▲ | codethief 2 days ago | parent | next [-] |
| After what release? Type checkers already support the various TypedDict PEPs, even those that haven't been accepted yet. Yet, none of them infers TypedDict types. |
|
| ▲ | mrweasel 2 days ago | parent | prev [-] |
| Okay so basically just "inline": class MyType(TypedDict):
a: str
b: int
or infer the type hint in: my_typed_dict: dict[str, int] = {"a": 5}
It should probably be something like: auto_typed: dict[typing.Const, typing.Const] = {"a": 5}
where typing.Const defaults to Any for an empty dict. |
| |
| ▲ | codethief 2 days ago | parent [-] | | Not sure we're talking about the same thing. Inline TypedDicts are already in the process of being formalized, see https://peps.python.org/pep-0764/ , and have experimental support in e.g. Pyright. What I meant was that foo = {"a": 5}
should be inferred as foo: TypedDict[{ "a": Literal[5] }] = {"a": 5}
| | |
| ▲ | mrweasel 2 days ago | parent [-] | | Ah, I didn't know about PEP 795. I don't really know if I like it though. It looks like something someone invented because they don't want to write classes or specifically want to be able to access a data structure like a dict, for whatever reason. It basically provides data types, but also ensures that you have no easy way to reuse them, and it will miss cases where two data structures happen to have the same signature. It's getting a little messy when we have class, namedtuple and TypedDict, which all can do much the same thing. | | |
| ▲ | codethief 17 hours ago | parent [-] | | > it will miss cases where two data structures happen to have the same signature. Huh. That's exactly the point: TypedDicts are structural types. > we have class, namedtuple and TypedDict, which all can do much the same thing. No, they can't. The former two are nominal types. |
|
|
|