[Python] Not sure why this generates a mypy "Incompatible types in assignment"

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    Simple example of what's confusing me:

    from typing import Callable, List, Union

    Value = Union[bool, int, str]
    Helper = Callable[[Value], List[Value]]


    def func_with_alias(aa: Value) -> List[Value]:
    return []


    def func_with_type(aa: bool) -> List[Value]:
    return []


    your_func1: Helper = func_with_alias
    your_func2: Helper = func_with_type


    mypy complains that "your_func2" has an incompatible type: error: Incompatible types in assignment (expression has type "Callable[[bool], List[Union[bool, int, str]]]", variable has type "Callable[[Union[bool, int, str]], List[Union[bool, int, str]]]")

    Why doesn't it let me do this, as bool is in Union[bool, int, str]?

    ❯ mypy --version
    mypy 0.782

    Continue reading...

Compartilhe esta Página