[Python] assert_type with callable

Discussão em 'Python' iniciado por Stack, Fevereiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página