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

Created on 2018-10-25 08:51 by Antony.Lee, last changed 2018-10-25 11:02 by xtreak.

Messages (1)
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

    FooImplFirst()
    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

    KeysImplFirst()
    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.)
History
Date User Action Args
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