Replies: 5 comments 17 replies
-
Yes, I am working on a PEP for this :) There are several things to get right here, I will publish it soon enough (but I think, I won't make it into 3.12). |
Beta Was this translation helpful? Give feedback.
-
After the new PEP, are there any uses for For example, consider:
If I understand correctly, this can now be written as:
|
Beta Was this translation helpful? Give feedback.
-
I was messing around with the experimental support of this in Pyright, would using Typevars in the dict be in the scope of this PEP? Currently Pyright errors on this but I don't know whether that's because its still experimental. something akin to this example: K = TypeVar("K", bound=LiteralString)
V = TypeVar("V")
def create(key: K, value: V) -> dict[{K: V}]:
return {key: value}
my_dict = create("a", 1)
reveal_type(my_dict["a"]) # Literal[1] |
Beta Was this translation helpful? Give feedback.
-
Should this also allow for something like this? def extra[D:TypedDict](data: D) -> TypedDict[{**D, 'score':float}]:
return {
**data,
'score': 0.0,
}
def peel[D:TypedDict](data:TypedDict[{**D, 'score': float}]) -> D:
copy = data.copy()
del copy['score']
return copy
def parse_score[D:TypedDict](data: TypedDict[{**D, 'score': str}]) -> TypedDict[{**D, 'score':float}]
copy = data.copy()
copy['score'] = float(copy['score'])
return copy |
Beta Was this translation helpful? Give feedback.
-
For anyone interested, a PEP is under discussion here: https://discuss.python.org/t/78779. |
Beta Was this translation helpful? Give feedback.
-
In the typing-sig thread Inline syntax for TypedDict, someone proposed an experimental syntax that looks roughly like
dict[{"key1": str, "key2": int}]
. Shortly after, Eric Traut added experimental support to pyright. I've created this post to share my feedback, in the same spirit as my message in the PEP 695 forum thread.Usability improvements
tl;dr I've been playing with this for a few days now and find the syntax to be intuitive and pleasant to use. It provides meaningful benefits (making typing easier, enabling types that are specific to one location, much nicer support of nested structure) without the overhead of having to define throwaway classes.
Typing one-off dicts is more "worth it"
I often run into dicts that have a known structure, but that I don't need more than once. One example is a function that reads from a file or hits some API, does something with the dict, and then discards it when returning.
Using the new syntax, I don't need to come up with a throwaway name. The type definition is inherently linked to the function it supports. No one can become an inadvertent consumer.
Typing nested dicts is much easier
Creating nested
TypedDict
s today is cumbersome. Every level of nesting has to be defined and named individually, since call expressions are not allowed in annotations. The intermediate levels of nesting are often not themselves meaningful or only meaningful in the context of the parent.For the record, this is still hard to read (perhaps harder—that's what, 5 closing chars in a row?). But this kind of anonymous nesting was simply not possible before; this improves the usability of typing. There are compromises, too, where the overall type could be defined as a standalone entity with only
nested_key
defined inline.IDE support
I know that language server autocompletion is not a standard, but being able to define one-off dicts creates a smoother developer experience. It's more likely that someone will add a type definition inline simply because it's easier than defining a new class.
I noticed this in test code, too. I often see/write test functions that are parameterized by a list of dicts, where each dict describes one test case. I don't think I'd ever define a
TypedDict
here—it feels like overkill—but defining it inline gives me LSP completion and type-checking warnings with very little extra work.Eric noted as much in his original email and I wholeheartedly agree:
Finally, it's easy to adopt this gradually. We can already make an annotation
x: dict
incrementally more precise by adding e.g.,x: dict[str, int]
. The proposed syntax ofx: dict[{"key": int, "key2": int}]
is a natural extension of this, being a small diff that improves usability.Closing
From Eric's original email, again:
I agree with this. The biggest downside is that it can be hard to read, but I think it's worth the tradeoff; this makes TypedDicts much more usable than they are today.
Beta Was this translation helpful? Give feedback.
All reactions