ABC + abstractmethod and class that returns itself #1401
Closed
Nathanjp91
started this conversation in
General
Replies: 1 comment 2 replies
-
If you use from abc import ABC, abstractmethod
class Query(ABC):
def __init__(self, n: int = 0):
self.n = n
def example(self) -> Query:
return self.__class__(self.n + 1)
@abstractmethod
def must_implement(self) -> None:
pass https://mypy-play.net/?mypy=latest&python=3.11&gist=653b699eb38f5b32251fc64edc98b5b0 (If the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm working on an ABC with abstractmethods that returns it's own instance. Mypy will complain on these class methods and I'm wondering if there's a way around this.
minimum reproducible example
example
here will error,Cannot instantiate abstract class "Query" with abstract attribute "must_implement"
. What I want is to signfiy that the example function should return the object class that inherits this base class and fills in the abstractmethods such that the object can be created. Is this a possible interaction between typing and ABC?Beta Was this translation helpful? Give feedback.
All reactions