8000 Fix rendering of nested type annotations with alias defined by JanLuca · Pull Request #9736 · sphinx-doc/sphinx · GitHub
[go: up one dir, main page]

Skip to content

Fix rendering of nested type annotations with alias defined #9736

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JanLuca
Copy link
@JanLuca JanLuca commented Oct 14, 2021

Subject: Fix rendering of nested type annotations with alias defined

Feature or Bugfix

  • Bugfix

Purpose

  • Make autodoc usable with nested types with alias defined

Detail

The current implementation replaces type annotations with a alias defined by the class TypeAliasForwardRef. This class is later again replaced by the type defined in the alias map.

This commit fixes the problem that this second replacement does not happen if the aliased type is nested in another generic type (e.g. Iterator[aliased_type]). The new function goes recursivly through the nested types and replaces the internal class TypeAliasForwardRef again by the correct annotation.

Relates

The current implementation replaces type annotations with a alias defined by the
class TypeAliasForwardRef. This class is later again replaced by the type
defined in the alias map.

This commit fixes the problem that this second replacement does not happen if
the aliased type is nested in another generic
type (e.g. Iterator[aliased_type]). The new function goes recursivly through the
nested types and replaces the internal class TypeAliasForwardRef again by the
correct annotation.
@JanLuca JanLuca force-pushed the bug-nested-type-alias branch from cc9a185 to d8c202f Compare October 14, 2021 16:57
@asmeurer
Copy link
Contributor
asmeurer commented Feb 8, 2022

I've been getting the error unhashable type: 'TypeAliasForwardRef' with nitpicky=True with certain types of nested type hints. This PR makes them go away.

(side question: is it possible to tell autodoc to just completely not try to make type hints into cross-references?)

asmeurer added a commit to asmeurer/array-api that referenced this pull request Feb 8, 2022
This makes Sphinx read the type hints directly.

It also triggers a Sphinx bug that requires this Sphinx PR to fix
sphinx-doc/sphinx#9736.
8000
Copy link
Member
@tk0miya tk0miya left a comment

Choose a reason for hiding this comment

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

Please add a testcase for this implementation.

@@ -614,6 +614,17 @@ def _should_unwrap(subject: Callable) -> bool:
return False


def _fix_type_alias_forward_ref(annotation: Any) -> Any:
Copy link
Member

Choose a reason for hiding this comment

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

How about expand_type_alias_forward_ref() instead?

Additonally, it would be better to add a docstring to this.


if hasattr(annotation, "__args__"): 8000
new_args = tuple(_fix_type_alias_forward_ref(arg) for arg in annotation.__args__)
return annotation.copy_with(new_args)
Copy link
Member

Choose a reason for hiding this comment

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

Is this method available on python3.6 and 3.10?

@tk0miya tk0miya added extensions:autodoc type:enhancement enhance or introduce a new feature labels Feb 11, 2022
@tk0miya tk0miya changed the base branch from 4.x to 5.x May 22, 2022 12:57
@AA-Turner AA-Turner changed the base branch from 5.x to master October 16, 2022 15:24
@AA-Turner AA-Turner added this to the some future version milestone Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extensions:autodoc type:enhancement enhance or introduce a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0