-
Notifications
You must be signed in to change notification settings - Fork 1
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
Use custom typehint formatter #124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I don't think it is too verbose.
Question: Can this be adapted so it also works nicely with Python 3.12 type aliases (I have not tried how those are rendered currently)?
{% endfor %}], | ||
{% endfor %}], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, black changed the corresponding line in the rendered conf.py
.
def _link(ty: type, kind: str, type_params: Optional[tuple[type, ...]] = None) -> str: | ||
if ty.__module__ == 'builtins': | ||
target = ty.__name__ | ||
else: | ||
target = f'{ty.__module__}.{ty.__name__}' | ||
label = _typehint_aliases.get(target, target) | ||
if type_params: | ||
label += f'[{", ".join(ty.__name__ for ty in type_params)}]' | ||
return f':{kind}:`{label} <{target}>`' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I get this correctly we get working links now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do for uses of NewType
. It seems that either with the latest Python or latest Sphinx, they are shown in the docs and then we also get links. It doesn't work with type Filename = str
as the example shows, though.
Fixes #61
The docs don't show type aliases, so the refs that this generates don't work. I added them regardless for when we (hopefully) find a way to show aliases. I added the underlying type so that users can find out what types are actually required.
The result is still quite verbose. It could be made more compact by removing the module name. But then it is harder to find the definitions of the types.
With this change, this:
becomes this:
But it does not handle
TypeVar
. So this:becomes this: