-
Notifications
You must be signed in to change notification settings - Fork 294
Add PEP 613 TypeAlias to typing_extensions #732
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
Changes from 1 commit
ccc906e
37eb9da
3b0f2d2
e3a94de
d48428d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,5 +241,43 @@ class Annotated(object): | |
| __slots__ = () | ||
|
|
||
|
|
||
| class _TypeAliasMeta(typing.TypingMeta): | ||
| """Metaclass for TypeAlias""" | ||
|
|
||
| def __new__(cls, name, bases, namespace): | ||
| cls.assert_no_subclassing(bases) | ||
| self = super(_TypeAliasMeta, cls).__new__(cls, name, bases, namespace) | ||
| return self | ||
|
|
||
| def __repr__(self): | ||
| return 'typing_extensions.TypeAlias' | ||
|
|
||
|
|
||
| class _TypeAliasBase(typing._FinalTypingBase): | ||
| """Special marker indicating that an assignment should | ||
| be recognized as a proper type alias definition by type | ||
| checkers. | ||
|
|
||
| For example:: | ||
|
|
||
| Predicate = Callable[[...], bool] # type: TypeAlias | ||
|
|
||
| It's invalid when used anyhow but as in the example above. | ||
|
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. anyhow but -> anywhere except |
||
| """ | ||
| __metaclass__ = _TypeAliasMeta | ||
| __slots__ = () | ||
|
|
||
| def __instancecheck__(self, obj): | ||
| raise TypeError("TypeAlias cannot be used with isinstance().") | ||
|
|
||
| def __subclasscheck__(self, cls): | ||
| raise TypeError("TypeAlias cannot be used with issubclass().") | ||
|
|
||
| def __repr__(self): | ||
| return 'typing_extensions.TypeAlias' | ||
|
|
||
|
|
||
| TypeAlias = _TypeAliasBase(_root=True) | ||
|
|
||
| # This alias exists for backwards compatibility. | ||
| runtime = runtime_checkable | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2071,3 +2071,100 @@ def get_args(tp): | |
| res = (list(res[:-1]), res[-1]) | ||
| return res | ||
| return () | ||
|
|
||
|
|
||
| if hasattr(typing, 'TypeAlias'): | ||
| TypeAlias = typing.TypeAlias | ||
| elif sys.version_info[:2] >= (3, 9): | ||
| class _TypeAliasForm(typing._SpecialForm, _root=True): | ||
| def __repr__(self): | ||
| return 'typing_extensions.' + self._name | ||
|
|
||
| @_TypeAliasForm | ||
| def TypeAlias(self, parameters): | ||
| """Special marker indicating that an assignment should | ||
| be recognized as a proper type alias definition by type | ||
| checkers. | ||
|
|
||
| For example:: | ||
|
|
||
| Predicate: TypeAlias = Callable[[...], bool] | ||
|
|
||
| It's invalid when used anyhow but as in the example above. | ||
|
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. anyhow as -> anywhere except |
||
| """ | ||
| raise TypeError("{} is not subscriptable".format(self)) | ||
|
|
||
| elif sys.version_info[:2] >= (3, 7): | ||
| class _TypeAliasForm(typing._SpecialForm, _root=True): | ||
| def __repr__(self): | ||
| return 'typing_extensions.' + self._name | ||
|
|
||
|
|
||
| TypeAlias = _TypeAliasForm('TypeAlias', doc= | ||
| """Special marker indicating that an assignment should | ||
| be recognized as a proper type alias definition by type | ||
| checkers. | ||
|
|
||
| For example:: | ||
|
|
||
| Predicate: TypeAlias = Callable[[...], bool] | ||
|
|
||
| It's invalid when used anyhow but as in the example above. | ||
| """) | ||
|
|
||
| elif hasattr(typing, '_FinalTypingBase'): | ||
| class _TypeAliasMeta(typing.TypingMeta): | ||
| """Metaclass for TypeAlias""" | ||
|
|
||
| def __repr__(self): | ||
| return 'typing_extensions.TypeAlias' | ||
|
|
||
| class _TypeAliasBase(typing._FinalTypingBase, metaclass=_TypeAliasMeta, _root=True): | ||
| """Special marker indicating that an assignment should | ||
| be recognized as a proper type alias definition by type | ||
| checkers. | ||
|
|
||
| For example:: | ||
|
|
||
| Predicate: TypeAlias = Callable[[...], bool] | ||
|
|
||
| It's invalid when used anyhow but as in the example above. | ||
|
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. (again) |
||
| """ | ||
| __slots__ = () | ||
|
|
||
| def __instancecheck__(self, obj): | ||
| raise TypeError("TypeAlias cannot be used with isinstance().") | ||
|
|
||
| def __subclasscheck__(self, cls): | ||
| raise TypeError("TypeAlias cannot be used with issubclass().") | ||
|
|
||
| def __repr__(self): | ||
| return 'typing_extensions.TypeAlias' | ||
|
|
||
| TypeAlias = _TypeAliasBase(_root=True) | ||
| else: | ||
| class _TypeAliasMeta(typing.TypingMeta): | ||
| """Metaclass for TypeAlias""" | ||
|
|
||
| def __instancecheck__(self, obj): | ||
| raise TypeError("TypeAlias cannot be used with isinstance().") | ||
|
|
||
| def __subclasscheck__(self, cls): | ||
| raise TypeError("TypeAlias cannot be used with issubclass().") | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| raise TypeError("Cannot instantiate TypeAlias") | ||
|
|
||
|
|
||
| class TypeAlias(metaclass=_TypeAliasMeta, _root=True): | ||
| """Special marker indicating that an assignment should | ||
| be recognized as a proper type alias definition by type | ||
| checkers. | ||
|
|
||
| For example:: | ||
|
|
||
| Predicate: TypeAlias = Callable[[...], bool] | ||
|
|
||
| It's invalid when used anyhow but as in the example above. | ||
|
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. (ditto) |
||
| """ | ||
| __slots__ = () | ||
Uh oh!
There was an error while loading. Please reload this page.