-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-45081: Fix __init__ method generation when inheriting from Protocol #28121
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1eb7835
bpo-45081: Fix dataclass __init__ method generation when inherit fro…
uriyyo 084840a
Lazly override __init__ method of a Protocol subclasses
uriyyo 6d1be74
Update Lib/test/test_dataclasses.py
uriyyo 91b011e
Update Ptocol init method
uriyyo 8dc6a2e
Update comment
uriyyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1400,8 +1400,29 @@ def _is_callable_members_only(cls): | |
return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls)) | ||
|
||
|
||
def _no_init(self, *args, **kwargs): | ||
raise TypeError('Protocols cannot be instantiated') | ||
def _no_init_or_replace_init(self, *args, **kwargs): | ||
cls = type(self) | ||
|
||
if cls._is_protocol: | ||
raise TypeError('Protocols cannot be instantiated') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: this is the previous behavior. |
||
|
||
# Initially, `__init__` of a protocol subclass is set to `_no_init_or_replace_init`. | ||
# The first instantiation of the subclass will call `_no_init_or_replace_init` which | ||
# searches for a proper new `__init__` in the MRO. The new `__init__` | ||
# replaces the subclass' old `__init__` (ie `_no_init_or_replace_init`). Subsequent | ||
# instantiation of the protocol subclass will thus use the new | ||
# `__init__` and no longer call `_no_init_or_replace_init`. | ||
for base in cls.__mro__: | ||
init = base.__dict__.get('__init__', _no_init_or_replace_init) | ||
if init is not _no_init_or_replace_init: | ||
cls.__init__ = init | ||
break | ||
else: | ||
# should not happen | ||
cls.__init__ = object.__init__ | ||
|
||
cls.__init__(self, *args, **kwargs) | ||
|
||
|
||
def _caller(depth=1, default='__main__'): | ||
try: | ||
|
@@ -1541,15 +1562,6 @@ def _proto_hook(other): | |
|
||
# We have nothing more to do for non-protocols... | ||
if not cls._is_protocol: | ||
if cls.__init__ == _no_init: | ||
for base in cls.__mro__: | ||
init = base.__dict__.get('__init__', _no_init) | ||
if init != _no_init: | ||
cls.__init__ = init | ||
break | ||
else: | ||
# should not happen | ||
cls.__init__ = object.__init__ | ||
return | ||
|
||
# ... otherwise check consistency of bases, and prohibit instantiation. | ||
|
@@ -1560,7 +1572,7 @@ def _proto_hook(other): | |
issubclass(base, Generic) and base._is_protocol): | ||
raise TypeError('Protocols can only inherit from other' | ||
' protocols, got %r' % base) | ||
cls.__init__ = _no_init | ||
cls.__init__ = _no_init_or_replace_init | ||
|
||
|
||
class _AnnotatedAlias(_GenericAlias, _root=True): | ||
|
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2021-09-02-12-42-25.bpo-45081.tOjJ1k.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix issue when dataclasses that inherit from ``typing.Protocol`` subclasses | ||
have wrong ``__init__``. Patch provided by Yurii Karabas. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.