Skip to content

Paramspec support in Generic #816

@shughes-uk

Description

@shughes-uk

Trying to define a generic using the backporked paramspec in python 3.

from typing import Generic
from typing_extensions import ParamSpec

p = ParamSpec("p")
class X(Generic[p]):
    pass

You'll run into an exception thrown by this snippet in the Generic class

            if not all(isinstance(p, TypeVar) for p in params):
                raise TypeError(
                    f"Parameters to {cls.__name__}[...] must all be type variables"
                )

In 3.10 this has been changed to allow it as defined by PEP 612

https://github.com/python/cpython/blob/3.10/Lib/typing.py#L1260

From the limitations note in the readme I would expect it to work for static typing checking but fail silently at runtime, instead I get an exception that interrupts program flow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions