Skip to content
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

Deprecate unusual ways of creating empty TypedDicts #105570

Closed
AlexWaygood opened this issue Jun 9, 2023 · 2 comments · Fixed by #105780
Closed

Deprecate unusual ways of creating empty TypedDicts #105570

AlexWaygood opened this issue Jun 9, 2023 · 2 comments · Fixed by #105780
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 9, 2023

Feature or enhancement

I propose that we deprecate the following two ways of creating empty TypedDicts:

from typing import TypedDict

T = TypedDict("T")
T2 = TypedDict("T2", None)

Pitch

Users currently have four distinct options if they want to create an empty TypedDict with no fields:

T = TypedDict("T")
T2 = TypedDict("T2", None)
T3 = TypedDict("T3", {})
class T4(TypedDict): ...

Of these four options, only T3 and T4 are supported by type checkers. typing.TypedDict has been around for a while now, so if T1 and T2 are still unsupported by type checkers, they probably never will be supported.

Deprecating, and eventually removing, the first two ways of constructing empty TypedDicts will allow us to simplify the code at runtime. It will also be less confusing for users. Every way in which the runtime and type checkers differ in behaviour is a potential point of confusion for users; in general, we should work to keep these points of difference to a minimum.

I also don't think these methods of constructing empty TypedDicts are particularly common.

Previous discussion

For very similar reasons, we previously deprecated and removed the keyword-argument syntax for creating TypedDicts. This was deprecated in 3.11, and removed in 3.13:

In retrospect, we should really have deprecated these ways of creating empty TypedDicts at the same time. But there's no way of changing that now.

Linked PRs

@AlexWaygood AlexWaygood added type-feature A feature request or enhancement stdlib Python modules in the Lib dir topic-typing 3.13 bugs and security fixes labels Jun 9, 2023
@sobolevn
Copy link
Member

I think that we should keep T4, because this way people might:

  1. Create some base metadata object and use it for inheritance later on
  2. Create literal empty dict: class EmptyDict(TypedDict): ..., which is a valid use-case

Others are good candidates to be deprecated, I agree.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Jun 10, 2023

I think that we should keep T4

Yes, I agree. I'm proposing we deprecate T1 and T2, but keep T3 and T4. I've edited my original post to clarify that point :)

AlexWaygood added a commit that referenced this issue Jun 14, 2023
Deprecate two methods of creating typing.TypedDict classes with 0 fields using the functional syntax: `TD = TypedDict("TD")` and `TD = TypedDict("TD", None)`. Both will be disallowed in Python 3.15. To create a TypedDict class with 0 fields, either use `class TD(TypedDict): pass` or `TD = TypedDict("TD", {})`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants