Skip to content

Improve documentation for typing.TypeVar #90925

Closed

Description

@AlexWaygood
BPO 46769
Nosy @gvanrossum, @ambv, @JelleZijlstra, @sobolevn, @Fidget-Spinner, @AlexWaygood
PRs
  • bpo-46769: Improve documentation for typing.TypeVar #31712
  • [3.10] bpo-46769: Improve documentation for typing.TypeVar (GH-31712) #31941
  • [3.9] bpo-46769: Improve documentation for typing.TypeVar (GH-31712) (GH-31941) #32067
  • [3.10] bpo-46769: Fix backticks in typing.rst to appease rstlint #32374
  • [3.9] bpo-46769: Fix backticks in typing.rst to appease rstlint #32375
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-03-23.18:22:59.879>
    created_at = <Date 2022-02-16.17:01:18.275>
    labels = ['3.11', 'type-bug', '3.9', '3.10', 'docs']
    title = 'Improve documentation for `typing.TypeVar`'
    updated_at = <Date 2022-04-06.18:03:19.762>
    user = 'https://github.com/AlexWaygood'

    bugs.python.org fields:

    activity = <Date 2022-04-06.18:03:19.762>
    actor = 'kj'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2022-03-23.18:22:59.879>
    closer = 'JelleZijlstra'
    components = ['Documentation']
    creation = <Date 2022-02-16.17:01:18.275>
    creator = 'AlexWaygood'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46769
    keywords = ['patch']
    message_count = 9.0
    messages = ['413342', '413362', '415349', '415852', '415890', '415892', '415893', '416894', '416895']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'docs@python', 'lukasz.langa', 'JelleZijlstra', 'sobolevn', 'kj', 'AlexWaygood']
    pr_nums = ['31712', '31941', '32067', '32374', '32375']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46769'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. AlexWaygood commented on Feb 16, 2022

      @AlexWaygood
      MemberAuthor

      There are three variants of TypeVars:

      (1) TypeVars that are neither constrained nor bound: T = TypeVar("T")
      (2) TypeVars that are bound: U = TypeVar("U", bound=str)
      (3) TypeVars that are constrained: V = TypeVar("V", str, bytes)

      The third variant is important for annotating certain functions, such as those in the re module. However, it has a number of issues (see python/typing#1080 for further discussion):

      (1) It has somewhat surprising semantics in many situations.
      (2) It is difficult for type checkers to deal with, leading to a number of bugs in mypy, for example.
      (3) Many users (especially people relatively inexperienced with Python typing) reach for constrained TypeVars in situations where using bound TypeVars or the @overload decorator would be more appropriate.

      Both PEP-484 and the documentation for the typing module, however:

      (1) Give examples for variants (1) and (3), but not for variant (2), which is treated as something of an afterthought.
      (2) Do not mention that TypeVars can be bound to a union of types, which is an important point: T = TypeVar("T", str, bytes) has different semantics to T = TypeVar("T", bound=str|bytes), and often the latter is more appropriate.

    2. added
      docsDocumentation in the Doc dir
      type-bugAn unexpected behavior, bug, or error
      3.11only security fixes
      on Feb 16, 2022
    3. added
      docsDocumentation in the Doc dir
      type-bugAn unexpected behavior, bug, or error
      on Feb 16, 2022
    4. gvanrossum commented on Feb 16, 2022

      @gvanrossum
      Member

      Maybe historically constrained type vars predated bound type vars. This could have influenced the way PEP-484 was written and then the docs were derived from the PEP.

      But yes, this should be fixed in as many places as possible (including the mypy docs, probably).

      I would even support a minor update to PEP-484 to help people find the right way (the PEP is still widely cited). It would have to be just be a textual change, not a spec change.

    5. JelleZijlstra commented on Mar 16, 2022

      @JelleZijlstra
      Member

      New changeset 81b425d by Alex Waygood in branch 'main':
      bpo-46769: Improve documentation for typing.TypeVar (GH-31712)
      81b425d

    6. JelleZijlstra commented on Mar 23, 2022

      @JelleZijlstra
      Member

      New changeset d5ed8a8 by Alex Waygood in branch '3.10':
      [3.10] bpo-46769: Improve documentation for typing.TypeVar (GH-31712) (GH-31941)
      d5ed8a8

    7. ambv commented on Mar 23, 2022

      @ambv
      Contributor

      New changeset 0bbb695 by Jelle Zijlstra in branch '3.9':
      [3.9] bpo-46769: Improve documentation for typing.TypeVar (GH-31712) (GH-31941) (GH-32067)
      0bbb695

    8. JelleZijlstra commented on Mar 23, 2022

      @JelleZijlstra
      Member

      Thanks Alex for the PR and Łukasz for merging the last backport!

    9. AlexWaygood commented on Mar 23, 2022

      @AlexWaygood
      MemberAuthor

      Thanks Guido for your insight, and Jelle/Łukasz for the reviews and backports!

      I'm on holiday right now, but when I'm back, I'll take a look at maybe proposing some minor revisions to PEP-484 as well, as Guido suggests.

    10. Fidget-Spinner commented on Apr 6, 2022

      @Fidget-Spinner
      Member

      New changeset 80af26d by Ken Jin in branch '3.10':
      [3.10] bpo-46769: Fix backticks in typing.rst to appease rstlint (GH-32374)
      80af26d

    11. Fidget-Spinner commented on Apr 6, 2022

      @Fidget-Spinner
      Member

      New changeset d6a7ee6 by Ken Jin in branch '3.9':
      [3.9] bpo-46769: Fix backticks in typing.rst to appease rstlint (GH-32375)
      d6a7ee6

    12. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifedocsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions