'How to add type annotation to abstract classmethod constructor?

I'd like to type-annotate abstract class method witch behave as a constructor. For example in the code below, ElementBase.from_data is meant to be a abstract classmethod constructor.

tmp.py

from abc import abstractmethod, abstractclassmethod
import copy
from typing import TypeVar, Type

ElementT = TypeVar('ElementT', bound='ElementBase')

class ElementBase:
    data: int
    def __init__(self, data): self.data

    #@abstractmethod
    def get_plus_one(self: ElementT) -> ElementT:
        out = copy.deepcopy(self)
        out.data = self.data + 1
        return out

    @abstractclassmethod
    def from_data(cls: Type[ElementT], data: int) -> ElementT: # mypy error!!!
        pass

class Concrete(ElementBase):
    @classmethod
    def from_data(cls, data: int) -> 'Concrete': # mypy error!!!
        return cls(data)

However, applying mypy to this code shows the following erros.

tmp.py:18: error: The erased type of self "Type[tmp.ElementBase]" is not a supertype of its class "tmp.ElementBase"
tmp.py:23: error: Return type "Concrete" of "from_data" incompatible with return type <nothing> in supertype "ElementBase"

Do you have any idea to fix this error? Also, I'm specifically confused that the part of get_plus_one does not cause error, while only the part of abstractclassmethod does cause the error.

FYI, I want to make the abstract method constructor generic becaues I want to statically ensure that all subclass of ElementBase returns object with it's type when calling from_data.

[EDIT] comment out abstractmethod



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source