Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python typing issue for child classes

This question is to clarify my doubts related to python typing

from typing import Union

class ParentClass:
    parent_prop = 1

class ChildA(ParentClass):
    child_a_prop = 2

class ChildB(ParentClass):
    child_b_prop = 3

def method_body(val) -> ParentClass:
    if val:
        return ChildA()
    else:
        return ChildB()

def another_method() -> ChildA:
    return method_body(True)

print(another_method().child_a_prop)

In the above piece of code, the linting tool I used is printing error as below

error: Incompatible return value type (got "ParentClass", expected "ChildA")

(where I do method_body(True))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have also set the method_body return type as Union[ChildA, ChildB].
This will result error: Incompatible return value type (got "Union[ChildA, ChildB]", expected "ChildA")

I am looking for a better way to do this.
If anyone knows the solution, your help will be very much appreciated.

>Solution :

mypy does not do runtime analysis so it cannot guess that calling
method_body with argument True will always result in a ChildA object. So the error it produces does make sense.

You have to guide mypy in some way to tell him that you know what you are doing and that another_method indeed produces a ChildA. One is to use a cast:

from typing import cast
def another_method() -> ChildA:
    return cast(ChildA, method_body(True))

Another one is to add an assertion:

def another_method() -> ChildA:
    result = method_body(True)
    assert isinstance(result, ChildA)
    return result
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading