-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeVarTuple
and ParamSpec
allow too few arguments to be specified when used together
#131696
Comments
Right now class A[*Ts, **P]: ...
A[int] substitutes See Lines 1085 to 1108 in 6fb5f7f
_paramspec_prepare_subst args=((), <class 'int'>)
__main__.A[int] But, class B[T, **P]: ...
B[int, str] # Should also raise clearly looks like a bug to me. Lines 1035 to 1041 in 6fb5f7f
It also substitutes as: _paramspec_prepare_subst args=(<class 'int'>, <class 'str'>)
__main__.B[int, str] However, |
So I think in: class A[*Ts, **P]: ...
A[int]
Valid forms would be: A[int, [str, int]]
# Ts -> (int,), P -> [str, int]
A[[str, int]]
# TS -> (), P -> [str, int] |
I think this is coming from the
As we can see, the However, if you parameterize the generic class with the same parameters, this code path will run and you will end up with a T = TypeVar('T')
P = ParamSpec('P')
class MyClass(Generic[T, P]): pass
alias = MyClass[T, P]
alias.__class__
#> typing._GenericAlias And when doing Things seem to work as expected when parameterizing the alias: class MyClass(Generic[T, P]): pass
alias = MyClass[T, P]
alias[int]
#> TypeError: Too few arguments for __main__.MyClass[~T, ~P]
alias[int, str]
#> TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'str'>
class MyClass(Generic[*Ts, P]): pass
alias = MyClass[*Ts, P]
alias[int]
#> TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'int'>
alias[str, int]
#> TypeError: Expected a list of types, an ellipsis, ParamSpec, or Concatenate. Got <class 'int'> Before working on fix, I'd like to know if this the omission |
Actually, that might be too disruptive to add. It might even have been intentional, but this would break use cases where forward references are used when parameterizing classes: class B[T, **P]: ...
B[int, "ForwardP"] # If we were to check the class arguments here, this would fail as str is not a list of types,an ellipsis, ParamSpec or Concatenate.
ForwardP = ParamSpec("ForwardP") Maybe we could try to do the necessary checks, but silently accept strings and forwardrefs? |
I'm closing this one in favor of #132100, which gives more context. |
Bug report
Bug description:
Following #99344, it seems to me that the following should raise a
TypeError
:I understand that these use cases may be really hard to support at runtime, if so I don't mind closing the issue.
CPython versions tested on:
3.14
Operating systems tested on:
No response
The text was updated successfully, but these errors were encountered: