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.
bpo-41905: added abc.update_abstractmethods #22485
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
bpo-41905: added abc.update_abstractmethods #22485
Changes from all commits
f65b0f8
c04ba74
ea69ae6
8b76715
61c4f40
a5a9fd7
5e1fffc
2f95b84
3986c98
4f3d846
74473c8
92326c9
1e47ee4
740183f
114028e
ba8df08
29dba37
779e6cf
eea97f4
8eec6c2
5698153
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add tests for deeper inheritance - I think iterating over
__bases__
may not be enough:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually is- we are assured that the parent's
__abstractmethods__
are correct since, after a class is subclassed, its abstraction status cannot change:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test case regardless
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, interesting. How about this case? I think you might need to break after the first hit for the method:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is expected (albeit confusing) behavior. Since abstraction status cannot change after subclassing, A is still considered abstract, even though its method is implemented. However, since namespace resolution occurs through the
__dict__
attribute, The method is defined as far as C is concerned. Indeed, if you were to callupdate_abstractmethods(C)
after this code, C would become concrete and usable, since itsfoo
method implemented.In essence, abstract methods shouldn't be changed after the class is used, so this use case is not handled by this implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I disagree with the check for empty
__subclasses__()
, and I think in Irit's last example the right thing to do is to callabc.update_abstractmethods(A)
and thenabc.update_abstractmethods(C)
.Note that it's not technically true that the abstractness cannot change once subclassed -- it may be undocumented but one could just write
A.__abstractmethods__ = ()
.FWIW I do agree that the code should assume that the abstractness of the bases is correct. If some base lies about it, well, Garbage In, Garbage Out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think abc.update_abstractmethods(A) would have any impact here, A never changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was commenting on your earlier example which has
A.foo = lambda ...
-- in that example it certainly would have an effect. :-)My disagreement was with this quote from @bentheiii:
I agree with @iritkatriel that adding that test would be useful (I didn't verify -- maybe it's already added?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, in that case it raises an exception that A has been subclasses so you can't update it anymore. I think there's a test for that already but if not then there should be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see now - you're suggesting to remove that exception. In that case this test is probably needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need this test case to cover the first loop:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iritkatriel What does that test case accomplish other than showing that FooABC is now in an inconsistent state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvanrossum In this comment I left out the update_abstractmethods, see Ben's version which he committed as test_update_del. Without this test the first loop in update_abstractmethods was not exercised.