Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

Intersphinx - RefType - Use objtypes inventory as fallback mechanism #144

Conversation

chrizzFTD
Copy link
Contributor

@chrizzFTD chrizzFTD commented Sep 19, 2021

Hi, thanks a lot for this awesome sphinx extension!

Issue

I came across a small issue where intersphinx class methods would not load hoverxref tooltips on my project at grill.rtfd.io. This was strange since classes from the same intersphinx project would work fine.

Discoveries

On my RST doc file, I refer to the intersphinx grill.names.UsdAsset.get_anonymous method like so:

 :py:meth:`grill.names.UsdAsset.get_anonymous`

For hoverxref, the inventory loaded here:

for inventory_name in app.config.hoverxref_intersphinx:
inventory = inventories.named_inventory.get(inventory_name, {})

Looks like this:

'py:method': {'grill.names.DateTimeFile.get_pattern_list': ('grill.names',
                                                             '2.5',
                                                             'https://grill-names.readthedocs.io/en/latest/names/DateTimeFile.html#grill.names.DateTimeFile.get_pattern_list',
                                                             '-'),
               'grill.names.UsdAsset.get_anonymous': ('grill.names',
                                                      '2.5',
                                                      'https://grill-names.readthedocs.io/en/latest/names/UsdAsset.html#grill.names.UsdAsset.get_anonymous',
                                                      '-')},

If I replace the RST text to use method like so:

 :py:method:`grill.names.UsdAsset.get_anonymous`

The translation fails with a message like:

WARNING: Unknown interpreted text role "py:method".

So it looks like it must be left as py:meth.

Proposal

I've chosen to use a "fallback" mechanism as presented on this PR.

You can see it working on the hint block from this docs:

2021-09-19

If this PR is not needed and there is something different I should do on my side, please let me know!

@astrojuanlu
Copy link
Contributor

cc @humitos

Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for your PR! 🤟🏼

I think this is correct. I haven't tested it yet, tho.

It would be good to have some tests for this case.

Also, are you aware of other synonyms (like py:method and py:meth) that may make sense to add here as well? In that case, we should add them as well.

Also also, do you know if this works for all domains or only for Python (e.g. py:)? In that case, we should restrict it to Python only.

@chrizzFTD
Copy link
Contributor Author

Thanks, @humitos! I'll look into adding some tests for this.

About the other questions, I'm not aware about other synonyms or the python only domain, I'll update the code to handle only python in the meantime :P

@humitos
Copy link
Member

humitos commented Sep 23, 2021

I found another case. :py:mod: is what Sphinx expects, but "intersphinx inventory" from CPython is defined as :py:module:.

humitos added a commit that referenced this pull request Sep 23, 2021
Borrowed from #144
@humitos
Copy link
Member

humitos commented Sep 28, 2021

@chrizzFTD Hi! I included your solution in #146 because I found myself having this issue. However, I didn't finish this work.

It would be good if you have some time to update your PR with the latest changes from master (the code was simplified a lot) and add tests so we can merge this PR and be covered 😄

@chrizzFTD chrizzFTD changed the title Intersphinx - RefType fallback mechanism for inventory reftype meth Intersphinx - RefType - Use objtypes inventory as fallback mechanism Oct 2, 2021
Copy link
Contributor Author

@chrizzFTD chrizzFTD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @humitos, I've updated my branch to take the recent changes from master. I've also taken a new approach by letting fallback keys to be given by the domain for that inventory + tests.

Please let me know what you think!

One thing to note, however, is that it looks like something else might have broken recently as my project grill is no longer able to display tooltips when using this branch. Everything, both internal and intersphinx references now get stuck in a "Loading" tooltip:

image

If I check on an older build from a couple of weeks ago, e.g. on https://grill.readthedocs.io/en/develop/api.html, the problem does not appear. Do you know what might be the issue?

@@ -4,9 +4,13 @@ Example page
This is an example page.

:ref:`This a :ref: to The Python Tutorial using intersphinx <python:tutorial-index>`.
:ref:`This a :ref: to datetime.datetime Python's function using intersphinx <python:datetime-datetime>`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to remove these single quotes since they were causing trouble on my end, they came as:

Python’s

Found it easier to remove them since I don't find them relevant to this test, but please let me know if I should bring them back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be an encoding problem on your side. You may not be declaring the encoding properly.


:py:meth:`collections.Counter.elements` to test intersphinx methods.

:py:func:`sum` to test intersphinx functions.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the changes, these two lines above do not have the "hoverxref ... reference external" html class added.

@chrizzFTD chrizzFTD requested a review from humitos October 3, 2021 00:42
@humitos
Copy link
Member

humitos commented Oct 4, 2021

If I check on an older build from a couple of weeks ago, e.g. on grill.readthedocs.io/en/develop/api.html, the problem does not appear. Do you know what might be the issue?

Yes. It's already fixed. There was a problem with CORS and the fix didn't go out yet. I solved it at readthedocs/readthedocs.org#8543. We are deploying that fix tomorrow and the tooltips should work properly in your docs with this branch.

@humitos
Copy link
Member

humitos commented Oct 6, 2021

@chrizzFTD can you confirm that tooltips work on your documentation using master branch now?

Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good! The new approach is way better since it will always bring all the supported reftypes. I left some suggestions to consider. Let me know!

# Read the Docs' link does have hoverxref enabled
r'<a class="hoverxref modal reference external" href="https://docs.readthedocs.io/en/stable/config-file/v2.html#python" title="\(in Read the Docs v\d\d?.\d\d?.\d+\)"><span class="xref std std-ref">This a :ref: to Config File v2 Read the Docs page using intersphinx</span></a>',
r'<a class=\"hoverxref modal reference external\" href=\"https://docs.readthedocs.io/en/stable/config-file/v2.html#python\" title=\"\(in Read the Docs v\d\d?.\d\d?.\d+\)\"><span class=\"xref std std-ref\">This a :ref: to Config File v2 Read the Docs page using intersphinx</span></a>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These \" are not required. It should just work with regular " since the string is defined with '

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This double quote character was not being escaped by the regular expression when I ran the tests locally, so it never matched, do you know of any setting that would cause this?

r'<a class=\"hoverxref tooltip reference external\" href=\"https://docs.python.org/3/tutorial/index.html#tutorial-index\" title=\"\(in Python v3.\d\d?\)\"><span class=\"xref std std-ref\">This a :ref: to The Python Tutorial using intersphinx</span></a>',
r'<a class=\"hoverxref tooltip reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime-datetime\" title=\"\(in Python v3.\d\d?\)\"><span class=\"xref std std-ref\">This a :ref: to datetime.datetime Python function using intersphinx</span></a>',
r'<a class=\"hoverxref modal reference external\" href=\"https://docs.python.org/3/library/functions.html#float\" title=\"\(in Python v3.\d\d?\)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">float</span></code></a>',
r'<a class=\"hoverxref tooltip reference external\" href=\"https://docs.python.org/3/library/collections.html#collections.Counter.elements\" title=\"\(in Python v3.\d\d?\)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">collections.Counter.elements\(\)</span></code></a> to test intersphinx methods.</p>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here that this case is particular and it's for py:meth (fallbacks) ? Also, if it's even clear to explain/understand, we can create a specific test case for these

# Intersphinx inventories may come with concrete types for domain roles.
# (e.g. for python, when reftype=='meth', inventory uses "method" or "classmethod"...
# so check in order, starting from the current reftype.
keys_to_check = [reftype, *env_domain.objtypes_for_role(reftype)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
keys_to_check = [reftype, *env_domain.objtypes_for_role(reftype)]
keys_to_check = set([reftype, *env_domain.objtypes_for_role(reftype)])

In some cases reftype is already included in the response from env_domain.objtypes_for_role(reftype), so we can create a set() to remove duplicated ones.

@chrizzFTD
Copy link
Contributor Author

Thanks @humitos ! I'll come back to this hopefully next weekend

@humitos
Copy link
Member

humitos commented May 30, 2022

I think #171 should solve this issue as well. @chrizzFTD can you please confirm that the latest commit from main branch does work as you expected?

@humitos
Copy link
Member

humitos commented Jun 14, 2022

@chrizzFTD We released a new version (v1.1.1) with the PR linked on it. I suppose this problem is solved now. However, feel free to re-open this PR or comment here if you find something wrong. Let me know!

@humitos humitos closed this Jun 14, 2022
chrizzFTD added a commit to thegrill/grill that referenced this pull request Sep 4, 2022
@chrizzFTD
Copy link
Contributor Author

Hi @humitos, thanks a lot for the updates & apologies it took me so long to come back here on GitHub; but I can confirm that the latest release sphinx_hoverxref-1.1.3 works perfectly! 😃

@humitos
Copy link
Member

humitos commented Sep 4, 2022

Great! Happy to read that 💪

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants