I don't know if I'm running into a bug or I'm just not doing things right.
I was able to minimize my problem to this using Python 3.5 and mypy 0.660:
from abc import ABC, abstractmethod from typing import Any, Optional, Type, Union class A(ABC): pass class AA(A): pass class AB(A): pass class M(object): pass class MA(M, AA): pass class MB(M, AB): pass MYTYPES = Union[MA, MB] class T(object): def __init__(self, newtype:Type[MYTYPES]=MA) -> None: assert isinstance(newtype, type(M)) self._new_type = newtype # type: Type[MYTYPES] def test(self) -> A: ret = self._new_type() print(type(ret)) return ret x = T() print(isinstance(x.test(), A)) # True y = T(MB) print(isinstance(y.test(), A)) # True
My results are:
$ python3 -m mypy mixin_test.py
mixin_test.py:27: error: Incompatible types in assignment (expression has type "Type[M]", variable has type "Union[Type[MA], Type[MB]]")
$ python3 mixin_test.py <class '__main__.MA'>
True
<class '__main__.MB'>
True
It seems that the assert is causing mypy to not realize that the object is still of type 'A'. I just want to make sure that the item passed in has the MixIn 'M' so that it can support the functions that I need. It is quite possible that I'm approaching this whole thing wrong.
I don't know if I'm running into a bug or I'm just not doing things right.
I was able to minimize my problem to this using Python 3.5 and mypy 0.660:
My results are:
It seems that the assert is causing mypy to not realize that the object is still of type 'A'. I just want to make sure that the item passed in has the MixIn 'M' so that it can support the functions that I need. It is quite possible that I'm approaching this whole thing wrong.