Skip to content

class object typed with Type[SomeAbstractClass] should not complain about instantiation #1843

Closed
@gvanrossum

Description

@gvanrossum

I wish this code would work:

from typing import Type

from abc import ABC, abstractmethod

class A(ABC):
  @abstractmethod
  def m(self) -> None: pass

def f(a: Type[A]) -> A:
    return a()  # E: Cannot instantiate abstract class 'A' with abstract attribute 'm'

The obvious intention is to pass f() a concrete subclass of A.

(UPDATE: My real-world use case is a bit more involved, in asyncio there are some classes that implement an elaborate ABC, and I'd like to just say SelectorEventLoop = ... # type: Type[AbstractEventLoop] rather than having to write out the class definition with all 38 concrete method definitions.)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions