summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-08-11 17:51:36 (GMT)
committerGitHub <noreply@github.com>2023-08-11 17:51:36 (GMT)
commita39f0a350662f1978104ee1136472d784aa6f29c (patch)
treea13f5ab25aa3b11743d7d146524887dee88c1805 /Lib/test
parent5f7d4ecf301ef12eb1d1d347add054f4fcd8fc5c (diff)
downloadcpython-a39f0a350662f1978104ee1136472d784aa6f29c.zip
cpython-a39f0a350662f1978104ee1136472d784aa6f29c.tar.gz
cpython-a39f0a350662f1978104ee1136472d784aa6f29c.tar.bz2
gh-107782: Pydoc: fall back to __text_signature__ if inspect.signature() fails (GH-107786)
It allows to show signatures which are not representable in Python, e.g. for getattr and dict.pop.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pydoc.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
index 8df8b60..fe4e37d 100644
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -1230,6 +1230,60 @@ class TestDescriptions(unittest.TestCase):
self.assertEqual(self._get_summary_line(dict.__class_getitem__),
"__class_getitem__(object, /) method of builtins.type instance")
+ def test_module_level_callable_unrepresentable_default(self):
+ self.assertEqual(self._get_summary_line(getattr),
+ "getattr(object, name, default=<unrepresentable>, /)")
+
+ def test_builtin_staticmethod_unrepresentable_default(self):
+ self.assertEqual(self._get_summary_line(str.maketrans),
+ "maketrans(x, y=<unrepresentable>, z=<unrepresentable>, /)")
+
+ def test_unbound_builtin_method_unrepresentable_default(self):
+ self.assertEqual(self._get_summary_line(dict.pop),
+ "pop(self, key, default=<unrepresentable>, /)")
+
+ def test_bound_builtin_method_unrepresentable_default(self):
+ self.assertEqual(self._get_summary_line({}.pop),
+ "pop(key, default=<unrepresentable>, /) "
+ "method of builtins.dict instance")
+
+ def test_overridden_text_signature(self):
+ class C:
+ def meth(*args, **kwargs):
+ pass
+ @classmethod
+ def cmeth(*args, **kwargs):
+ pass
+ @staticmethod
+ def smeth(*args, **kwargs):
+ pass
+ for text_signature, unbound, bound in [
+ ("($slf)", "(slf, /)", "()"),
+ ("($slf, /)", "(slf, /)", "()"),
+ ("($slf, /, arg)", "(slf, /, arg)", "(arg)"),
+ ("($slf, /, arg=<x>)", "(slf, /, arg=<x>)", "(arg=<x>)"),
+ ("($slf, arg, /)", "(slf, arg, /)", "(arg, /)"),
+ ("($slf, arg=<x>, /)", "(slf, arg=<x>, /)", "(arg=<x>, /)"),
+ ("(/, slf, arg)", "(/, slf, arg)", "(/, slf, arg)"),
+ ("(/, slf, arg=<x>)", "(/, slf, arg=<x>)", "(/, slf, arg=<x>)"),
+ ("(slf, /, arg)", "(slf, /, arg)", "(arg)"),
+ ("(slf, /, arg=<x>)", "(slf, /, arg=<x>)", "(arg=<x>)"),
+ ("(slf, arg, /)", "(slf, arg, /)", "(arg, /)"),
+ ("(slf, arg=<x>, /)", "(slf, arg=<x>, /)", "(arg=<x>, /)"),
+ ]:
+ with self.subTest(text_signature):
+ C.meth.__text_signature__ = text_signature
+ self.assertEqual(self._get_summary_line(C.meth),
+ "meth" + unbound)
+ self.assertEqual(self._get_summary_line(C().meth),
+ "meth" + bound + " method of test.test_pydoc.C instance")
+ C.cmeth.__func__.__text_signature__ = text_signature
+ self.assertEqual(self._get_summary_line(C.cmeth),
+ "cmeth" + bound + " method of builtins.type instance")
+ C.smeth.__text_signature__ = text_signature
+ self.assertEqual(self._get_summary_line(C.smeth),
+ "smeth" + unbound)
+
@requires_docstrings
def test_staticmethod(self):
class X: