简体   繁体   中英

Infer Type of a Generic subclass as having itself as Type

I am working on making stubs for an external ORM library, I have encountered an issue that I am not sure how to overcome though. So the example bellow technically passes the mypy check, but only after expecting the library user to tediously repeat themselves during class declaration.

# Library stubs:
from typing import Generic, TypeVar, Type, Any, Optional
from collections.abc import Collection, Sequence
from abc import ABC


T = TypeVar('T', bound='BaseItem')
K = TypeVar('K')

class ItemSet(Generic[K]):
    def get_or_none(self, **kwargs: Any) -> Optional[K]: ...
    def first(self) -> K: ...
    def all(self) -> Collection[K]: ...
    def order_by(self, *args: Any) -> Sequence[K]: ...

class BaseItem(ABC, Generic[T]):
    @classmethod
    def set(cls: Type[T]) -> ItemSet[T]: ...

# User's model:
from library import BaseItem


class FooItem(BaseItem['FooItem']):
    name: str

class BarItem(BaseItem['BarItem']):
    size: float

class BazItem(BaseItem['BazItem']):
    id_: int

reveal_type(FooItem.set())
reveal_type(FooItem.set().all())

This generates this output:

main.py:32: note: Revealed type is "__main__.ItemSet[__main__.FooItem*]"
main.py:33: note: Revealed type is "typing.Collection[__main__.FooItem*]"

Which is exactly what you would expect, however this only works because the user had to pass the class name as a type on every class definition. The omission of type leads to it having the Any type

class FooItem(BaseItem):
    name: str
main.py:32: note: Revealed type is "__main__.ItemSet[Any]"
main.py:33: note: Revealed type is "typing.Collection[Any]"

So my question is how to make so this type inference is invisible to the user?

It's because you made it a generic class, it shouldn't be generic class, it is a generic function , essentially. Just use the following:

from typing import Generic, TypeVar, Type, Any, Optional
from collections.abc import Collection, Sequence
from abc import ABC


T = TypeVar('T', bound='BaseItem')
K = TypeVar('K')

class ItemSet(Generic[K]):
    def get_or_none(self, **kwargs: Any) -> Optional[K]: ...
    def first(self) -> K: ...
    def all(self) -> Collection[K]: ...
    def order_by(self, *args: Any) -> Sequence[K]: ...

class BaseItem(ABC):
    @classmethod
    def set(cls: Type[T]) -> ItemSet[T]: ...


class FooItem(BaseItem):
    name: str

class BarItem(BaseItem):
    size: float

class BazItem(BaseItem):
    id_: int

reveal_type(FooItem.set())
reveal_type(FooItem.set().all())

Here's what MyPy thinks (note, I put everything in one module named test.py for brevity):

(py39) Juans-MacBook-Pro:~ juan$ mypy test.py
test.py:29: note: Revealed type is "test.ItemSet[test.FooItem*]"
test.py:30: note: Revealed type is "typing.Collection[test.FooItem*]"

Note, this specific situation is addressed here in the PEP-484 spec

Note, there is a PEP to remove the TypeVar boilerplate:

https://www.python.org/dev/peps/pep-0673/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM