8000 bpo-41905: added abc.update_abstractmethods by bentheiii · Pull Request #22485 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41905: added abc.update_abstractmethods #22485 8000

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 21 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions Doc/library/abc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ The :mod:`abc` module also provides the following decorator:
to declare abstract methods for properties and descriptors.

Dynamically adding abstract methods to a class, or attempting to modify the
abstraction status of a method or class once it is created, are not
supported. The :func:`abstractmethod` only affects subclasses derived using
regular inheritance; "virtual subclasses" registered with the ABC's
:meth:`register` method are not affected.
abstraction status of a method or class once it is created, are only
supported using the :func:`update_abstractmethods` function. The
:func:`abstractmethod` only affects subclasses derived using regular
inheritance; "virtual subclasses" registered with the ABC's :meth:`register`
method are not affected.

When :func:`abstractmethod` is applied in combination with other method
descriptors, it should be applied as the innermost decorator, as shown in
Expand Down Expand Up @@ -235,7 +236,6 @@ The :mod:`abc` module also provides the following decorator:
super-call in a framework that uses cooperative
multiple-inheritance.


The :mod:`abc` module also supports the following legacy decorators:

.. decorator:: abstractclassmethod
Expand Down Expand Up @@ -335,6 +335,17 @@ The :mod:`abc` module also provides the following functions:

.. versionadded:: 3.4

.. function:: update_abstractmethods(cls)
A function to recalculate an abstract class's abstraction status. This
function should be called if a class's abstract methods have been
implemented or changed after it was created. Usually, this function should
be called from within class decorator.

Returns *cls*, to allow usage as a class decorator.

If *cls* has any subclasses, raises a :exc:`RuntimeError`.

.. versionadded:: 3.10

.. rubric:: Footnotes

Expand Down
32 changes: 32 additions & 0 deletions Lib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,38 @@ def _abc_caches_clear(cls):
"""Clear the caches (for debugging or testing)."""
_reset_caches(cls)

def update_abstractmethods(cls):
"""Repopulate the abstract methods of an abstract class, or a subclass of
an abstract class.
Copy link
Member

Choose a reason for hiding this comment

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

I would like the summary to fit on one line.

Suggested change
"""Repopulate the abstract methods of an abstract class, or a subclass of
an abstract class.
"""Recalculate the set of abstract methods of an abstract class.


If a class has had one of its abstract methods implemented after the
class was created, the method will not be considered implemented until
this function is called. Alternativly, if a new abstract method has been
added to the class, it will not be considered to require that method
implemented until this function is called.

This function should be called before any use is made of the class,
usually in class decorators that implement methods.

Returns cls, to allow usage as a class decorator.

If cls is has any subclasses, raises a RuntimeError.
"""
if cls.__subclasses__():
raise RuntimeError("cannot update abstract method of class after suclassing")
abstracts = set()
# check the existing abstract methods, keep only the ones that are
# still abstract
for name in cls.__abstractmethods__:
Copy link
Member
@iritkatriel iritkatriel Oct 1, 2020

Choose a reason for hiding this comment

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

Since you didn't check in this function that it's an ABC, it's not guaranteed that cls.__abstractmethods__ works:

>>> class FooABC: pass
... 
>>> FooABC.__abstractmethods__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __abstractmethods__

Maybe add a test for when this function is called for a non-ABC.

value = getattr(cls, name, None)
if getattr(value, "__isabstractmethod__", False):
abstracts.add(name)
# also add any other newly added abstract methods
for name, value in cls.__dict__.items():
if getattr(value, "__isabstractmethod__", False):
abstracts.add(name)
cls.__abstractmethods__ = frozenset(abstracts)
return cls

class ABC(metaclass=ABCMeta):
"""Helper class that provides a standard way to create an ABC using
Expand Down
5 changes: 5 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import keyword
import builtins
import functools
import abc
import _thread
from types import GenericAlias

Expand Down Expand Up @@ -992,6 +993,10 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
cls.__doc__ = (cls.__name__ +
str(inspect.signature(cls)).replace(' -> None', ''))

# update the abstract methods of the class, if it is abstract
if isinstance(cls, abc.ABCMeta):
abc.update_abstractmethods(cls)

return cls


Expand Down
11 changes: 8 additions & 3 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'partial', 'partialmethod', 'singledispatch', 'singledispatchmethod',
'cached_property']

from abc import get_cache_token
from abc import ABCMeta, get_cache_token, update_abstractmethods
from collections import namedtuple
# import types, weakref # Deferred to single_dispatch()
from reprlib import recursive_repr
Expand Down Expand Up @@ -187,15 +187,20 @@ def _lt_from_ge(self, other, NotImplemented=NotImplemented):

def total_ordering(cls):
"""Class decorator that fills in missing ordering methods"""
# Find user-defined comparisons (not those inherited from object).
roots = {op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)}
# Find user-defined comparisons (not those inherited from object or abstract).
roots = {op for op in _convert
if getattr(cls, op, None) is not getattr(object, op, None)
and not getattr(getattr(cls, op, None), '__isabstractmethod__', False)}
Copy link
Contributor

Choose a reason for hiding this comment

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

The nested getattr() isn't very clear.

Also, since this is in the inner loop for a dynamic decorator, it will slow the class creation process. I didn't used to think that was important but experience with named tuples show that people care a great deal about the amount of computation that occurs during an import.

Copy link
Contributor Author
@bentheiii bentheiii Oct 2, 2020

Choose a reason for hiding this comment

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

would using a walrus operator be okay here?

roots = {...
         if (root:=getattr(cls, op, None)) is not getattr(object, op, None)
         and not getattr(root, '__isabstractmethod__', False)}

Copy link
Member

Choose a reason for hiding this comment

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

That seems fine to me.

if not roots:
raise ValueError('must define at least one ordering operation: < > <= >=')
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
for opname, opfunc in _convert[root]:
if opname not in roots:
opfunc.__name__ = opname
setattr(cls, opname, opfunc)
# update the abstract methods of the class, if it is abstract
if isinstance(cls, ABCMeta):
update_abstractmethods(cls)
return cls


Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,42 @@ class B: ...
class C(with_metaclass(abc_ABCMeta, A, B)):
pass
self.assertEqual(C.__class__, abc_ABCMeta)

def test_update_new_abstractmethods(self):
class A(metaclass=abc_ABCMeta):
@abc.abstractmethod
def bar(self):
pass

@abc.abstractmethod
def updated_foo(self):
pass

A.foo = updated_foo
abc.update_abstractmethods(A)
msg = "class A with abstract methods bar, foo"
self.assertEqual(A.__abstractmethods__, {'foo', 'bar'})
self.assertRaisesRegex(TypeError, msg, A)

def test_update_implementation(self):
class A(metaclass=abc_ABCMeta):
@abc.abstractmethod
def foo(self):
pass

class B(A):
pass

msg = "class B with abstract method foo"
self.assertRaisesRegex(TypeError, msg, B)
self.assertEqual(B.__abstractmethods__, {'foo'})

B.foo = lambda self: None

abc.update_abstractmethods(B)

B()
self.assertEqual(B.__abstractmethods__, set())

Copy link
Member
@iritkatriel iritkatriel Oct 1, 2020

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:

import abc
class FooABC(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def bar(self):
        pass

del FooABC.bar
assert ('bar' in FooABC.__abstractmethods__)
assert ('bar' not in FooABC.__dict__)

Copy link
Member

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?

Copy link
Member

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.


class TestABCWithInitSubclass(unittest.TestCase):
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dataclasses import *

import abc
import pickle
import inspect
import builtins
Expand Down Expand Up @@ -3332,6 +3333,42 @@ class C:

## replace(c, x=5)

class TestAbstract(unittest.TestCase):
def test_abc_implementation(self):
class Ordered(abc.ABC):
@abc.abstractmethod
def __lt__(self, other):
pass

@abc.abstractmethod
def __le__(self, other):
pass

@dataclass(order=True)
class Date(Ordered):
year: int
month: 'Month'
day: 'int'

self.assertFalse(inspect.isabstract(Date))
self.assertGreater(Date(2020,12,25), Date(2020,8,31))

def test_maintain_abc(self):
class A(abc.ABC):
@abc.abstractmethod
def foo(self):
pass

@dataclass
class Date(A):
year: int
month: 'Month'
day: 'int'

self.assertTrue(inspect.isabstract(Date))
msg = 'class Date with abstract method foo'
self.assertRaisesRegex(TypeError, msg, Date)


if __name__ == '__main__':
unittest.main()
14 changes: 14 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import collections
import collections.abc
import copy
import inspect
from itertools import permutations
import pickle
from random import choice
Expand Down Expand Up @@ -1157,6 +1158,19 @@ def test_pickle(self):
method_copy = pickle.loads(pickle.dumps(method, proto))
self.assertIs(method_copy, method)

def test_abc_impl(self):
class A(abc.ABC):
@abc.abstractmethod
def __gt__(self, other):
pass

class B(A):
def __lt__(self, other):
return True
B = functools.total_ordering(B)
B()
self.assertFalse(inspect.isabstract(B))

@functools.total_ordering
class Orderable_LT:
def __init__(self, value):
Expand Down
0