admin管理员组文章数量:1291221
pyright
seems to expect a name of the parameter in a Callable
when using assert_type
For such code:
from typing import assert_type
from collections.abc import Callable
def tuple_of_nums(n: int) -> tuple[int,...]:
return tuple(range(n))
assert_type(tuple_of_nums, Callable[[int], tuple[int,...]])
running pyright file.py
yields:
file.py:5:13 - error: "assert_type" mismatch: expected "(int) -> tuple[int, ...]" but received "(n: int) -> tuple[int, ...]" (reportAssertTypeFailure)
The only difference being n:
in the received
function.
How do I make this type assertion work?
pyright
seems to expect a name of the parameter in a Callable
when using assert_type
For such code:
from typing import assert_type
from collections.abc import Callable
def tuple_of_nums(n: int) -> tuple[int,...]:
return tuple(range(n))
assert_type(tuple_of_nums, Callable[[int], tuple[int,...]])
running pyright file.py
yields:
file.py:5:13 - error: "assert_type" mismatch: expected "(int) -> tuple[int, ...]" but received "(n: int) -> tuple[int, ...]" (reportAssertTypeFailure)
The only difference being n:
in the received
function.
How do I make this type assertion work?
Share Improve this question edited Feb 13 at 16:58 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked Feb 13 at 16:55 VulwsztynVulwsztyn 2,2711 gold badge14 silver badges23 bronze badges 9 | Show 4 more comments1 Answer
Reset to default 2There are two reasons for this.
First, the asserted signature (int) -> tuple[int, ...]
only allows the lone argument to be passed in positionally, but the actual signature (n: int) -> tuple[int, ...]
allows it to be passed as n
as well, which causes a difference in arity.
Second, assert_type()
's semantics are not standardized. Here's everything what the specs have to say about it, save for an example (emphasis mine):
The function
typing.assert_type(val, typ)
allows users to ask a static type checker to confirm that val has an inferred type of typ.When a type checker encounters a call to
assert_type()
, it should emit an error if the value is not of the specified type[.]
Neither the exact algorithm assert_type()
should follow nor type inference is standardized, and so different type checkers might handle assert_type()
differently.
In this case, the type checker is Pyright, and it checks for equivalency (or, more precisely, gradual equivalency) rather than assignability.
本文标签: pythonasserttype with callableStack Overflow
版权声明:本文标题:python - assert_type with callable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515623a2382869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
n
positional-only:def tuple_of_nums(n: int, /) -> tuple[int, ...]: ...
. I'll look for a dupe. – InSync Commented Feb 13 at 16:59Callable
andassert_type
with pyright, and protocols don't seem to work at all. – STerliakov Commented Feb 13 at 17:08assert_type
isn't strictly about subtyping, but requires exact match. And the problem in question is the opposite anyway: thisdef
is assignable toCallable[[int], something]
, because it can be called with one positional arg of type int, soassert_type
would have passed if it were to follow subtyping relationships. – STerliakov Commented Feb 13 at 18:40assert_type()
. – InSync Commented Feb 13 at 19:01