pythonpython-typingmypyabstract-base-class

Why won't mypy understand this object instantiation?


I'm trying to define a class that takes another class as an attribute _model and will instantiate objects of that class.

from abc import ABC
from typing import Generic, TypeVar, Any, ClassVar, Type

Item = TypeVar("Item", bound=Any)


class SomeClass(Generic[Item], ABC):
    _model: ClassVar[Type[Item]]

    def _compose_item(self, **attrs: Any) -> Item:
        return self._model(**attrs)

I think it should be obvious that self._model(**attrs) returns an instance of Item, since _model is explicitly declared as Type[Item] and attrs is declared as Dict[str, Any].

But what I'm getting from mypy 0.910 is:

test.py: note: In member "_compose_item" of class "SomeClass":
test.py:11: error: Returning Any from function declared to return "Item"
            return self._model(**attrs)
            ^

What am I doing wrong?


Solution

  • MyPy can sometimes be a bit funny about the types of classes. You can solve this by specifying _model as Callable[..., Item] (which, after all, isn't a lie) instead of Type[Item]:

    from abc import ABC
    from typing import Generic, TypeVar, Any, ClassVar, Callable
    
    Item = TypeVar("Item")
    
    
    class SomeClass(Generic[Item], ABC):
        _model: ClassVar[Callable[..., Item]]
    
        def _compose_item(self, **attrs: Any) -> Item:
            return self._model(**attrs)