Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Generic with ParamSpec raise TypeError #126

Closed
@omaxx

Description

@omaxx

I create generic class with specifying __init__ arguments through ParamSpec:

from typing import Generic
from typing_extensions import ParamSpec

P = ParamSpec('P')

class CLS(Generic[P]):
    def __init__(self, *args: P.args, **kwargs: P.kwargs):
        ...

class ONE(CLS[int]):
    ...

class TWO(CLS[int, int]):
    ...

# should pass type checks:
ONE(1)
TWO(1, 2)
# should fail type checks:
ONE(1, 2)
TWO(1)

mypy type checking works as expected, but during runtime I got next error:

Traceback (most recent call last):
  File "temp/issue.py", line 13, in <module>
    class TWO(CLS[int, int]):
  File "/usr/local/Cellar/[email protected]/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 277, in inner
    return func(*args, **kwds)
  File "/usr/local/Cellar/[email protected]/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 1004, in __class_getitem__
    _check_generic(cls, params, len(cls.__parameters__))
  File "/usr/local/lib/python3.9/site-packages/typing_extensions.py", line 109, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
TypeError: Too many parameters for <class '__main__.CLS'>; actual 2, expected 1

Is it a bug? It works fine in 3.10 and 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions