-
Notifications
You must be signed in to change notification settings - Fork 302
Add PEP 655 Required and NotRequired to typing_extensions #807
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
034ee82
Add PEP 655 Required and NotRequired to typing_extensions
davidfstr 2a673a5
Fix f-string errors on Python <3.6
davidfstr 52a0946
Inline _REQUIRED_DOC and _NOT_REQUIRED_DOC
davidfstr 67cb26d
Remove Python 3.5.0-3.5.2 support for Required. Test NotRequired.
davidfstr e6cd764
Take 2: Remove Python 3.5.0-3.5.2 support for Required.
davidfstr 9da738d
Merge branch 'master' into f/required
srittau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2844,3 +2844,163 @@ def is_str(val: Union[str, float]): | |
| PEP 647 (User-Defined Type Guards). | ||
| """ | ||
| __type__ = None | ||
|
|
||
|
|
||
| if hasattr(typing, 'Required'): | ||
| Required = typing.Required | ||
| NotRequired = typing.NotRequired | ||
| elif sys.version_info[:2] >= (3, 9): | ||
| class _ExtensionsSpecialForm(typing._SpecialForm, _root=True): | ||
| def __repr__(self): | ||
| return 'typing_extensions.' + self._name | ||
|
|
||
| @_ExtensionsSpecialForm | ||
| def Required(self, parameters): | ||
| """A special typing construct to mark a key of a total=False TypedDict | ||
| as required. For example: | ||
|
|
||
| class Movie(TypedDict, total=False): | ||
| title: Required[str] | ||
| year: int | ||
|
|
||
| m = Movie( | ||
| title='The Matrix', # typechecker error if key is omitted | ||
| year=1999, | ||
| ) | ||
|
|
||
| There is no runtime checking that a required key is actually provided | ||
| when instantiating a related TypedDict. | ||
| """ | ||
| item = typing._type_check(parameters, '{} accepts only single type'.format(self._name)) | ||
| return typing._GenericAlias(self, (item,)) | ||
|
|
||
| @_ExtensionsSpecialForm | ||
| def NotRequired(self, parameters): | ||
| """A special typing construct to mark a key of a TypedDict as | ||
| potentially missing. For example: | ||
|
|
||
| class Movie(TypedDict): | ||
| title: str | ||
| year: NotRequired[int] | ||
|
|
||
| m = Movie( | ||
| title='The Matrix', # typechecker error if key is omitted | ||
| year=1999, | ||
| ) | ||
| """ | ||
| item = typing._type_check(parameters, '{} accepts only single type'.format(self._name)) | ||
| return typing._GenericAlias(self, (item,)) | ||
| elif sys.version_info[:2] >= (3, 7): | ||
| class _RequiredForm(typing._SpecialForm, _root=True): | ||
| def __repr__(self): | ||
| return 'typing_extensions.' + self._name | ||
|
|
||
| def __getitem__(self, parameters): | ||
| item = typing._type_check(parameters, | ||
| '{} accepts only single type'.format(self._name)) | ||
| return _GenericAlias(self, (item,)) | ||
|
|
||
| Required = _RequiredForm('Required', doc= | ||
| """A special typing construct to mark a key of a total=False TypedDict | ||
| as required. For example: | ||
|
|
||
| class Movie(TypedDict, total=False): | ||
| title: Required[str] | ||
| year: int | ||
|
|
||
| m = Movie( | ||
| title='The Matrix', # typechecker error if key is omitted | ||
| year=1999, | ||
| ) | ||
|
|
||
| There is no runtime checking that a required key is actually provided | ||
| when instantiating a related TypedDict. | ||
| """) | ||
| NotRequired = _RequiredForm('NotRequired', doc= | ||
| """A special typing construct to mark a key of a TypedDict as | ||
| potentially missing. For example: | ||
|
|
||
| class Movie(TypedDict): | ||
| title: str | ||
| year: NotRequired[int] | ||
|
|
||
| m = Movie( | ||
| title='The Matrix', # typechecker error if key is omitted | ||
| year=1999, | ||
| ) | ||
| """) | ||
| elif hasattr(typing, '_FinalTypingBase'): | ||
| # NOTE: Modeled after _Final's implementation when _FinalTypingBase available | ||
| class _MaybeRequired(typing._FinalTypingBase, _root=True): | ||
| __slots__ = ('__type__',) | ||
|
|
||
| def __init__(self, tp=None, **kwds): | ||
| self.__type__ = tp | ||
|
|
||
| def __getitem__(self, item): | ||
| cls = type(self) | ||
| if self.__type__ is None: | ||
| return cls(typing._type_check(item, | ||
| '{} accepts only single type.'.format(cls.__name__[1:])), | ||
| _root=True) | ||
| raise TypeError('{} cannot be further subscripted' | ||
| .format(cls.__name__[1:])) | ||
|
|
||
| def _eval_type(self, globalns, localns): | ||
| new_tp = typing._eval_type(self.__type__, globalns, localns) | ||
| if new_tp == self.__type__: | ||
| return self | ||
| return type(self)(new_tp, _root=True) | ||
|
|
||
| def __repr__(self): | ||
| r = super().__repr__() | ||
| if self.__type__ is not None: | ||
| r += '[{}]'.format(typing._type_repr(self.__type__)) | ||
| return r | ||
|
|
||
| def __hash__(self): | ||
| return hash((type(self).__name__, self.__type__)) | ||
|
|
||
| def __eq__(self, other): | ||
| if not isinstance(other, _Final): | ||
| return NotImplemented | ||
| if self.__type__ is not None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems incorrect; it will make Also, could you add tests for |
||
| return self.__type__ == other.__type__ | ||
| return self is other | ||
|
|
||
| class _Required(_MaybeRequired, _root=True): | ||
| """A special typing construct to mark a key of a total=False TypedDict | ||
| as required. For example: | ||
|
|
||
| class Movie(TypedDict, total=False): | ||
| title: Required[str] | ||
| year: int | ||
|
|
||
| m = Movie( | ||
| title='The Matrix', # typechecker error if key is omitted | ||
| year=1999, | ||
| ) | ||
|
|
||
| There is no runtime checking that a required key is actually provided | ||
| when instantiating a related TypedDict. | ||
| """ | ||
|
|
||
| class _NotRequired(_MaybeRequired, _root=True): | ||
| """A special typing construct to mark a key of a TypedDict as | ||
| potentially missing. For example: | ||
|
|
||
| class Movie(TypedDict): | ||
| title: str | ||
| year: NotRequired[int] | ||
|
|
||
| m = Movie( | ||
| title='The Matrix', # typechecker error if key is omitted | ||
| year=1999, | ||
| ) | ||
| """ | ||
|
|
||
| Required = _Required(_root=True) | ||
| NotRequired = _NotRequired(_root=True) | ||
| else: | ||
| # Python 3.5.0 - 3.5.2: Unsupported | ||
| pass | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.