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

Type annotations using collections.abc.Callable not working ok #457

Closed
chiuczek opened this issue Mar 18, 2025 · 3 comments
Closed

Type annotations using collections.abc.Callable not working ok #457

chiuczek opened this issue Mar 18, 2025 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@chiuczek
Copy link

According to the Python docs, typing.Callable is deprecated for type hinting, in favour of using collections.abc.Callable. When I replaced this usage in some of my code, pdoc3 fails to produce documentation. I have a minimal example which shows the issue (this was running on Python 3.10.16 with pdoc3 0.11.5)

from collections.abc import Callable
from typing import Callable as CallableTyping


def test_fn(
    in1: str, in2: str, in3: str|None
) -> Callable[[str], str]:
    """Test function"""
    def returned_fn(iput: str) -> str:
        print(iput)
        return f"Printed {iput}"

    return returned_fn

def test_fn_2(
    in1: str, in2: str, in3: str|None
) -> CallableTyping[[str], str]:
    """Test function"""
    def returned_fn2(iput: str) -> str:
        print(iput)
        return f"Printed {iput}"

    return returned_fn2

The first function will trip pdoc3 up, with the error TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got (<class 'str'>,), while the second will be fine.

@chiuczek
Copy link
Author

Full error traceback:


Traceback (most recent call last):
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 156, in _render_template
    return t.render(**config).strip()
  File "workdir/.venv/lib/python3.10/site-packages/mako/template.py", line 434, in render
    return runtime._render(self, self.callable_, args, data)
  File "workdir/.venv/lib/python3.10/site-packages/mako/runtime.py", line 874, in _render
    _render_context(
  File "workdir/.venv/lib/python3.10/site-packages/mako/runtime.py", line 916, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "workdir/.venv/lib/python3.10/site-packages/mako/runtime.py", line 943, in _exec_template
    callable_(context, *args, **kwargs)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/templates/html.mako", line 451, in render_body
    ${show_module(module)}
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/templates/html.mako", line 0, in show_module
    
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/templates/html.mako", line 171, in render_show_module
    ${show_func(f)}
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/templates/html.mako", line 114, in show_func
    return_type = get_annotation(f.return_annotation, '\N{non-breaking hyphen}>')
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/templates/html.mako", line 22, in get_annotation
    annot = show_type_annotations and bound_method(link=link) or ''
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1469, in return_annotation
    s = _formatannotation(annot)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1294, in wrapper
    return func(*args)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1367, in _formatannotation
    return str(inspect.formatannotation(maybe_replace_reprs(annot)))
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1358, in maybe_replace_reprs
    a = a.__origin__[(args[:-1], args[-1])]
  File "/homedir/.pyenv/versions/3.10.16/lib/python3.10/_collections_abc.py", line 436, in __new__
    raise TypeError(f"Expected a list of types, an ellipsis, "
TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got (<class 'str'>,)

Traceback (most recent call last):
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1355, in maybe_replace_reprs
    a = a.__origin__[args]
  File "/homedir/.pyenv/versions/3.10.16/lib/python3.10/_collections_abc.py", line 436, in __new__
    raise TypeError(f"Expected a list of types, an ellipsis, "
TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'str'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1292, in wrapper
    return cached_func(*args)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1367, in _formatannotation
    return str(inspect.formatannotation(maybe_replace_reprs(annot)))
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1358, in maybe_replace_reprs
    a = a.__origin__[(args[:-1], args[-1])]
  File "/homedir/.pyenv/versions/3.10.16/lib/python3.10/_collections_abc.py", line 436, in __new__
    raise TypeError(f"Expected a list of types, an ellipsis, "
TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got (<class 'str'>,)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1355, in maybe_replace_reprs
    a = a.__origin__[args]
  File "/homedir/.pyenv/versions/3.10.16/lib/python3.10/_collections_abc.py", line 436, in __new__
    raise TypeError(f"Expected a list of types, an ellipsis, "
TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'str'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "workdir/.venv/bin/pdoc3", line 8, in <module>
    sys.exit(main())
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/cli.py", line 609, in main
    recursive_write_files(module, ext='.html', **template_config)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/cli.py", line 348, in recursive_write_files
    f.write(m.html(**kwargs))
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 908, in html
    html = _render_template('/html.mako', module=self, **kwargs)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 156, in _render_template
    return t.render(**config).strip()
  File "workdir/.venv/lib/python3.10/site-packages/mako/template.py", line 434, in render
    return runtime._render(self, self.callable_, args, data)
  File "workdir/.venv/lib/python3.10/site-packages/mako/runtime.py", line 874, in _render
    _render_context(
  File "workdir/.venv/lib/python3.10/site-packages/mako/runtime.py", line 916, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "workdir/.venv/lib/python3.10/site-packages/mako/runtime.py", line 943, in _exec_template
    callable_(context, *args, **kwargs)
  File "_html_mako", line 145, in render_body
  File "_html_mako", line 34, in show_module
  File "_html_mako", line 421, in render_show_module
  File "_html_mako", line 333, in show_func
  File "_html_mako", line 73, in get_annotation
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1469, in return_annotation
    s = _formatannotation(annot)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1294, in wrapper
    return func(*args)
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1367, in _formatannotation
    return str(inspect.formatannotation(maybe_replace_reprs(annot)))
  File "workdir/.venv/lib/python3.10/site-packages/pdoc/__init__.py", line 1358, in maybe_replace_reprs
    a = a.__origin__[(args[:-1], args[-1])]
  File "/homedir/.pyenv/versions/3.10.16/lib/python3.10/_collections_abc.py", line 436, in __new__
    raise TypeError(f"Expected a list of types, an ellipsis, "
TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got (<class 'str'>,)

@chiuczek
Copy link
Author

Ah, I seem to have re-encountered the bug that was fixed with #454
I can close the issue

@kernc kernc added the duplicate This issue or pull request already exists label Mar 20, 2025
@kernc
Copy link
Member

kernc commented Mar 20, 2025

Fix released. Thanks! 🥂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Development

No branches or pull requests

2 participants