-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Suggested cross-reference syntax for attributes should be :py:attr:
#234
Comments
I realize now that "module", "function", and others use the directive string rather than the cross-reference role string, so I guess I'm observing a missing feature rather than an isolated bug. By far, the easiest solution I can think of is a simple look-up table when composing the suggestion, but this would have to be updated as new index types are added to Sphinx. Still, it should be an easy enough patch, if you would like me to submit one. |
Actually, a static table would be unwieldy, but a table could be derived more easily than I thought by inspecting the This implies there might already be an API for this. Please let me know if this is a contribution you would be interested in, and whether you would object to a dependency on |
Yep, the
Definitely, as long as we can figure out a solid way to do it.
Unfortunately, this is a non-starter, at least as a core dependency -- Sphinx is way too big, and AFAIK you can't get I don't know if this would really provide a lot of benefit, though, because I think you'd have to know which version of Sphinx was used to create a given
Maybe, maybe not. Sphinx narrows down the roles it puts into the
I would think that, at least for the I could picture something more complex possibly working to get the Ultimately, it seems like a lot of work to fix a relatively minor inconvenience, which is mostly addressed with the caveat expressed to the user of 'these are the directive forms, not the role forms'. One thing that would be easy to do is update the language on the |
I don't think you need to know which version of sphinx produced the There is also an issue in that both roles and directives can be aliased or otherwise not map one-to-one. Both import importlib.resources
from pathlib import Path
from sphinx.domains import Domain
for module_name in [str(resource).split('.')[0] for resource in importlib.resources.contents('sphinx.domains') if str(resource).endswith('.py')]:
module = importlib.import_module(f'.{module_name}', package='sphinx.domains')
for item in [getattr(module, attr) for attr in dir(module)]:
if isinstance(item, type) and issubclass(item, Domain):
for directive, objType in item.object_types.items():
domain = item.name
for role in objType.roles:
print(f'{domain}:{directive} -> {domain}:{role}') However, remarkably, the first named role seems to be fairly consistently the best choice. In the few cases where it isn't, the best choice is usually has a role that is a substring of the directive. import importlib.resources
from pathlib import Path
from sphinx.domains import Domain
def map_directives_to_roles():
for module_name in [str(resource).split('.')[0] for resource in importlib.resources.contents('sphinx.domains') if str(resource).endswith('.py')]:
module = importlib.import_module(f'.{module_name}', package='sphinx.domains')
for item in [getattr(module, attr) for attr in dir(module)]:
if isinstance(item, type) and issubclass(item, Domain):
for directive, objType in item.object_types.items():
domain = item.name
shortnames = [role for role in objType.roles if directive.startswith(role)]
if len(shortnames) == 1:
yield (f'{domain}:{directive}', f':{domain}:{shortnames[0]}:')
else:
yield (f'{domain}:{directive}', f':{domain}:{objType.roles[0]}:')
mapping = dict(map_directives_to_roles()) produces the following translation table.
This table could be embedded, used for translation if the |
Hmmm, nod... and, the I like the idea of a So, yeah -- if you're interested in assembling a PR based on this mapping table approach, I'd say go for it, I would be inclined to merge. It would be good to sand off the rough edge of the |
I had a few thoughts this morning on some preferences for this implementation:
|
Brief description
Suggested syntax for Python attributes is
:py:attribute:
, but should be:py:attr:
To reproduce
Example: https://github.com/bskinn/sphobjinv/blob/main/doc/source/levenshtein.rst#L192
Reference
https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#role-py-attr
The text was updated successfully, but these errors were encountered: