KEMBAR78
Experimental: allow inline/anonymous TypedDicts by ilevkivskyi · Pull Request #17457 · python/mypy · GitHub
Skip to content

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Jun 30, 2024

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

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@JukkaL JukkaL 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 type intersection instead of a union,

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. {"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
Member Author

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

  • __future__.annotations are not used
  • inline TypedDict 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 banning list[int] on old Python versions).

@github-actions
Copy link
Contributor

github-actions bot commented Jul 4, 2024

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]

@ilevkivskyi
Copy link
Member Author

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

@ilevkivskyi ilevkivskyi merged commit 6d45f3c into python:master Jul 7, 2024
@ilevkivskyi ilevkivskyi deleted the anon-typed-dict branch July 7, 2024 09:59
@sobolevn
Copy link
Member

sobolevn commented Aug 4, 2024

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 that TypedDict[{"a": int}] is better than {"a: int}, for example TypedDict[{"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 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.

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anonymous and inline declaration of TypedDict types

4 participants