This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve __repr__ of TypeVar
Type: enhancement Stage:
Components: Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ktbarrett
Priority: normal Keywords:

Created on 2022-03-19 17:19 by ktbarrett, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg415562 - (view) Author: Kaleb Barrett (ktbarrett) Date: 2022-03-19 17:19
Currently the __repr__ for TypeVar includes the variance information and the type name (for example ~T, +T_co, -T_contra), but it does not contain bound or constraint information. I'm not sure what value including variance but not bound information in the __repr__ is, both are important for the use of interfaces that use that variable.

I propose we add the bound and constraint information to the __repr__. The __repr__ is arbitrary as popular type checking tools, such as mypy, and documentation tools, such as Sphinx, do not use the standard __repr__. Nor is the __repr__ eval()-able like many builtin types. And for documentation tools that do use the standard __repr__, this improvement will be propagated to those tools. (I originally requested this improvement in pdoc which uses the standard __repr__; the maintainer agreed with this improvement.)

Bounds can be represented using an ASCII representation of the subset operator "<=" and then the bound. Constraints can be represented using "<=" with a tuple of the constraints. Perhaps spaces should be added around the "<=" operator? I have no opinion.

Some examples of the proposed __repr__:
>>> TypeVar("T")
~T
>>> IntT = TypeVar("IntT", bound=int)
>>> IntT
~IntT<=int
>>> TypeVar("AnyStr", str, bytes)
~AnyStr<=(str, bytes)
>>> List[IntT]
List[~IntT<=int]
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91224
2022-03-19 17:19:22ktbarrettcreate