Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Experimental: allow inline/anonymous TypedDicts#17457

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ilevkivskyi merged 4 commits intopython:masterfromilevkivskyi:anon-typed-dict
Jul 7, 2024

Conversation

@ilevkivskyi
Copy link
Member

@ilevkivskyiilevkivskyi commentedJun 30, 2024
edited
Loading

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

danieleds, emosenkis, blast-hardcheese, aploium, and aleksanb reacted with rocket emoji
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@JukkaLJukkaL left a 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.

@bzoracler
Copy link
Contributor

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__future__.annotations guard, a union of two bare literal dicts in a runtime type annotation context will result in what looks like a typeintersection instead of a union,

classA: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.{"str": str} | list[int]).

Maybe (before a PEP etc is proposed for the actual syntax), unions should be banned from being composed with bare literal dicts?

@ilevkivskyi
Copy link
MemberAuthor

@bzoracler Although a valid concern, this is really a corner case. I mean all four things need to happen:

  • __future__.annotations are not used
  • inlineTypedDict is used
  • TypedDict appears in a union
  • the user is interested in runtime type-checking

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 banninglist[int] on old Python versions).

@github-actions
Copy link
Contributor

Diff frommypy_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]

@ilevkivskyi
Copy link
MemberAuthor

@JukkaL I put this behind a flag, if there are no more suggestions I will be merging this soon.

@ilevkivskyiilevkivskyi merged commit6d45f3c intopython:masterJul 7, 2024
@ilevkivskyiilevkivskyi deleted the anon-typed-dict branchJuly 7, 2024 09:59
@sobolevn
Copy link
Member

sobolevn commentedAug 4, 2024
edited
Loading

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:

  1. I think that all agreed thatTypedDict[{"a": int}] is better than{"a: int}, for exampleTypedDict[{"a: int"}] | None will work when{"a": int} | None - won't.
  2. The most problematic part (which really killed the proposal) was the semantics for update / inheritance for inline typed dict. Let's say that you have a function like:
_TD:TypeAlias=TypedDict[{"a":int}]# Syntax was not decided and the whole idea was rejecteddefadd_key(obj:_TD)->TypedDict[{*_TD,*{"b":int}}]:obj["b"]=0returnobj

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.

  1. The syntax above in my example would require a PR / spec for thetyping /typing_extensions module, while this one does not require any changes.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@JukkaLJukkaLJukkaL left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Anonymous and inline declaration ofTypedDict types

4 participants

@ilevkivskyi@bzoracler@sobolevn@JukkaL

[8]ページ先頭

©2009-2025 Movatter.jp