Skip to content

SubType and SubInstance #2169

Closed
Closed
@sirkonst

Description

@sirkonst

Hi,

I trying check with mypy following code:

import abc
from typing import Type

class Base(abc.ABC):
    @abc.abstractmethod
    def foo(self) -> None:
        pass

class Foo(Base):
    def foo(self):
        pass

def make_obj(cls: Type[Base]) -> Base:
    inst = cls()  # line 15
    return inst

make_obj(Foo)

And got errors:

subtype.py: note: In function "make_obj":
subtype.py:15: error: Cannot instantiate abstract class 'Base' with abstract attribute 'foo'

I want have some expression for define that variable type is one of subclasses of Base but not Base, somethink like this:

def make_obj(cls: SubType[Base]) -> SubInstance[Base]:
    inst = cls()
    return inst

Is it possible now?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions