Skip to content

Improve Rendering of typing.TypeVar #361

Description

@ktbarrett

Problem 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

  1. Stating the bound and variance in the docstring. Docstrings can get out of date, but mypy ensures that annotations remain correct.
  2. Taking sphinx's approach and adding support for documenting the TypeVar (yuck!).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions