This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Checking for abstractmethod implementation fails to consider MRO for builtins
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, iritkatriel, xtreak
Priority: normal Keywords:

Created on 2018-10-25 08:51 by Antony.Lee, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg328419 - (view) Author: Antony Lee (Antony.Lee) * Date: 2018-10-25 08:51
When checking whether a class implements all abstractmethods (to know whether the class can be instantiated), one should only consider methods that come *before* the abstractmethod in the MRO -- methods that come after cannot be said to override the abstractmethod.  Indeed, this is currently the case:

    from abc import ABC, abstractmethod

    class NeedsFoo(ABC):
        foo = abstractmethod(lambda self: None)

    class HasFoo(ABC):
        foo = lambda self: None

    class FooImplFirst(HasFoo, NeedsFoo): pass
    class FooImplSecond(NeedsFoo, HasFoo): pass

    try: FooImplSecond()
    except TypeError: pass
    else: raise Exception("Expected error")

Here FooImplFirst correctly overrides the foo method (using the HasFoo mixin first), so can be instantiated; FooImplSecond doesn't (by the MRO, FooImplSecond().foo would resolve to the abstract implementation), and we get a TypeError on instantiation.

However, this is not the case when considering builtins:

    from abc import ABC, abstractmethod

    class NeedsKeys(ABC):
        keys = abstractmethod(lambda self: None)

    HasKeys = dict  # dict has a keys method.

    class KeysImplFirst(HasKeys, NeedsKeys): pass
    class KeysImplSecond(NeedsKeys, HasKeys): pass

    try: KeysImplSecond()
    except TypeError: pass
    else: raise Exception("Expected error")

This example differs from the first only by having dict be the mixin that provides the keys method.  However, running this example shows that KeysImplSecond() will incorrectly succeed: the ABC machinery does not realize that the keys method has not been overridden.

(Alternatively, one could say that "providing the method later in the MRO" is also sufficient; I think that goes against the expectations about ABCs but at least consistency between the non-builtin and builtin cases would be better.)
msg407805 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-06 13:46
See also issue37927.
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79244
2021-12-06 13:46:09iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.7
nosy: + iritkatriel

messages: + msg407805

type: behavior
2018-10-28 20:33:56terry.reedyunlinkissue33396 dependencies
2018-10-28 20:27:50terry.reedylinkissue33396 dependencies
2018-10-25 11:02:19xtreaksetnosy: + xtreak
2018-10-25 08:51:44Antony.Leecreate