8000 bpo-36542: Allow to overwrite the signature for Python functions. (GH… · lisroach/cpython@d53cf99 · GitHub
[go: up one dir, main page]

Skip to content

Commit d53cf99

Browse files
bpo-36542: Allow to overwrite the signature for Python functions. (pythonGH-12705)
1 parent 96aeaec commit d53cf99

File tree

17 files changed

+40
-3
lines changed

17 files changed

+40
-3
lines changed

Lib/bdb.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ def runcall(*args, **kwds):
649649
self.quitting = True
650650
sys.settrace(None)
651651
return res
652+
runcall.__text_signature__ = '($self, func, /, *args, **kwds)'
652653

653654

654655
def set_trace():

Lib/cProfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def runcall(*args, **kw):
124124
return func(*args, **kw)
125125
finally:
126126
self.disable()
127+
runcall.__text_signature__ = '($self, func, /, *args, **kw)'
127128

128129
def __enter__(self):
129130
self.enable()

Lib/collections/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ def __init__(*args, **kwargs):
10181018
self.update(dict)
10191019
if kwargs:
10201020
self.update(kwargs)
1021+
__init__.__text_signature__ = '($self, dict=None, /, **kwargs)'
1022+
10211023
def __len__(self): return len(self.data)
10221024
def __getitem__(self, key):
10231025
if key in self.data:

Lib/concurrent/futures/_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ def submit(*args, **kwargs):
567567
'got %d' % (len(args)-1))
568568

569569
raise NotImplementedError()
570+
submit.__text_signature__ = '($self, fn, /, *args, **kwargs)'
570571

571572
def map(self, fn, *iterables, timeout=None, chunksize=1):
572573
"""Returns an iterator equivalent to map(fn, iter).

Lib/concurrent/futures/process.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ def submit(*args, **kwargs):
630630

631631
self._start_queue_management_thread()
632632
return f
633+
submit.__text_signature__ = _base.Executor.submit.__text_signature__
633634
submit.__doc__ = _base.Executor.submit.__doc__
634635

635636
def map(self, fn, *iterables, timeout=None, chunksize=1):

Lib/concurrent/futures/thread.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def submit(*args, **kwargs):
174174
self._work_queue.put(w)
175175
self._adjust_thread_count()
176176
return f
177+
submit.__text_signature__ = _base.Executor.submit.__text_signature__
177178
submit.__doc__ = _base.Executor.submit.__doc__
178179

179180
def _adjust_thread_count(self):

Lib/contextlib.py

9E88
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ def callback(*args, **kwds):
454454
_exit_wrapper.__wrapped__ = callback
455455
self._push_exit_callback(_exit_wrapper)
456456
return callback # Allow use as a decorator
457+
callback.__text_signature__ = '($self, callback, /, *args, **kwds)'
457458

458459
def _push_cm_exit(self, cm, cm_exit):
459460
"""Helper to correctly register callbacks to __exit__ methods."""
@@ -615,6 +616,7 @@ def push_async_callback(*args, **kwds):
615616
_exit_wrapper.__wrapped__ = callback
616617
self._push_exit_callback(_exit_wrapper, False)
617618
return callback # Allow use as a decorator
619+
push_async_callback.__text_signature__ = '($self, callback, /, *args, **kwds)'
618620

619621
async def aclose(self):
620622
"""Immediately unwind the context stack."""

Lib/curses/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,4 @@ def wrapper(*args, **kwds):
110110
echo()
111111
nocbreak()
112112
endwin()
113+
wrapper.__text_signature__ = '(func, /, *args, **kwds)'

Lib/functools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ def __init__(*args, **keywords):
388388
self.func = func
389389
self.args = args
390390
self.keywords = keywords
391+
__init__.__text_signature__ = '($self, func, /, *args, **keywords)'
391392

392393
def __repr__(self):
393394
args = ", ".join(map(repr, self.args))

Lib/inspect.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ def _signature_from_builtin(cls, func, skip_bound_arg=True):
21212121
return _signature_fromstr(cls, func, s, skip_bound_arg)
21222122

21232123

2124-
def _signature_from_function(cls, func):
2124+
def _signature_from_function(cls, func, skip_bound_arg=True):
21252125
"""Private helper: constructs Signature for the given python function."""
21262126

21272127
is_duck_function = False
@@ -2133,6 +2133,10 @@ def _signature_from_function(cls, func):
21332133
# of pure function:
21342134
raise TypeError('{!r} is not a Python function'.format(func))
21352135

2136+
s = getattr(func, "__text_signature__", None)
2137+
if s:
2138+
return _signature_fromstr(cls, func, s, skip_bound_arg)
2139+
21362140
Parameter = cls._parameter_cls
21372141

21382142
# Parameter information.
@@ -2301,7 +2305,8 @@ def _signature_from_callable(obj, *,
23012305
if isfunction(obj) or _signature_is_functionlike(obj):
23022306
# If it's a pure Python function, or an object that is duck type
23032307
# of a Python function (Cython functions, for instance), then:
2304-
return _signature_from_function(sigcls, obj)
2308+
return _signature_from_function(sigcls, obj,
2309+
skip_bound_arg=skip_bound_arg)
23052310

23062311
if _signature_is_builtin(obj):
23072312
return _signature_from_builtin(sigcls, obj,

0 commit comments

Comments
 (0)
0