-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
Feature
The __new__ method of a Metaclass should be hinted as returning a specific Type.
Pitch
class MyPool:
registry: Dict[str, Type[MyAbstractClass]] = {}
@classmethod
def register(cls, the_class):
cls.registry[the_class.__name__] = the_class
class MyMetaClass(type):
def __new__(cls, name, bases, attrs) -> Type[MyAbstractClass]:
if not bases:
return super().__new__(name, bases, attrs)
new_class = super().__new__(name, bases, attrs)
# do something with new_class. in my case:
MyPool.register(new_class)
return new_class
class MyAbstractClass(metaclass=MyMetaClass):
pass
class MyRealClass(MyAbstractClass):
passCurrently, mypy will say: "__new__" must return a class instance (got "Type[MyAbstractClass]")