-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Avoid expansion of typenames into long expressions, e.g., numpy.typing.ArrayLike
#420
Comments
Thanks for the very convincing example. I agree we should do better here, although it's not quite trivial as we heavily rely on dynamic instrumentation. Maybe it's worth to prototype some code that extracts the verbatim annotation from the AST and see how viable that is. |
Thanks for the quick response!
|
Basing this on postponed evaluations is an interesting trick - that way we don't need to do any AST shenanigans. It still requires some substantial changes to how we render function signature, but maybe we can make that work. The key part is that we currently evaluate all type hints here, before we format the signature. In the first step we need to retain the original string somewhere and then we need to figure out how we can use that as the link text. Not trivial. :) |
Upon further inspection, here are the options we have: 1a) Extend
|
The code in (1a) is intricate and I don't have the context to understand it well; I can see that it would become complicated to parse and reassemble the type strings. The code in (2) is very nice. (I hadn't seen the use of a lambda as a replacement --neat!) |
I'd generally like to keep the CLI surface as small as possible so that pdoc remains simple. I'm currently leaning towards this not crossing the bar, but I'll ponder on it for a bit. In either case, a from pdoc import pdoc, doc_types
doc_types.simplify_annotation.replacements["A"] = "B"
doc_types.simplify_annotation.recompile()
pdoc(...) |
The |
|
We may want |
Is there any reason to not do the desired behavior when a user is on a newer Python and is fine with using |
Are there any pending developments or interim proposed solutions for this item? It would be nice if there were a simple annotations flag of some sort which only returns the name (without path) of the annotation. |
I haven't tried with the |
A combination of Lines 25 to 29 in 8916055
to support typing-extensions (try import for <3.12, fall back to current implementation if import does not work). Contributions are welcome! |
Based on discussion at mitmproxy#420
Packages often include typenames that expand to long
Union[...]
definitions.Examples include
ArrayLike
andDTypeLike
fromnumpy.typing
(see here)For this sample program
the
pdoc
output looks awful:Is there any way to direct
pdoc
to show shorter typenames?We cannot use
typing.NewType
ortyping.TypeVar
because theUnion[...]
is not subclassable.Assigning the type to a local constant unfortunately does not help:
TypeAlias
also does not help:Can you think of any other workaround or solution? Ideally, any local typename constant could remain unexpanded.
(
TypeAlias
is not ideal because it only appears in Python 3.10.)Possibly look for
pdoc
metadata withintyping.Annotated
?The text was updated successfully, but these errors were encountered: