Closed pawamoy closed 2 years ago
Oops I thought this was a pull request 😅 But I'll definitely check this out
Thanks! I'll experiment anyway, and send a PR if I get something elegant :slightly_smiling_face:
we render Class with one of its alias path (there could be many), autorefs maps this alias path to its URL (it should not use the canonical path, because the alias is the public one, that's what the user wants)
Hmm I've certainly never planned for this case, I thought everything should always have unambiguous canonical path. That is, perhaps I straight up disagree with "it should not use the canonical path". If the handler wants to do shenanigans with "canonical aliases", perhaps it should somehow consistently report one chosen path.
the corresponding handler collects pkg.base.Class, and return its canonical path: pkg.base.Class
Well no, the returned canonical path should be pkg.Class
, that's what the user wants.
That's all I have to say for now and probably won't actually be able to look into this in detail. I don't know if this is useful, but maybe it'll lead you to some interesting thought.
Sure, your input is always welcomed :slightly_smiling_face:
That is, perhaps I straight up disagree with "it should not use the canonical path"
I thought about it, and yes, perhaps it could use both, which corresponds to the proposed solutions, but not the current situation.
Well no, the returned canonical path should be pkg.Class, that's what the user wants.
True as well, but considering an object can have multiple aliases, the handler has no way of knowing which one of the aliases the object is registered with.
Of course I've very biased toward Python, which certainly does not mirror other languages concepts. But I guess populating the URL map with more data would not be harmful anyway.
True as well, but considering an object can have multiple aliases, the handler has no way of knowing which one of the aliases the object is registered with.
I'm not sure if this is fully true.
The handler could perhaps memorize the first identifier it was collected through and use that henceforth.
But that would probably be considered more hacky than what you're suggesting..
Yeah there are multiple options here!
Currently, when autorefs doesn't know the URL for an identifier, it falls back to collecting the object through every handler (and getting an anchor through its renderer) until an anchor is found.
It works well when canonical objects are rendered (the ones with canonical paths, i.e. the identifier corresponding to where they are defined, not imported/aliased), but not the other way around.
Example:
pkg.base
definesClass
pkg
importspkg.base.Class
asClass
pkg.Class
is an alias topkg.base.Class
Class
with its canonical path, autorefs maps its canonical path to its URL[Class][pkg.Class]
crossref, it falls back as mentioned abovepkg.Class
, and returns its canonical path:pkg.base.Class
The other way around:
pkg.base
definesClass
pkg
importpkg.base.Class
asClass
pkg.Class
is an alias topkg.base.Class
Class
with one of its alias path (there could be many), autorefs maps this alias path to its URL (it should not use the canonical path, because the alias is the public one, that's what the user wants)[Class][pkg.base.Class]
(which happens, for example, because of Python annotations that are resolved using their actual scope, which is where they are defined, not imported), it falls back as mentioned abovepkg.base.Class
, and return its canonical path:pkg.base.Class
So, what we need here is one of two things, or both:
The option 2 would also prevent redundant, costly fallbacks. Maybe this can already be improved by updating the URL map when finding a new valid anchor/identifier.
Maybe we could call the renderers
get_anchor[s]
method early, inmkdocstrings.extension.AutodocProcessor.run
andmkdocstrings.extension.AutodocProcessor._process_block
, to register more anchors into autorefs.