Skip to content

User generic with TypeVarTuple does not check for minimal type of arguments #99382

Closed

Description

@serhiy-storchaka

The following code:

from typing import *
T = TypeVar('T')
T2 = TypeVar('T2')
Ts = TypeVarTuple('Ts')
class A(Generic[T, T2, *Ts]):
    x: List[T]
    y: List[T2]
    z: Tuple[*Ts]

A[int]

is executed without errors. It is expected to get an error because A requires at least two arguments.

Without TypeVarTuple you get helpful errors.

class B(Generic[T, T2]):
    pass

B[int]
B[int, str, bytes]
TypeError: Too few arguments for <class '__main__.B'>; actual 1, expected 2
TypeError: Too many arguments for <class '__main__.B'>; actual 3, expected 2

Activity

  1. sobolevn commented on Nov 11, 2022

    @sobolevn
    Member

    The code says this:

    cpython/Lib/typing.py

    Lines 1833 to 1836 in f531b68

    # We only run this if there are no TypeVarTuples, because we
    # don't check variadic generic arity at runtime (to reduce
    # complexity of typing.py).
    _check_generic(cls, params, len(cls.__parameters__))

  2. added a commit that references this issue on Nov 12, 2022
  3. added a commit that references this issue on Nov 29, 2022
  4. added 3 commits that reference this issue on Nov 29, 2022
  5. AlexWaygood commented on May 23, 2023

    @AlexWaygood
    Member

    I believe that this has been fixed and backported (in #99412 and #99866). Closing as completed.

    (Feel free to reopen if I'm incorrect.)

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.11only security fixes3.12only security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions