PEP 544: Protocols by ilevkivskyi · Pull Request #224 · python/peps

and others added 30 commits

March 5, 2017 02:01
I'm excited by this proposal but couldn't help but make some minor changes while reading through it. I hope they'll help make it more readable.

@ilevkivskyi

JukkaL

ddfisher

@ilevkivskyi

ambv

ambv approved these changes Mar 17, 2017

@ilevkivskyi

@ilevkivskyi

gvanrossum

gvanrossum

gvanrossum pushed a commit to python/mypy that referenced this pull request

Mar 27, 2017
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.