Skip to content

Commit ba070d9

Browse files
sixoletJukkaL
authored andcommitted
Better callable: Callable[[Arg('x', int), VarArg(str)], int] now a thing you can do (#2607)
Implements an experimental feature to allow Callable to have any kind of signature an actual function definition does. This should enable better typing of callbacks &c. Initial discussion: python/typing#239 Proposal, v. similar to this impl: python/typing#264 Relevant typeshed PR: python/typeshed#793
1 parent 64da8a8 commit ba070d9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

mypy_extensions.py

+36
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from mypy_extensions import TypedDict
66
"""
77

8+
from typing import Any
9+
810
# NOTE: This module must support Python 2.7 in addition to Python 3.x
911

1012
import sys
@@ -92,6 +94,40 @@ class Point2D(TypedDict):
9294
syntax forms work for Python 2.7 and 3.2+
9395
"""
9496

97+
# Argument constructors for making more-detailed Callables. These all just
98+
# return their type argument, to make them complete noops in terms of the
99+
# `typing` module.
100+
101+
102+
def Arg(type=Any, name=None):
103+
"""A normal positional argument"""
104+
return type
105+
106+
107+
def DefaultArg(type=Any, name=None):
108+
"""A positional argument with a default value"""
109+
return type
110+
111+
112+
def NamedArg(type=Any, name=None):
113+
"""A keyword-only argument"""
114+
return type
115+
116+
117+
def DefaultNamedArg(type=Any, name=None):
118+
"""A keyword-only argument with a default value"""
119+
return type
120+
121+
122+
def VarArg(type=Any):
123+
"""A *args-style variadic positional argument"""
124+
return type
125+
126+
127+
def KwArg(type=Any):
128+
"""A **kwargs-style variadic keyword argument"""
129+
return type
130+
95131

96132
# Return type that indicates a function does not return
97133
class NoReturn: pass

0 commit comments

Comments
 (0)