Skip to content
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

Fix styling of punctuation in signatures #77

Merged
merged 1 commit into from
Apr 28, 2022
Merged

Conversation

jbms
Copy link
Owner

@jbms jbms commented Apr 28, 2022

Fixes #76.

@jbms
Copy link
Owner Author

jbms commented Apr 28, 2022

I think this is the simplest fix --- we just need to make sure it doesn't inadvertently break something else.

Copy link
Collaborator

@2bndy5 2bndy5 left a comment

Choose a reason for hiding this comment

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

It looks to me like #76 was already fixed in #75 , but I'm in favor of this change since it is less generic and doesn't regress the current solution (as far as I can tell).

@jbms jbms merged commit 98d0ab6 into main Apr 28, 2022
@2bndy5
Copy link
Collaborator

2bndy5 commented Apr 28, 2022

I don't think that adding mypy to intersphinx config settings will allow hyperlinking abstracted types (like Tuple, Dict, etc). It looks like the mypy docs don't actually declare the abstract types using the py domain, so we can't hyperlink the type hints. Currently, the python docs (already in our intersphinx config) do provide the abstractions found in collections.abc module.

@mhostetter
Copy link
Contributor

I can confirm this PR fixes #76 for me. I see no side effects in my docs. Thanks!

Also, the object_description_options config is pretty neat! 🎉

@jbms
Copy link
Owner Author

jbms commented Apr 28, 2022

The issue with hyperlinking Tuple, Dict and other types defined by the typing module is simply that they are defined in the typing module. In the TensorStore docs I have monkey patched the Python domain xref resolution to handle these as a special case.

@mhostetter
Copy link
Contributor

@2bndy5 I think that's a bug in Sphinx. Notice, Union links correctly here, but Optional does not here.

I noticed something similar in my docs and added the autodoc-process-signature callback to resolve it. (I copied things I saw @jbms do in tensorstore)

def autodoc_process_signature(app, what, name, obj, options, signature, return_annotation):
    signature = modify_type_hints(signature)
    return_annotation = modify_type_hints(return_annotation)

    return signature, return_annotation

def modify_type_hints(signature):
    if signature:
        # Ensure types from the typing module are properly hyperlinked
        for type_name in ["Tuple", "List", "Sequence", "Dict", "Optional", "Union", "Iterator", "Type", "Literal"]:
            signature = signature.replace(f"{type_name}[", f"~typing.{type_name}[")
            signature = signature.replace("~typing.~typing", "~typing")

    return signature

def setup(app):
    app.connect("autodoc-process-signature", autodoc_process_signature)

@mhostetter
Copy link
Contributor

Jinx... lol

@mhostetter
Copy link
Contributor

I found that in functions and methods, typing.List was provided in the signature. However in properties, only List was provided, which didn't cross reference. A modified version of the above code was meant to add the typing. prefix to instances of List (etc) in signatures that didn't have it. 🤷

@2bndy5 2bndy5 deleted the fix-signature-punctuation branch April 28, 2022 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API signature punctuation has modified CSS
3 participants