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

gh-73536: Add support of multi-signatures #117671

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
7 changes: 4 additions & 3 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,10 +1162,11 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
try:
# In some cases fetching a signature is not possible.
# But, we surely should not fail in this case.
text_sig = str(inspect.signature(cls)).replace(' -> None', '')
doc = '\n'.join(cls.__name__ + str(sig).replace(' -> None', '')
for sig in inspect.signatures(cls))
except (TypeError, ValueError):
text_sig = ''
cls.__doc__ = (cls.__name__ + text_sig)
doc = cls.__name__
cls.__doc__ = doc

if match_args:
# I could probably compute this once.
Expand Down
17 changes: 11 additions & 6 deletions Lib/idlelib/calltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,25 @@ def get_argspec(ob):

# Initialize argspec and wrap it to get lines.
try:
argspec = str(inspect.signature(fob))
signatures = inspect.signatures(fob)
except Exception as err:
msg = str(err)
if msg.startswith(_invalid_method):
return _invalid_method
else:
argspec = ''
signatures = []

if isinstance(fob, type) and argspec == '()':
lines = []
for sig in signatures:
line = str(sig)
if len(line) > _MAX_COLS:
lines.extend(textwrap.wrap(line, _MAX_COLS, subsequent_indent=_INDENT))
else:
lines.append(line)
if isinstance(fob, type) and lines == ['()']:
# If fob has no argument, use default callable argspec.
argspec = _default_callable_argspec
lines = [_default_callable_argspec]

lines = (textwrap.wrap(argspec, _MAX_COLS, subsequent_indent=_INDENT)
if len(argspec) > _MAX_COLS else [argspec] if argspec else [])

# Augment lines from docstring, if any, and join to get argspec.
doc = inspect.getdoc(ob)
Expand Down
32 changes: 32 additions & 0 deletions Lib/idlelib/idle_test/test_calltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from idlelib import calltip
import unittest
from unittest.mock import Mock
import builtins
import textwrap
import types
import re
Expand Down Expand Up @@ -151,17 +152,48 @@ def f(): pass
"Signature information for builtins requires docstrings")
def test_multiline_docstring(self):
# Test fewer lines than max.
def range():
"""range(stop) -> range object
range(start, stop[, step]) -> range object

Return an object that produces a sequence of integers from start
(inclusive) to stop (exclusive) by step.
"""
range.__text_signature__ = '<no signature>'
self.assertEqual(get_spec(range),
"range(stop) -> range object\n"
"range(start, stop[, step]) -> range object")
self.assertEqual(get_spec(builtins.range),
"(stop, /)\n"
"(start, stop, step=1, /)\n"
"Create a range object.")

# Test max lines
def bytes():
"""bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object

Construct an immutable array of bytes from:
- an iterable yielding integers in range(256)
- a text string encoded using the specified encoding
- any object implementing the buffer API.
- an integer"""
bytes.__text_signature__ = '<no signature>'
self.assertEqual(get_spec(bytes), '''\
bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object''')
self.assertEqual(get_spec(builtins.bytes), '''\
()
(source)
(source, encoding)
(source, encoding, errors)
Construct an immutable array of bytes.''')

def test_multiline_docstring_2(self):
# Test more than max lines
Expand Down
Loading