Improve Rendering of `typing.TypeVar`
See original GitHub issueProblem Description
TypeVars seem to be simply put through repr
to get their textual format. TypeVar’s __repr__
is missing important information necessary to the use of the class including constraints or bounds and covariance or contravariance.
Proposal
I would like to see TypeVars printed with a more information. For example, instead of just ~T
, it could be {variance} T ≤ {bound}
. The variance of a type only needs to be know once per type (likely at the occurrence of the TypeVar in the class definition) as TypeVars in functions/methods cannot have variance. Bounds should likely be printed on every occurrence of the TypeVar. Constraints could be represented as a tuple of bounds.
Alternatives
- Stating the bound and variance in the docstring. Docstrings can get out of date, but mypy ensures that annotations remain correct.
- Taking sphinx’s approach and adding support for documenting the TypeVar (yuck!).
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Python type annotation improvement with Generic and TypeVar
In this article, I would like to share my favorite way of type annotating classes using typing modules with Generic and TypeVar.
Read more >pyright faulty diagnostics for AnyStr = TypeVar ... - GitHub
No diagnostics. Actual behavior. Type check error resulting in Cannot access member "endswith" for type "TypeVar" - Member "endswith" is unknown ...
Read more >more_autodoc.typevars — sphinx-toolbox 2.18.0 documentation
TypeVar 's, similar to Sphinx's autotypevar but with a different appearance. New in version 1.3.0. ... TypeVarDocumenter with better type hint rendering.
Read more >python - what does argument "bound" mean in typing.TypeVar?
Here the bound parameter means that any instance of class that ineriths Foo , or any of its subclasses validates the typing criteria...
Read more >A Dive into Python Type Hints - Luke Merrett
An overview of typing hinting in Python, including Optional, Union, Protocol, TypeVar and Callable types.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would probably tell people who don’t think it’s important to correct their understanding. The majority of my uses of TypeVar’s (across 100k+ lines of code) have bounds, and knowledge of those bounds is necessary to correctly use that interface. The only time I really see unbounded TypeVar’s is on type-generic collections, which in general people aren’t writing since those are already implemented in the standard library. Not trying to make this an argument, but educational.
Bounds and covariance are documented by Sphinx, but only if the entire TypeVar is documented. That’s due to design decisions in Sphinx, not because they think that info is not important. Just correcting the record.
Of course it would, but I don’t think the core devs take the position that repr or str on types or values should be sufficient for documentation purposes. My gut feeling is they will try to push this burden on to the tool doing the documentation.
I’ll try this first, then the listed option second. Thank you for considering this change.
I think I agree with you that listing bounds is probably useful. But I do think that it’s not very important for the majority of pdoc users. One symptom of that is that it’s not supported in Sphinx, which usually tries to do much more than pdoc and provides more configuration knobs. Additionally, it should ideally be done upstream.
If you feel super strongly about this I don’t want to make your life overly difficult. Maybe we can adjust the patch above as follows:
typing._type_repr
instead of copying it.try
the patched__repr__
but log a warning and fall back to the default one if anything raises.defuse_unsafe_reprs
so that there are no permanent side effects. (I will take care of renaming + deprecating the old name)typing._type_repr
)misc.py
Alternatively, I’d suggest you post a full PR for the BPO. From my experience that greatly increases the odds that you get actual feedback. 😃