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

bpo-38834: enable runtime inspection of TypeDict classes #17214

Merged
merged 1 commit into from
Nov 24, 2019
Merged

bpo-38834: enable runtime inspection of TypeDict classes #17214

merged 1 commit into from
Nov 24, 2019

Conversation

Zac-HD
Copy link
Contributor

@Zac-HD Zac-HD commented Nov 18, 2019

This patch enables downstream projects inspecting a TypedDict subclass at runtime to tell which keys are optional, which is essential for generating test data with Hypothesis or validating inputs with typeguard or pydantic.

More background on the typing-sig mailing list (cc @ilevkivskyi).

https://bugs.python.org/issue38834

This patch enables downstream projects inspecting a TypedDict subclass at runtime to tell which keys are optional.

This is essential for generating test data with Hypothesis or validating inputs with typeguard or pydantic.
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! All looks good.

@ilevkivskyi
Copy link
Member

@gvanrossum What do you think, can we include this in 3.8.1? Or should it just go to master (3.9) and typing_extensions?

@gvanrossum
Copy link
Member

@gvanrossum What do you think, can we include this in 3.8.1? Or should it just go to master (3.9) and typing_extensions?

I'd put in 3.9 (master) and in typing_extensions.

@ilevkivskyi
Copy link
Member

OK, @Zac-HD could you please make a PR to https://github.com/python/typing adding this to typing_extensions?

@ilevkivskyi ilevkivskyi merged commit 665ad3d into python:master Nov 24, 2019
@Zac-HD Zac-HD deleted the typdedict-runtime branch November 24, 2019 12:22
ilevkivskyi pushed a commit to python/typing that referenced this pull request Nov 24, 2019
jacobneiltaylor pushed a commit to jacobneiltaylor/cpython that referenced this pull request Dec 5, 2019
This patch enables downstream projects inspecting a TypedDict subclass at runtime to tell which keys are optional.

This is essential for generating test data with Hypothesis or validating inputs with typeguard or pydantic.
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
This patch enables downstream projects inspecting a TypedDict subclass at runtime to tell which keys are optional.

This is essential for generating test data with Hypothesis or validating inputs with typeguard or pydantic.
@agronholm
Copy link
Contributor

@Zac-HD I don't think this was intended:

>>> DummyDict = TypedDict('DummyDict', {'x': int, 'y': str}, total=False)
>>> DummyDict.__required_keys__
frozenset({'x', 'y'})

__required_keys__ turns out the same regardless of whether total is False or True.
It's unfortunate that this got in the final release.

@agronholm
Copy link
Contributor

The bug is not really your fault though; It happens because def TypedDict(typename, fields=None, /, *, total=True, **kwargs): does not pass total to _TypedDictMeta(). It still passes __total__ in the attribute namespace, but your code does not check that. A test against the older syntax should have been included.

@agronholm
Copy link
Contributor

Created https://bugs.python.org/issue42059.

JelleZijlstra pushed a commit to python/typing_extensions that referenced this pull request May 19, 2022
JelleZijlstra pushed a commit to python/typing_extensions that referenced this pull request May 19, 2022
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.

None yet

6 participants