Skip to content

Subclassing Protocol get different __init__ #644

@superChing

Description

@superChing

When we subclassing Protocol, we get a __init__ differing from default one but the protocol in question didn't define any __init__.

example

Previously, pure class __init__ take no arguments.

In [21]: class B:
    ...:     pass

In [22]: B(b=1,c=2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-9790dbc81f4f> in <module>
----> 1 B(b=1,c=2)

TypeError: B() takes no arguments

Now with Protocol, we get different __init__ API implicitly.

In [20]: from typing_extensions import Protocol
    ...: from typing import *
    ...:
    ...: class A(Protocol):
    ...:     a:int
    ...:
    ...: class B(A):
    ...:     pass
    ...:
    ...: B(b=1,c=2)
Out[20]: <__main__.B at 0x10a866198>

It seems that it inherit __init__(self,*args,**kwargs) from Protocol.

But the document (mypy) does not say anything about it.
This is not intuitive and surprising. At least any sane programmer would assume that __init__ didn't change, in reasoning that Protocol is just pure interface thus nothing would be inherited.


ENV

➜ pip show typing_extensions
Name: typing-extensions
Version: 3.7.2
Summary: Backported and Experimental Type Hints for Python 3.5+
Home-page: https://github.com/python/typing/blob/master/typing_extensions/README.rst
Author: Guido van Rossum, Jukka Lehtosalo, Lukasz Langa, Michael Lee
Author-email: [email protected]
License: PSF
Location: /Users/catch22/miniconda3/lib/python3.7/site-packages
Requires:
Required-by:

➜ ipython
Python 3.7.1 (default, Dec 14 2018, 13:28:58)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions