Instantiating implementations of an abstract class from Type fails type validation
Basically, if T is abstract, then pyre seems to assume that any variable annotated with Type[T] is also abstract. According to docs, Type[T] accepts subclasses of T, so I don't think this is correct behavior.
Here's a code sample demonstrating:
from abc import ABCMeta, abstractmethod
from typing import Dict, Type
class Abstract(metaclass=ABCMeta):
@abstractmethod
def run(self) -> None:
pass
class FirstImpl(Abstract):
def run(self) -> None:
print("first")
class SecondImpl(Abstract):
def run(self) -> None:
print("second")
impl_mapper: Dict[str, Type[Abstract]] = {"first": FirstImpl, "second": SecondImpl}
impl_mapper["first"]().run()
And the error:
type_test.py:23:0 Invalid class instantiation [45]: Cannot instantiate abstract class `Abstract` with abstract method `run`.
I've the same need. Is there any workaround for this? For now I'm simply suppressing the warning.
I don't think the typechecker is wrong here--Type[T] does accept concrete subclasses of T, but it also accepts T itself--they may be instantiable, but the typechecker doesn't know that they are. What you want is a metatype that accepts concrete subtypes of Abstract but not it (or also-abstract subtypes).
I don't think this is a correct behavior. In a statically typed language I could have a Factory method that returns an abstract class and it's not a problem.
class Foo { virtual void bar() = 0; };
std::function<std::unique_ptr<Foo>()) baz;
and calling
auto foo = baz();
wouldn't be a problem.