-
Notifications
You must be signed in to change notification settings - Fork 258
Subclassing Protocol
get different __init__
#644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This issue is fixed by python/cpython#27545 |
Still getting this in Python 3.12 I believe. When I define an empty init in the class inheriting from the protocol my issues are solved. And they are only caused if I inherit from a Protocol. Maybe it's linked to the fact that I'm using a generic Protocol but I wouldn't know much about this. ...
# Generic types per repository
Model = TypeVar("Model", bound=DbBaseModel)
TCreate = TypeVar("TCreate", bound=BaseModel, contravariant=True)
TUpdate = TypeVar("TUpdate", bound=BaseModel, contravariant=True)
# Only used as input
ModelSchema = TypeVar("ModelSchema", bound=BaseModel)
Data = TypeVar("Data", bound=BaseModel)
class RepositoryProtocol(Protocol[Model, TCreate, TUpdate]):
@property
def model(self) -> Type[Model]: ... class BaseRepository(ABC, RepositoryProtocol[Model, TCreate, TUpdate]):
@property
@abstractmethod
def model(self) -> Type[Model]:
raise NotImplementedError I noticed this as I was using BaseRepository as a dependency in a FastAPI route. As soon as I added the protocol, the route was expecting *args, *kwargs as required arguments. As soon as I remove the protocol, this is fixed again. def __init__(self) -> None: ... |
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.Now with
Protocol
, we get different__init__
API implicitly.It seems that it inherit
__init__(self,*args,**kwargs)
fromProtocol
.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.
The text was updated successfully, but these errors were encountered: