◐ Shell
reader mode source ↗
Skip to content

bpo-41905: added abc.update_abstractmethods#22485

Merged
gvanrossum merged 21 commits into
python:masterfrom
bentheiii:master
Oct 6, 2020
Merged

bpo-41905: added abc.update_abstractmethods#22485
gvanrossum merged 21 commits into
python:masterfrom
bentheiii:master

Conversation

@bentheiii

@bentheiii bentheiii commented Oct 1, 2020

Copy link
Copy Markdown
Contributor

@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@bentheiii

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@ericvsmith ericvsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide comment

Although I'm no expert on ABC's, this looks good to me, with a few noted tiny changes. I'll do some more research before signing off on this, though.

My only real concern is that dataclasses now imports abc. We've jumped through hoops to make dataclasses not import typing, because it's slow to import. I don't know if abc brings the same issues, but I'll try to do some investigating. The trick it uses for typing is to check if 'typing' is in sys.modules before checking for typing.<somthing_or_other>. I'm not sure such trickery is warranted here. Probably not, since just starting the REPL looks like it imports abc (but not typing).

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@bentheiii

Copy link
Copy Markdown
Contributor Author

I have made the requested changes; please review again

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@ericvsmith: please review the changes made to this pull request.

@ericvsmith

Copy link
Copy Markdown
Member

Since update_abstractmethods is documented as being usable as a decorator, there should be a test for that. I realize it's trivially obvious that it works, but still. Although I guess you'll need another decorator that adds an abstract method (like dataclasses used to do) in order to test it. And then use them together.

@ericvsmith

Copy link
Copy Markdown
Member

Since update_abstractmethods is documented as being usable as a decorator, there should be a test for that. I realize it's trivially obvious that it works, but still. Although I guess you'll need another decorator that adds an abstract method (like dataclasses used to do) in order to test it. And then use them together.

Change looks good. Thanks.

@bentheiii

Copy link
Copy Markdown
Contributor Author

My only real concern is that dataclasses now imports abc. We've jumped through hoops to make dataclasses not import typing, because it's slow to import. I don't know if abc brings the same issues, but I'll try to do some investigating. The trick it uses for typing is to check if 'typing' is in sys.modules before checking for typing.<somthing_or_other>. I'm not sure such trickery is warranted here. Probably not, since just starting the REPL looks like it imports abc (but not typing).

If this does prove to be an issue, then we can change the isinstance(cls, ABCMeta) checking to hasattr(cls, '__abstractmethods__') (no sys.modules check necessary), and then import abc.update_abstractmethods before calling it inside the "if" block.

25 hidden items Load more…

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide comment

I just don't care enough about the purely hypothetical scenario you're describing to want to complicate this function. The automatic solution also has its problems (to begin, it would require C code).

@gvanrossum

Copy link
Copy Markdown
Member

Just noted this:

@ericvsmith

My only real concern is that dataclasses now imports abc.

I looked into this, dataclasses already imports functools which already imports abc, so there's no new module being loaded. Also, abc.py is pretty compact and backed by C code -- most of the Python code is in _py_abc.py which is only loaded if _abc (the C code) cannot be loaded for some reason.

So I don't think there's a worry. @ericvsmith, all your other comments seem to have been taken care of this -- can you sign off? (Unless you want to jump into the debate about subclasses.)

@ericvsmith

Copy link
Copy Markdown
Member

@gvanrossum: Agreed about importing abc. I'll let you and the others work out the subclass issue.

bentheiii and others added 3 commits October 5, 2020 12:56
Co-authored-by: Guido van Rossum <guido@python.org>
Co-authored-by: Guido van Rossum <guido@python.org>
Co-authored-by: Guido van Rossum <guido@python.org>
@bentheiii

Copy link
Copy Markdown
Contributor Author

Of course, I defer judgement to the dev team, but I'd be remiss if I didn't reflect the implications of this behavior:

When this behavior is implemented, it will effectively split all class decorators into one of three categories:

  1. ABC-agnostic, who don't consider abstract superclasses as a use case. total_ordering will be of this category.
  2. ABC-aware, subclass-agnostic, who accept the possibility of an abstract superclass, and thus call update_abstractmethods. dataclass will be of this of this category, as well as, update_abstractmethods, techinally.
  3. ABC-aware, subclass-aware, who accept the possibility of an abstract superclass, and call update_abstractmethods, and also accept possibility of it having subclasses, so they also call update_abstractmethods on its subclasses (perhaps even recursively).

An important consequence is, that decorators can only be reliably stacked on top of each other only if they are of the same category. And while update_abstractmethods can be used to adapt a category 1 decorator to category 2, there are no simple means to adapt to category 3.

Of course, the mere existence of a decorator of the third category is a case so far fetched that I doubt it even exists across all python code. This is a valid reason to discount it, but the implication is that if were to ever exist, it could not be used with virtually any other decorator.

@ericvsmith ericvsmith dismissed their stale review October 5, 2020 14:14

Importing abc is not a concern: it will already be imported via functools.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hide comment

You can consider us amply warned.

@gvanrossum

Copy link
Copy Markdown
Member

@rhettinger any final word? @iritkatriel any comments?

@iritkatriel

Copy link
Copy Markdown
Member

@rhettinger any final word? @iritkatriel any comments?

A very pedantic one, which should not block this PR being merged. The doc says "It does not update any subclasses", but no test asserts this.

@gvanrossum gvanrossum merged commit bef7d29 into python:master Oct 6, 2020
@gvanrossum

Copy link
Copy Markdown
Member

Thanks! All merged.

shihai1991 added a commit to shihai1991/cpython that referenced this pull request Oct 9, 2020
* origin/master: (147 commits)
  Fix the attribute names in the docstring of GenericAlias (pythonGH-22594)
  bpo-39337: Add a test case for normalizing of codec names (pythonGH-19069)
  bpo-41557: Update Windows installer to use SQLite 3.33.0 (pythonGH-21960)
  bpo-41976: Fix the fallback to gcc of ctypes.util.find_library when using gcc>9 (pythonGH-22598)
  bpo-41306: Allow scale value to not be rounded (pythonGH-21715)
  bpo-41970: Avoid test failure in test_lib2to3 if the module is already imported (pythonGH-22595)
  bpo-41376: Fix the documentation of `site.getusersitepackages()` (pythonGH-21602)
  Revert "bpo-26680: Incorporate is_integer in all built-in and standard library numeric types (pythonGH-6121)" (pythonGH-22584)
  bpo-41923: PEP 613: Add TypeAlias to typing module (python#22532)
  Fix comment about PyObject_IsTrue. (pythonGH-22343)
  bpo-38605: Make 'from __future__ import annotations' the default (pythonGH-20434)
  bpo-41905: Add abc.update_abstractmethods() (pythonGH-22485)
  bpo-41944: No longer call eval() on content received via HTTP in the UnicodeNames tests (pythonGH-22575)
  bpo-41944: No longer call eval() on content received via HTTP in the CJK codec tests (pythonGH-22566)
  Post 3.10.0a1
  Python 3.10.0a1
  bpo-41584: clarify when the reflected method of a binary arithemtic operator is called (python#22505)
  bpo-41939: Fix test_site.test_license_exists_at_url() (python#22559)
  bpo-41774: Tweak new programming FAQ entry (pythonGH-22562)
  bpo-41936. Remove macros Py_ALLOW_RECURSION/Py_END_ALLOW_RECURSION (pythonGH-22552)
  ...
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
This function recomputes `cls.__abstractmethods__`.
Also update `@dataclass` to use it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants