-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental: allow inline/anonymous TypedDicts #17457
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! What about putting this behind an experimental feature flag, since this is non-standard? New users might discover and use this by accident otherwise, since the syntax is quite intuitive.
While I like the elegance of the bare literal dict, the problem is that it doesn't compose well with union expressions in type annotation contexts. Without a class A:
a: {"int": int} | {"str": str} >>> A.__annotations__
{'a': {'int': <class 'int'>, 'str': <class 'str'>}} and a union with most other objects would result in a runtime error (e.g. Maybe (before a PEP etc is proposed for the actual syntax), unions should be banned from being composed with bare literal dicts? |
@bzoracler Although a valid concern, this is really a corner case. I mean all four things need to happen:
If I would guess, this will probably affect 0.01% of users. I don't want to force the rest 99.99% of users to see some ugly/non-intuitive syntax because of this. We already made similar mistakes in the past (like banning |
Diff from mypy_primer, showing the effect of this PR on open source code: steam.py (https://github.com/Gobot1234/steam.py)
- steam/http.py:901: error: Invalid type comment or annotation [valid-type]
+ steam/http.py:901: error: Inline TypedDict is experimental, must be enabled with --enable-incomplete-feature=InlineTypedDict [misc]
|
@JukkaL I put this behind a flag, if there are no more suggestions I will be merging this soon. |
I presented this topic to Guido, @erictraut, @JelleZijlstra, @JukkaL and others around a year ago. As far as I remember we decided not to go for this feature. Several take-aways from this meeting:
_TD: TypeAlias = TypedDict[{"a": int}]
# Syntax was not decided and the whole idea was rejected
def add_key(obj: _TD) -> TypedDict[{*_TD, *{"b": int}}]:
obj["b"] = 0
return obj At the time I felt like inline typed dicts are not useful enough with the "extension" / "update" / "inheritance" syntax. But, now I think that it might be a good idea to start small.
|
Fixes #9884
I was always a bit skeptical about this thing, since it feels more like TypeScript than Python, but it is second most upvoted issue. Also (this specific) implementation is like 60 lines of code plus tests, so why not.
I know there is no PEP etc., but IMO this syntax is obvious and it just works.
cc @JukkaL