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

Use custom typehint formatter #124

Merged
merged 6 commits into from
Jan 12, 2024
Merged

Use custom typehint formatter #124

merged 6 commits into from
Jan 12, 2024

Conversation

jl-wynen
Copy link
Member

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:
before

becomes this:
after


But it does not handle TypeVar. So this:
before2

becomes this:
after2

Copy link
Member

@SimonHeybrock SimonHeybrock left a 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)?

template/docs/conf.py.jinja Outdated Show resolved Hide resolved
@jl-wynen
Copy link
Member Author

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)?

It gets rendered in a bad way (my function does not apply to this case, so this is the default):
py312

This uses

type Filename = str;
"""The name of a file."""

Mode = NewType("Mode", str)
"""File open mode"""

Flags: TypeAlias = int
""""File flags"""

Interestingly, the link to Mode works now. Maybe something changes in python 3.12?

I'll try and improve the rendering.

@jl-wynen
Copy link
Member Author

I added handling of TypeAliasType. With the above dummy example, this produces the following. (I added a type parameter to Filename for testing. I there is no param, the [T] disappears.)
p312-after

Comment on lines -137 to +141
{% endfor %}],
{% endfor %}],
Copy link
Member

Choose a reason for hiding this comment

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

Intentional?

Copy link
Member Author

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.

Comment on lines +74 to +82
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}>`'
Copy link
Member

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?

Copy link
Member Author

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.

@jl-wynen jl-wynen merged commit 0b94d03 into main Jan 12, 2024
2 checks passed
@jl-wynen jl-wynen deleted the type-alias-formatter branch January 12, 2024 13:26
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.

Documentation with links to type aliases?
2 participants