Skip to content

Feature request: option for strict TypedDict __getitem__ #6483

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
ariebovenberg opened this issue Feb 26, 2019 · 1 comment
Closed

Feature request: option for strict TypedDict __getitem__ #6483

ariebovenberg opened this issue Feb 26, 2019 · 1 comment

Comments

@ariebovenberg
Copy link

TypedDict is really useful for expressing data with a consistent shape, but where not all fields are present.
I feel it can be improved by adding a "strict" mode for accessing items. This way, mypy can enforce that the proper checks are made.

An example to illustrate:

from mypy_extensions import TypedDict

class FooBase(TypedDict):
    x: int  # a required field

class Foo(FooBase, total=False):
    y: int  # an optional field

f = Foo(x=5)

# current situation
f['x']  # ok
f['y']  # ok
f.get('y', 0)  # ok

# "strict" situation
f['x']  # ok (item is always present)
f['y']  # FAIL
f.get('y', 0)  # ok (explicit fallback is needed)

I understand that not everyone wants this behavior, so perhaps it would be useful as a strictness flag?

@AlexWaygood
Copy link
Member

Closing as a duplicate of #12094, which is a newer issue but has a PR attached to it.

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

4 participants