[Python] How to build a type from another type and make mypy checking work

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working with Python's type hinting system and I'm trying to create a type alias for a function that's similar to an existing function type, but with one additional argument. Here's what I've attempted:

    from typing import Callable

    # Original function type
    RK_function = Callable[[float, int], int]

    # Attempted new function type with an additional int argument
    RK_functionBIS = Callable[[*RK_function.__args__[:-1], int], int]


    I expected RK_functionBIS to represent Callable[[float, int, int], int], which it does when runned directly without checking it with mypy. However, when I run mypy for type checking, I get this error: text


    error: Invalid type alias: expression is not a valid type [valid-type] Q
    ​

    Continue reading...

Compartilhe esta Página