Skip to content

Commit 1bb95b4

Browse files
committed
BUG: Fix crash on annotating something as typing.Callable
Probably related: https://bugs.python.org/issue42195 Traceback: ... File "~/pdoc/pdoc/__init__.py", line 1296, in _formatannotation return str(inspect.formatannotation(maybe_replace_reprs(annot))) File "python3.7/inspect.py", line 1194, in formatannotation return repr(annotation).replace('typing.', '') File "python3.7/typing.py", line 648, in __repr__ return (f'typing.Callable' IndexError: tuple index out of range
1 parent f49faf0 commit 1bb95b4

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pdoc/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,11 @@ def maybe_replace_reprs(a):
12851285
# nptyping.types._ndarray.NDArray -> NDArray[(Any,), Int[64]] # GH-231
12861286
if module.startswith('nptyping.'):
12871287
return force_repr(repr(a))
1288-
# Recurse into args
1288+
# Recurse into typing.Callable/etc. args
12891289
if hasattr(a, 'copy_with') and hasattr(a, '__args__'):
1290+
if a is typing.Callable:
1291+
# Bug on Python < 3.9, https://bugs.python.org/issue42195
1292+
return a
12901293
a = a.copy_with(tuple([maybe_replace_reprs(arg) for arg in a.__args__]))
12911294
return a
12921295

pdoc/test/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,14 @@ def bug253_newtype_annotation(a: CustomType):
902902
pdoc.Function('bug253', mod, bug253_newtype_annotation).params(annotate=True),
903903
['a:\N{NBSP}CustomType'])
904904

905+
# typing.Callable bug
906+
def f(a: typing.Callable):
907+
return
908+
909+
self.assertEqual(
910+
pdoc.Function('f', mod, f).params(annotate=True),
911+
['a:\N{NBSP}Callable'])
912+
905913
# builtin callables with signatures in docstrings
906914
from itertools import repeat
907915
self.assertEqual(pdoc.Function('repeat', mod, repeat).params(), ['object', 'times'])

0 commit comments

Comments
 (0)