Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell mypy I know the type of return parameter for sure

The following code produces a - understandable, yet wrong - error from mypy:

from typing import List, Union, Any

class classA():
    def __init__(self, name: str) -> None:
        self.__name = name

    def __eq__(self, other: Any) -> bool:
        if (type(self) == type(other)):
            return (self.name == other.name)
        return False

    @property
    def name(self) -> str:
        return self.__name


class classB():
    def __init__(self, id: int) -> None:
        self.__id = id

    def __eq__(self, other: Any) -> bool:
        if (type(self) == type(other)):
            return (self.id == other.id)
        return False

    @property
    def id(self) -> int:
        return self.__id


class classC():
    def __init__(self) -> None:
        self.__elements: List[Union[classA, classB]] = list()

    def add(self, elem: Union[classA, classB]) -> None:
        if (elem not in self.__elements):
            self.__elements.append(elem)

    def get_a(self, name_of_a: str) -> classA:
        tmp_a = classA(name_of_a)
        if (tmp_a in self.__elements):
            return self.__elements[self.__elements.index(tmp_a)]
        raise Exception(f'{name_of_a} not found')

    def get_b(self, id_of_b: int) -> classB:
        tmp_b = classB(id_of_b)
        if (tmp_b in self.__elements):
            return self.__elements[self.__elements.index(tmp_b)]
        raise Exception(f'{id_of_b} not found')

The call mypy --show-error-codes classes.py shows the following output:

classes.py:43: error: Incompatible return value type (got "Union[classA, classB]", expected "classA")  [return-value]
classes.py:49: error: Incompatible return value type (got "Union[classA, classB]", expected "classB")  [return-value]
Found 2 errors in 1 file (checked 1 source file)

How do I tell mypy that the function get_a will only ever return classA?

like image 619
user2606240 Avatar asked Sep 02 '25 01:09

user2606240


1 Answers

You can tell this to mypy by using assert:

    def get_a(self, name_of_a: str) -> classA:
        tmp_a = classA(name_of_a)
        if (tmp_a in self.__elements):
            result = self.__elements[self.__elements.index(tmp_a)]
            assert isinstance(result, classA)
            return result
        raise Exception(f'{name_of_a} not found')
like image 128
qouify Avatar answered Sep 06 '25 16:09

qouify