griffe: Include the location of the mistake in case of a "Could not resolve X" error

Is your feature request related to a problem? Please describe. I’m trying to generate documentation for my project with mkdocstrings, and griffe constantly crashes with weird “Could not resolve X” errors that are impossible to track down and fix.

Describe the solution you’d like The error message should include the exact location of the symbol that couldn’t be resolved.

Additional context This is the error I get running mkdocs serve on my project:

ERROR    -  Error reading page 'api_reference.md': Could not resolve typing._no_init_or_replace_init
Traceback (most recent call last):
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\griffe\dataclasses.py", line 1111, in _resolve_target
    resolved = self.modules_collection[self.target_path]
               ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\griffe\mixins.py", line 28, in __getitem__
    return self.members[parts[0]][parts[1:]]  # type: ignore[attr-defined]
           ~~~~~~~~~~~~^^^^^^^^^^
KeyError: 'typing'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocs\__main__.py", line 301, in <module>
    cli()
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocs\__main__.py", line 234, in serve_command
    serve.serve(dev_addr=dev_addr, livereload=livereload, watch=watch, **kwargs)
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocs\commands\serve.py", line 83, in serve
    builder(config)
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocs\commands\serve.py", line 76, in builder
    build(config, live_server=live_server, dirty=dirty)
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocs\commands\build.py", line 308, in build
    _populate_page(file.page, config, files, dirty)
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocs\commands\build.py", line 181, in _populate_page
    page.render(config, files)
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocs\structure\pages.py", line 270, in render
    self.content = md.convert(self.markdown)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\markdown\core.py", line 264, in convert
    root = self.parser.parseDocument(self.lines).getroot()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\markdown\blockparser.py", line 90, in parseDocument
    self.parseChunk(self.root, '\n'.join(lines))
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\markdown\blockparser.py", line 105, in parseChunk
    self.parseBlocks(parent, text.split('\n\n'))
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\markdown\blockparser.py", line 123, in parseBlocks
    if processor.run(parent, blocks) is not False:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings\extension.py", line 116, in run
    html, handler, data = self._process_block(identifier, block, heading_level)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings\extension.py", line 202, in _process_block
    rendered = handler.render(data, options)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\handler.py", line 255, in render
    return template.render(
           ^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\jinja2\environment.py", line 1301, in render
    self.environment.handle_exception()
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\jinja2\environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\module.html", line 1, in top-level template code
    {% extends "_base/module.html" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\_base\module.html", line 58, in top-level template code
    {% include "children.html" with context %}
^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\children.html", line 1, in top-level template code
    {% extends "_base/children.html" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\_base\children.html", line 45, in top-level template code
    {% include "class.html" with context %}
^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\class.html", line 1, in top-level template code
    {% extends "_base/class.html" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\_base\class.html", line 30, in top-level template code
    {%- include "signature.html" with context -%}
^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\signature.html", line 1, in top-level template code
    {% extends "_base/signature.html" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\mkdocstrings_handlers\python\templates\material\_base\signature.html", line 12, in top-level template code
    {%- for parameter in function.parameters -%}
^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\jinja2\environment.py", line 485, in getattr
    return getattr(obj, attribute)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\griffe\dataclasses.py", line 1035, in parameters
    return cast(Function, self.target).parameters
                          ^^^^^^^^^^^
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\griffe\dataclasses.py", line 1078, in target
    self.resolve_target()
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\griffe\dataclasses.py", line 1105, in resolve_target
    self._resolve_target()
  File "C:\Users\Aran-Fey\AppData\Roaming\Python\Python311\site-packages\griffe\dataclasses.py", line 1113, in _resolve_target
    raise AliasResolutionError(self.target_path) from error
griffe.exceptions.AliasResolutionError: Could not resolve typing._no_init_or_replace_init

My project doesn’t contain typing._no_init_or_replace_init anywhere. I have no idea what’s causing this error or how to fix it. I would like to see a more helpful error message, something along the lines of

Could not resolve “typing._no_init_or_replace_init” in class “Foo” from “my_project/foo.py”

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Oh, huh. Ok, rookie mistake on my part. But I swear I have seen documentation being generated today even without that setting enabled. Nothing makes sense to me anymore…

Unfortunately this particular crash is pretty tricky to reproduce. I still haven’t quite figured it out yet, but this one in particular is probably my own fault (I’m doing something unusual with my PYTHONPATH and other things). I’ll probably open another issue related to that, because there’s something weird about how griffe discovers modules, and it’s making it very difficult for me to debug this.