You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow instantiation of Type[A], if A is abstract (#2853)
Fixes#1843 (It was also necessary to fix few minor things to make this work correctly)
The rules are simple, assuming we have:
```python
class A:
@AbstractMethod
def m(self) -> None: pass
class C(A):
def m(self) -> None:
...
```
then
```python
def fun(cls: Type[A]):
cls() # OK
fun(A) # Error
fun(C) # OK
```
The same applies to variables:
```python
var: Type[A]
var() # OK
var = A # Error
var = C # OK
```
Also there is an option for people who want to pass abstract classes around: type aliases, they work as before. For non-abstract ``A``, ``Type[A]`` also works as before.
My intuition why you opened #1843 is when someone writes annotation ``Type[A]`` with an abstract ``A``, then most probably one wants a class object that _implements_ a certain protocol, not just inherits from ``A``.
NOTE: As discussed in python/peps#224 this behaviour is good for both protocols and usual ABCs.
0 commit comments