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

Add Expand special form #22

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions mypy_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
# NOTE: This module must support Python 2.7 in addition to Python 3.x

import sys
# _type_check is NOT a part of public typing API, it is used here only to mimic
# the (convenient) behavior of types provided by typing module.
from typing import _type_check # type: ignore
# _GenericAlias, _SpecialForm, _tp_cache and _type_check are NOT a part of
# public typing API, they are used here only to mimic the (convenient) behavior
# of types provided by typing module.
from typing import _GenericAlias, _SpecialForm, _tp_cache, _type_check # type: ignore


def _check_fails(cls, other):
Expand Down Expand Up @@ -164,3 +165,32 @@ def __getitem__(self, args):


FlexibleAlias = _FlexibleAliasCls()


class _Expand(_SpecialForm, _root=True):
def __repr__(self):
return 'mypy_extensions.' + self._name

@_tp_cache
def __getitem__(self, parameters):
if parameters == ():
raise TypeError('Cannot use Expand without an argument.')
item = _type_check(parameters, 'Expand accepts only a single TypedDict type.')
return _GenericAlias(self, (item,))


Expand = _Expand('Expand', doc="""Special type indicating the type of ``**kwargs``.
Example::

class Options(TypedDict, total=False):
timeout: int
alternative: str
on_error: Callable[[int], None]
on_timeout: Callable[[], None]
...

def fun(x: int, *, **options: Expand[Options]) -> None:
...

Cannot be used anywhere except with ``**kwargs`` in function definitions.
The only valid argument is of type ``TypedDict``.""")