Skip to content

Commit

Permalink
BUG: Fix crash on annotating something as typing.Callable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kernc committed Aug 3, 2021
1 parent f49faf0 commit 1bb95b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,8 +1285,11 @@ def maybe_replace_reprs(a):
# nptyping.types._ndarray.NDArray -> NDArray[(Any,), Int[64]] # GH-231
if module.startswith('nptyping.'):
return force_repr(repr(a))
# Recurse into args
# Recurse into typing.Callable/etc. args
if hasattr(a, 'copy_with') and hasattr(a, '__args__'):
if a is typing.Callable:
# Bug on Python < 3.9, https://bugs.python.org/issue42195
return a
a = a.copy_with(tuple([maybe_replace_reprs(arg) for arg in a.__args__]))
return a

Expand Down
8 changes: 8 additions & 0 deletions pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,14 @@ def bug253_newtype_annotation(a: CustomType):
pdoc.Function('bug253', mod, bug253_newtype_annotation).params(annotate=True),
['a:\N{NBSP}CustomType'])

# typing.Callable bug
def f(a: typing.Callable):
return

self.assertEqual(
pdoc.Function('f', mod, f).params(annotate=True),
['a:\N{NBSP}Callable'])

# builtin callables with signatures in docstrings
from itertools import repeat
self.assertEqual(pdoc.Function('repeat', mod, repeat).params(), ['object', 'times'])
Expand Down

0 comments on commit 1bb95b4

Please sign in to comment.