Skip to content

Final TypedDict #12266

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

Closed
JukkaL opened this issue Mar 1, 2022 · 2 comments
Closed

Final TypedDict #12266

JukkaL opened this issue Mar 1, 2022 · 2 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 1, 2022

We could perhaps support final TypedDicts that don't allow subtypes to have extra keys. Example:

from typing import final, TypedDict

@final
class A(TypedDict):
    x: int

@final
class B(TypedDict):
    x: int
    y: str

def f(d: A) -> None: ...

d: B = {...}
f(d)  # Error -- extra key 'y'

Here are some random thoughts:

  • We should be able to infer a more precise type for values(), since there can't be extra "hidden" values with arbitrary types.
  • A non-final TypedDict isn't compatible with a final one, since it could have some extra keys due to structural subtyping.
  • This could help with exhaustiveness checking in match statements.

I haven't thought carefully about all the implications of this feature. Before implementing this, some additional analysis would be useful.

See #10759 for more context.

@97littleleaf11
Copy link
Collaborator

Previous discussion: #7981

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 2, 2022

Closing as dupe of #7981, oops.

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

No branches or pull requests

2 participants