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 issue #481 #533

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Fix issue #481 #533

wants to merge 7 commits into from

Conversation

nineteendo
Copy link

No description provided.

@nineteendo nineteendo marked this pull request as ready for review March 30, 2025 13:34
@hoodmane
Copy link
Collaborator

Please add a test.

@nineteendo
Copy link
Author

What should I do with "Class Alias"? That's not a valid forward reference.

@hoodmane
Copy link
Collaborator

The documentation of autodoc-type-aliases says:

A dictionary for users defined type aliases that maps a type name to the full-qualified object name. It is used to keep type aliases not evaluated in the document.

https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_type_aliases

Class Alias isn't a fully qualified object name or even an identifier.

@hoodmane
Copy link
Collaborator

But I don't understand the problem or the fix very well @nineteendo could you explain a bit more?

@nineteendo
Copy link
Author

nineteendo commented Mar 30, 2025

  1. Aliases are converted to sphinx.util.inspect.TypeAliasForwardRef
  2. Alias | A is not supported.
  3. This crashes typing.get_type_hints()
  4. All annotations become strings.

I fixed this by creating a subclass that defines __or__(). (This might need to be fixed upstream)

@nineteendo
Copy link
Author

Hmm, forward references are not subscriptable either:

WARNING: Cannot resolve forward reference in type annotations of "jsonyx.dump": 'MyTypeAliasForwardRef' object is
not subscriptable

@nineteendo
Copy link
Author

Here a potential implementation for that (I've had enough of it):

diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py
index 4f20b97..0231a91 100644
--- a/src/sphinx_autodoc_typehints/__init__.py
+++ b/src/sphinx_autodoc_typehints/__init__.py
@@ -65,6 +65,9 @@ class MyTypeAliasForwardRef(TypeAliasForwardRef):
     def __or__(self, value: Any) -> Any:
         return Union[self, value]  # noqa: UP007
 
+    def __getitem__(self, item: Any) -> types.GenericAlias:
+        return types.GenericAlias(self, item)  # type: ignore
+
 
 def _get_types_type(obj: Any) -> str | None:
     try:
@@ -227,9 +230,6 @@ def format_annotation(annotation: Any, config: Config, *, short_literals: bool =
     if isinstance(annotation, tuple):
         return format_internal_tuple(annotation, config)
 
-    if isinstance(annotation, TypeAliasForwardRef):
-        return annotation.name
-
     try:
         module = get_annotation_module(annotation)
         class_name = get_annotation_class_name(annotation, module)
@@ -300,6 +300,10 @@ def format_annotation(annotation: Any, config: Config, *, short_literals: bool =
         formatted_args = args_format.format(", ".join(fmt))
 
     escape = "\\ " if formatted_args else ""
+    annotation = getattr(annotation, "__origin__", annotation)
+    if isinstance(annotation, TypeAliasForwardRef):
+        return f"{annotation.name}{escape}{formatted_args}"
+
     return f":py:{role}:`{prefix}{full_name}`{escape}{formatted_args}"

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.

2 participants