Distinguish argument name error from argument type error in callback.
Hi,
Thanks for maintaining such a good piece of software.
Is your feature request related to a problem? Please describe. Pyright reports reportArgumentType error when the type of the parameter of a callback is invalid (ok) but also when the name of the parameter does not match the expected one.
Demonstration (note that pyright is in strict mode):
from collections.abc import Callable
def valid_callback(a: int) -> None:
pass
def invalid_callback(a: str) -> None:
pass
def should_be_valid_callback(b: int) -> None:
pass
class callback_store[**P]:
def __init__(self, cb: Callable[P, None]) -> None:
self._cb = cb
self._store = list[Callable[P, None]]()
def add(self, cb: Callable[P, None]) -> None:
self._store.append(cb)
@callback_store
def expected_signature(a: int) -> None:
pass
expected_signature.add(valid_callback) # OK: no error
expected_signature.add(invalid_callback) # OK: "int" is not assignable to "str" [reportArgumentType]
expected_signature.add(should_be_valid_callback) # Not OK: Parameter name mismatch: "a" versus "b" [reportArgumentType]
I caller should be free to use the parameter name they prefer in their callback. This is particularly useful when the expected_signature accepts an abstract type and the user code registers a more specific type. In such a case, it is often needed to adjust the parameter name.
Describe the solution you’d like Depending on the strictness of pyright, it could:
- completely ignore parameter name errors
- report them but using another error, like
reportArgumentName, so that we could, in a comment, tell pyright to ignore it for the specific line without disabling type error reports, which are pretty much always a bug.
Hi. The reason pyright reports this as an error is because a regular parameter can be passed both positionally and as a keyword:
def func(a: int) -> None: ...
func(1) # positional
func(a=1) # keyword
In your case, the callables all need to satisfy the paramspec P, which is bound to (a: int) here. With (b: int), there is no way to pass a as a keyword, so it is reported.
PEP 570 positional-only parameters are what you are looking for. In a function signature, any parameter that comes before a / is positional-only:
from typing import reveal_type, Callable
def test1(x: int, y: int, z: int, /) -> None: ...
def test2(a: int, b: int, c: int, /) -> None: ...
def check[**P](
f1: Callable[P, None],
f2: Callable[P, None],
) -> tuple[Callable[P, None], ...]:
return f1, f2
reveal_type(check(test1, test2))
Type of "check(test1, test2)" is "tuple[(x: int, y: int, z: int, /) -> None, ...]"
A minimal change for your repro would be to make expected_signature pos-only:
@callback_store
def expected_signature(a: int, /) -> None: ...
expected_signature.add(valid_callback) # (a: int) assignable to (int) ✅
expected_signature.add(invalid_callback) # (a: str) not assignable to (int) ❌
expected_signature.add(should_be_valid_callback) # (b: int) assignable to (int) ✅
I completely overlooked keyword arguments. The behavior of pyright is perfectly valid. Thank you for your detailed answer. I'll update my codebase.