Skip to content

Introducing CallableParametersVariable #636

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion typing_extensions/src_py2/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import types
from unittest import TestCase, main, skipUnless

from typing_extensions import NoReturn, ClassVar, Final, IntVar, Literal, TypedDict
from typing_extensions import NoReturn, CallableParametersVariable, ClassVar, Final, IntVar, Literal, TypedDict
from typing_extensions import ContextManager, Counter, Deque, DefaultDict
from typing_extensions import NewType, overload, Protocol, runtime
import typing
Expand Down Expand Up @@ -78,6 +78,20 @@ def test_cannot_instantiate(self):
type(NoReturn)()


class CallableParametersVariableTests(BaseTestCase):
def test_valid(self):
T_params = CallableParametersVariable("T_params")
f_type = typing.Callable[T_params, int]

def test_invalid(self):
with self.assertRaises(TypeError):
T_params = CallableParametersVariable("T_params", int)
with self.assertRaises(TypeError):
T_params = CallableParametersVariable("T_params", bound=int)
with self.assertRaises(TypeError):
T_params = CallableParametersVariable("T_params", covariant=True)


class ClassVarTests(BaseTestCase):

def test_basics(self):
Expand Down
29 changes: 29 additions & 0 deletions typing_extensions/src_py2/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'DefaultDict',

# One-off things.
'CallableParametersVariable'
'final',
'IntVar',
'Literal',
Expand Down Expand Up @@ -70,6 +71,34 @@ def __subclasscheck__(self, cls):
NoReturn = _NoReturn(_root=True)


def CallableParametersVariable(name):
"""This kind of type variable captures callable parameter specifications
instead of types, allowing the typing of decorators which transform the
return type of the given callable. For example:

from typing import TypeVar, Callable, List
from typing_extensions import CallableParametersVariable
Tparams = CallableParametersVariable("Tparams")
Treturn = TypeVar("Treturn")

def unwrap(
f: Callable[Tparams, List[Treturn],
Copy link
Member

Choose a reason for hiding this comment

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

Should be: f: Callable[Tparams, List[Treturn]],

) -> Callable[Tparams, Treturn]: ...

@unwrap
def foo(x: int, y: str, z: bool = False) -> List[int]:
return [1, 2, 3]

decorates foo into a callable that returns int, but still has the same
parameters, including their names and whether they are required.

The empty list is required for backwards compatibility with the runtime
implementation for callables, which requires the first argument to be
a list of types
"""
return []


T_co = typing.TypeVar('T_co', covariant=True)

if hasattr(typing, 'ContextManager'):
Expand Down
16 changes: 15 additions & 1 deletion typing_extensions/src_py3/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Generic
from typing import get_type_hints
from typing import no_type_check
from typing_extensions import NoReturn, ClassVar, Final, IntVar, Literal, Type, NewType, TypedDict
from typing_extensions import NoReturn, CallableParametersVariable, ClassVar, Final, IntVar, Literal, Type, NewType, TypedDict
try:
from typing_extensions import Protocol, runtime
except ImportError:
Expand Down Expand Up @@ -129,6 +129,20 @@ def test_cannot_instantiate(self):
type(NoReturn)()


class CallableParametersVariableTests(BaseTestCase):
def test_valid(self):
T_params = CallableParametersVariable("T_params")
f_type = typing.Callable[T_params, int]

def test_invalid(self):
with self.assertRaises(TypeError):
T_params = CallableParametersVariable("T_params", int)
with self.assertRaises(TypeError):
T_params = CallableParametersVariable("T_params", bound=int)
with self.assertRaises(TypeError):
T_params = CallableParametersVariable("T_params", covariant=True)


class ClassVarTests(BaseTestCase):

def test_basics(self):
Expand Down
29 changes: 29 additions & 0 deletions typing_extensions/src_py3/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def _check_methods_in_mro(C, *methods):
'DefaultDict',

# One-off things.
'CallableParametersVariable'
'final',
'IntVar',
'Literal',
Expand Down Expand Up @@ -212,6 +213,34 @@ def stop() -> NoReturn:
T_contra = typing.TypeVar('T_contra', contravariant=True) # Ditto contravariant.


def CallableParametersVariable(name):
"""This kind of type variable captures callable parameter specifications
instead of types, allowing the typing of decorators which transform the
return type of the given callable. For example:

from typing import TypeVar, Callable, List
from typing_extensions import CallableParametersVariable
Tparams = CallableParametersVariable("Tparams")
Treturn = TypeVar("Treturn")

def unwrap(
f: Callable[Tparams, List[Treturn],
Copy link
Member

Choose a reason for hiding this comment

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

Should be f: Callable[Tparams, List[Treturn]],

) -> Callable[Tparams, Treturn]: ...

@unwrap
def foo(x: int, y: str, z: bool = False) -> List[int]:
return [1, 2, 3]

decorates foo into a callable that returns int, but still has the same
parameters, including their names and whether they are required.

The empty list is required for backwards compatibility with the runtime
implementation for callables, which requires the first argument to be
a list of types
"""
return []


if hasattr(typing, 'ClassVar'):
ClassVar = typing.ClassVar
elif hasattr(typing, '_FinalTypingBase'):
Expand Down