You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems the experimental variadic generic classes support doesn't handle having a zero-long tuple of types correctly. This is useful when handling a callback which takes no arguments. Both explicitly parametrizing with [()] and inferring from a function doesn't seem to work.
To Reproduce
fromtypingimportGeneric, Callable, TypeVarTuple, UnpackT=TypeVarTuple("T")
defno_args() ->None: ...
defone_arg(a: int, /) ->None: ...
classMyClass(Generic[Unpack[T]]):
func: Callable[[Unpack[T]], object]
def__init__(self, func: Callable[[Unpack[T]], object]) ->None:
self.func=funcdefcheck(explicit: MyClass[()]) ->None:
# note: Revealed type is "MyClass[Unpack[builtins.tuple[Any, ...]]]"reveal_type(explicit)
# error: Incompatible types in assignment (expression has type "Callable[[], None]",# variable has type "Callable[[VarArg(Any)], object]") [assignment]explicit.func=no_argsimplicit=MyClass(no_args) # Inference doesn't work either.reveal_type(implicit) # note: Revealed type is "MyClass"reveal_type(implicit.func) # note: Revealed type is "def (*Unpack[T`1]) -> builtins.object"implicit.func() # error: Too few arguments [call-arg]implicit.func(1) # error: Argument 1 has incompatible type "int"; expected "Unpack[T]" [arg-type]reveal_type(MyClass(one_arg)) # note: Revealed type is "MyClass[builtins.int]"
Expected Behavior
MyClass[()] should set T to be zero-long, thus accepting only functions with no args.
Actual Behavior
It looks like the TypeVarTuple is being set to tuple[Any, ...], which is not what is intended.
Fixes#16199
It was surprisingly hard to fix, because all possible fixes strongly
interfered with the code that makes "no-args" aliases possible:
```python
l = list
x: l[int] # OK, same as list[int]
```
So after all I re-organized (and actually simplified) that old code.
Bug Report
It seems the experimental variadic generic classes support doesn't handle having a zero-long tuple of types correctly. This is useful when handling a callback which takes no arguments. Both explicitly parametrizing with
[()]
and inferring from a function doesn't seem to work.To Reproduce
Expected Behavior
MyClass[()]
should setT
to be zero-long, thus accepting only functions with no args.Actual Behavior
It looks like the TypeVarTuple is being set to
tuple[Any, ...]
, which is not what is intended.Your Environment
--new-type-inference
--enable-incomplete-feature=Unpack
--enable-incomplete-feature=TypeVarTuple
The text was updated successfully, but these errors were encountered: