mkdocstrings / autorefs

Automatically link across pages in MkDocs.
https://mkdocstrings.github.io/autorefs/
ISC License
51 stars 8 forks source link

bug: AttributeError: 'dict' object has no attribute 'resolve_closest' #57

Closed kumaranvpl closed 2 months ago

kumaranvpl commented 2 months ago

Description of the bug

From version 1.2.0, running mkdocs build command results in the error AttributeError: 'dict' object has no attribute 'resolve_closest'. This issue is not happening in version 1.1.0.

I did some digging around, the error obviously happening at https://github.com/mkdocstrings/autorefs/blob/main/src/mkdocs_autorefs/plugin.py#L178 and the self.config returns an empty dictionary. Even at on_config https://github.com/mkdocstrings/autorefs/blob/main/src/mkdocs_autorefs/plugin.py#L211, the self.config value is an empty dictionary. But printing the self.config in mkdocstrings project at https://github.com/mkdocstrings/mkdocstrings/blob/main/src/mkdocstrings/plugin.py#L137 returns some value.

This could be because of incorrect initialization/usage of config variable. Hopefully above info helps with debugging.

Full traceback

Full traceback ```python File "/home/userdev/project/venv/bin/mkdocs", line 8, in sys.exit(cli()) ^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/click/core.py", line 1157, in __call__ return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/click/core.py", line 1078, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/click/core.py", line 1688, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/click/core.py", line 1434, in invoke return ctx.invoke(self.callback, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/click/core.py", line 783, in invoke return __callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs/__main__.py", line 288, in build_command build.build(cfg, dirty=not clean) File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs/commands/build.py", line 337, in build _build_page( File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs/commands/build.py", line 226, in _build_page output = config.plugins.on_post_page(output, page=page, config=config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs/plugins.py", line 646, in on_post_page return self.run_event('post_page', output, page=page, config=config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs/plugins.py", line 566, in run_event result = method(item, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs_autorefs/plugin.py", line 315, in on_post_page fixed_output, unmapped = fix_refs(output, url_mapper, _legacy_refs=self.legacy_refs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs_autorefs/references.py", line 412, in fix_refs html = AUTOREF_RE.sub(fix_ref(url_mapper, unmapped), html) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs_autorefs/references.py", line 369, in inner url = url_mapper(unescape(identifier)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs_autorefs/plugin.py", line 211, in get_item_url url = self._get_item_url(identifier, fallback, from_url) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/userdev/project/venv/lib/python3.12/site-packages/mkdocs_autorefs/plugin.py", line 185, in _get_item_url if self.config.resolve_closest and from_url is not None: ^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'dict' object has no attribute 'resolve_closest' ```

Environment information

pawamoy commented 2 months ago

Thanks for the report @kumaranvpl. Could you tell me which version of MkDocs you have installed?

kumaranvpl commented 2 months ago

Hello @pawamoy,

I am using following version of libraries:

mkdocs                                    1.6.1
mkdocs-autorefs                           1.2.0
mkdocstrings                              0.26.0
mkdocstrings-python                       1.10.9
pawamoy commented 2 months ago

Thanks!

Unfortunately I am not able to reproduce the issue you describe. Could you set up a minimal reproducible example (zip archive or GitHub project) that I could use to investigate?

beijbom commented 2 months ago

+1 to this. Just happened to me as well. Here is my setup.

griffe                     1.2.0
mkdocs                     1.6.1
mkdocs-autorefs            1.2.0
mkdocs-get-deps            0.2.0
mkdocs-material            9.5.34
mkdocs-material-extensions 1.3.1
mkdocstrings               0.26.0
mkdocstrings-python        1.11.1

The particular line it's complaining about for me is a Type Alias, e.g. MyVariable = str. I can try to pull together a full repo including code, if you can't get it to repro.

pawamoy commented 2 months ago

Thanks @beijbom. Still cannot reproduce though, so yes, a full repo would be great :slightly_smiling_face:

kumaranvpl commented 2 months ago

Thanks for the report @kumaranvpl. Could you tell me which version of MkDocs you have installed?

Hello @pawamoy,

Checkout my repo - https://github.com/kumaranvpl/faststream/tree/main. Please clone it, create a venv(any 3.9+), install dependencies by running pip install -e ".[dev]" and run the script ./scripts/build-docs.sh.

This will throw the error AttributeError: 'dict' object has no attribute 'resolve_closest'. I hope this helps with your debugging.

bls-amid commented 2 months ago

We also encountered this error when upgrading mkdocstrings to v.0.26.0 which included an upgrade of autorefs to 1.2.0

We added the following to the mkdocs.yml as a workaround, but would be nice if it was not necessary

image

pawamoy commented 2 months ago

@kumaranvpl thanks, I'm able to reproduce the error within your repository. However that's far from a minimal reproduction example and that makes it hard to debug. Basically, in your project, self.config is indeed an empty dict, while in my own (smaller) projects, self.config is an instance of mkdocs_autorefs.plugin.AutorefsConfig, which allows attribute access, and has resolve_closest set to False by default. What I don't understand is why the types are different from one project to another. This is why an MRE would be greatly appreciated.

pawamoy commented 2 months ago

OK I found the difference. It's when autorefs is listed or not under plugins (previously missed that because it needed to cross-ref a mkdocstrings object twice to trigger the error, not just a markdown heading). Working on a fix.

pawamoy commented 2 months ago

Fixed by mkdocstrings 0.26.1, which now instantiates the config attribute of the autorefs plugin instance it creates itself.

bls-amid commented 2 months ago

Can confirm that 0.26.1 allows us to remove the aforementioned workaround - thanks for the quick fix!

kumaranvpl commented 2 months ago

@pawamoy Thanks for the quick fix. I can also confirm that with version 0.26.1 my project works. Sorry, I didn't had time to create a MRE.

pawamoy commented 2 months ago

No worries, you were all helpful 😊