-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
base: main
Are you sure you want to change the base?
Fix issue #481 #533
Conversation
for more information, see https://pre-commit.ci
Please add a test. |
What should I do with |
The documentation of autodoc-type-aliases says:
https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_type_aliases
|
But I don't understand the problem or the fix very well @nineteendo could you explain a bit more? |
I fixed this by creating a subclass that defines |
Hmm, forward references are not subscriptable either:
|
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}" |
No description provided.