[Python-ideas] Structural type checking for PEP 484

Matthias Kramm kramm at google.com
Thu Sep 10 20:57:21 CEST 2015


On Wednesday, September 9, 2015 at 1:19:12 PM UTC-7, Guido van Rossum wrote:
>
> Jukka wrote up a proposal for structural subtyping. It's pretty good. 
> Please discuss.
>
> https://github.com/ambv/typehinting/issues/11#issuecomment-138133867
>

I like this proposal; given Python's flat nominal type hierarchy, it will 
be useful to have a parallel subtyping mechanism to give things finer 
granularity without having to resort to ABCs.

Are the return types of methods invariant or variant under this proposal?

I.e. if I have

  class A(Protocol):
    def f() -> int: ...

does

  class B:
    def f() -> bool:
      return True

implicitly implement the protocol A?

Also, marking Protocols using subclassing seems confusing and error-prone.
In your examples above, one would think that you could define a new 
protocol using

class SizedAndClosable(Sized):
    pass

instead of

class SizedAndClosable(Sized, Protocol):
    pass

because Sized is already a protocol.

Maybe the below would be a more intuitive syntax:

  @protocol
  class SizedAndClosable(Sized):
      pass

Furthermore, I strongly agree with #7. Typed, but optional, attributes are 
a bad idea.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150910/2a9face3/attachment-0001.html>


More information about the Python-ideas mailing list