Skip to content

acostapazo/typing-inheritance-example

Repository files navigation

typing-inheritance-example 👪

This example implements a dummy code to represent a double inheritance.

See diagram: Diagram

It seems PyCharm warn us about unresolved double inheritance in Python.

Pycharm Warning

If use mypy:

mypy typing_inheritance.py             
typing_inheritance.py:71: error: Argument 1 to "create_random_user" has incompatible type "IRepository"; expected "IUserRepository"
Found 1 error in 1 file (checked 1 source file)

As Python-Vigo colleagues recommended, using a dynamic TypeVar hinting we can resolve this issue.

Check the solution in typing_inheritance_with_typevar.

Source to obtain every inheritors from specific class stackoverflow

If we check now with mypy:

$ mypy typing_inheritance_with_typevar.py
typing_inheritance_with_typevar.py:79: error: Unexpected argument to TypeVar()
typing_inheritance_with_typevar.py:82: error: Variable "typing_inheritance_with_typevar.Repository" is not valid as a type
typing_inheritance_with_typevar.py:82: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
Found 2 errors in 1 file (checked 1 source file)

It seems, I'm doing something wrong declaring the TypeVar

I tried with the following, but I should try it again with more time :)

from typing import TypeVar

def inheritors(klass):
    subclasses = []
    work = [klass]
    while work:
        parent = work.pop()
        for child in parent.__subclasses__():
            if child not in subclasses:
                subclasses.append(child)
                work.append(child)
    return tuple([TypeVar(subclass.__name__, bound=subclass) for subclass in subclasses])

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages