from abc import ABC, abstractmethod
from typing import Dict, Generic, List, TypeVar

T = TypeVar("T")


class FooGenericAbstract(ABC, Generic[T]):

    @abstractmethod
    def func(self) -> T:
        pass


class Foo(FooGenericAbstract[Dict[str, int]]): 

    def func(self) -> Dict[str, str]:
        pass

With coc.nvim and python 3.8, mypy 0.770 warns as expected.

I guess maybe you should use type hints instead of built-in types, since mypy cannot recognize the built-in types until now.

Answer from ssfdust on Stack Overflow
🌐
Python
typing.python.org › en › latest › spec › generics.html
Generics — typing documentation
If a generic base class has a type variable as a type argument, this makes the defined class generic. For example, we can define a generic LinkedList class that is iterable and a container: from typing import TypeVar from collections.abc import Iterable, Container T = TypeVar('T') class ...
Discussions

Inheriting Generic and ABC: Which order?
When inheriting both ABC, Generic and maybe some other classes, how to order inheritance correctly, to always have correct consistent MRO? # Which one is correct? class MyClass(collections.abc.Mapping, Generic[TV], ABC): ... class MyClass(collections.abc.Mapping, ABC, Generic[TV]): ... More on discuss.python.org
🌐 discuss.python.org
0
0
June 26, 2025
python - User defined generic types and collections.abc - Stack Overflow
I have a Python package that defines a variety of collections based on the ABCs provided by collections.abc (Mapping, Sequence, etc). I want to take advantage of the type hinting facilities introdu... More on stackoverflow.com
🌐 stackoverflow.com
Variance of arguments for Generic ABC vs Generic Protocol
I am trying to define a Generic base class, but see different type checking behavior when I inherit from abc.ABC versus Protocol. I am trying to figure out if it is really a difference of variance ... More on github.com
🌐 github.com
1
1
July 9, 2024
abstractmethod + generic method seems to disturb inference results
I believe this would qualify as a bug. When a method takes generic input types that are also present in the return type, and that method is defined as abstract, the revealed type of the method appe... More on github.com
🌐 github.com
7
November 3, 2019
🌐
Mypy
mypy.readthedocs.io › en › stable › generics.html
Generics - mypy 1.19.1 documentation
Functions can also be generic, i.e. they can have type parameters (Python 3.12 syntax): from collections.abc import Sequence # A generic function!
🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
This module provides the infrastructure for defining abstract base classes (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python.
🌐
Python.org
discuss.python.org › python help
Inheriting Generic and ABC: Which order? - Python Help - Discussions on Python.org
June 26, 2025 - When inheriting both ABC, Generic and maybe some other classes, how to order inheritance correctly, to always have correct consistent MRO? # Which one is correct? class MyClass(collections.abc.Mapping, Generic[TV], ABC): ... class MyClass(collections.abc.Mapping, ABC, Generic[TV]): ...
🌐
GitHub
github.com › python › typing › discussions › 1793
Variance of arguments for Generic ABC vs Generic Protocol · python/typing · Discussion #1793
July 9, 2024 - I am trying to define a Generic base class, but see different type checking behavior when I inherit from abc.ABC versus Protocol. I am trying to figure out if it is really a difference of variance ...
Author   python
🌐
Gui Commits
guicommits.com › python-generic-type-function-class
Python Generic function and class types
March 1, 2024 - Imagine a data-layer class that reads data from a data source and parses to some Python model for our scenario. typing.TypeVar is not enough anymore, we also need typing.Generic. ... Note we're not implementing this functionality as our purpose is only to understand how to use generics to define complex classes. import typing as t from datetime import datetime from abc import ABC, abstractmethod T = t.TypeVar("T") # 👈 We still use the TypeVar # 👇 Now we must also rely on Generic to say the class # accepts a type class BaseDatabase(t.Generic[T], ABC): @abstractmethod def get_by_id(self, id: int) -> T: ...
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 month ago - When inheriting from generic classes, some type parameters could be fixed: from collections.abc import Mapping class MyDict[T](Mapping[str, T]): ...
🌐
GitHub
github.com › python › mypy › issues › 7863
abstractmethod + generic method seems to disturb inference results · Issue #7863 · python/mypy
November 3, 2019 - from abc import ABC, abstractmethod from typing import Generic, TypeVar A = TypeVar('A') B = TypeVar('B') class TwoTypes(Generic[A, B]): pass class MakeTwoSurprise(ABC, Generic[A]): @abstractmethod def __call__(self, a: B) -> TwoTypes[A, B]: pass class MakeTwoExpected(Generic[A]): def __call__(self, a: B) -> TwoTypes[A, B]: pass class Test(): def make_two(self, mts: MakeTwoSurprise[A], mte: MakeTwoExpected[A]) -> None: reveal_type(mts.__call__) # Revealed type is 'def [B] (a: B`-1) -> repro.TwoTypes[A`-1, B`-1]' reveal_type(mts(2)) # Revealed type is 'repro.TwoTypes[builtins.int*, builtins.int*]' reveal_type(mte.__call__) # Revealed type is 'def [B] (a: B`2) -> repro.TwoTypes[A`-1, B`2]' reveal_type(mte(2)) # Revealed type is 'repro.TwoTypes[A`-1, builtins.int*]'
Author   beezee
🌐
Justin A. Ellis
jellis18.github.io › post › 2022-01-11-abc-vs-protocol
Abstract Base Classes and Protocols: What Are They? When To Use Them?? Lets Find Out! - Justin A. Ellis
January 11, 2022 - In Python there are two similar, yet different, concepts for defining something akin to an interface, or a contract describing what methods and attributes a class will contain. These are Abstract Base Classes (ABCs) and Protocols.
🌐
Python
peps.python.org › pep-3119
PEP 3119 – Introducing Abstract Base Classes | peps.python.org
ABCs are compatible with Generic Functions (GFs). For example, my own Generic Functions implementation [4] uses the classes (types) of the arguments as the dispatch key, allowing derived classes to override base classes. Since (from Python’s perspective) ABCs are quite ordinary classes, using an ABC in the default implementation for a GF can be quite appropriate.
🌐
Python.org
discuss.python.org › python help
Abstract, generic class properties beyond Python 3.13 - Python Help - Discussions on Python.org
May 13, 2024 - Up until now (Python 3.12) I’ve been using this pattern to get the concrete type out of an abstract generic at run time: DataIn = TypeVar('DataIn', infer_variance=True) class Handler(Generic[DataIn], ABC): @classmethod @property def processable(cls) -> type[DataIn]: return get_args(get_o...
🌐
Python
peps.python.org › pep-0560
PEP 560 – Core support for typing module and generic types | peps.python.org
Very long method resolution orders (MROs) for generic classes will be half as long; they are present because we duplicate the collections.abc inheritance chain in typing.
🌐
GitHub
github.com › python › typing › discussions › 1460
Is there any way to specify a class-level generic? · python/typing · Discussion #1460
Whenever you are expecting a subclass to provide something, using abc.abstractmethod is probably the way to go: https://mypy-play.net/?mypy=latest&python=3.11&gist=617d24caceb1069f05fd252ca2fe97d2 · As you can see an abstract property with a setter can be provided by a ClassVar, since it provides the same interface.
Author   python
🌐
Earthly
earthly.dev › blog › abstract-base-classes-python
Abstract Base Classes in Python - Earthly Blog
July 19, 2023 - Abstract Base Classes (ABCs) offer a solution to these limitations by allowing us to define a set of common methods and attributes that must be implemented by any class that inherits from the ABC.
🌐
Rogulski
rogulski.it › blog › python-typing-with-generic-typevar
Python type annotation improvement with Generic and TypeVar | rogulski.it
October 24, 2021 - We can fix this by introducing TypeVar with Generic: import abc from typing import TypeVar, Generic from schemas.base import BaseSchema from tables.base import BaseTable SCHEMA = TypeVar("SCHEMA", bound=BaseSchema) TABLE = TypeVar("TABLE", bound=BaseTable) class BaseRepository(Generic[SCHEMA, TABLE], metaclass=abc.ABCMeta): @property @abc.abstractmethod def _schema(self) -> Type[SCHEMA]: ...
🌐
Medium
shanenullain.medium.com › abstract-factory-in-python-with-generic-typing-b9ceca2bf89e
Abstract Factory in Python with Generic Typing | by Shane Nolan | Medium
January 19, 2022 - Lastly, we need to create our “factory of factories”, GameFactory. GameFactoryencapsulates PlayerFactoryand ItemFactory without specifying their concrete classes (what the abstract factory design pattern is meant to do).
🌐
Python
typing.python.org › en › latest › spec › protocol.html
Protocols — typing documentation
Generic protocols follow the rules for generic abstract classes, except for using structural assignability instead of assignability defined by inheritance relationships. Static type checkers will recognize protocol implementations, even if the corresponding protocols are not imported: # file lib.py from collections.abc import Sized class ListLike[T](Sized, Protocol): def append(self, x: T) -> None: pass def populate(lst: ListLike[int]) -> None: ...