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.

Author Antony.Lee
Recipients Antony.Lee
Date 2018-10-25.08:51:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1540457504.91.0.788709270274.issue35063@psf.upfronthosting.co.za>
In-reply-to
Content
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-25 08:51:44Antony.Leesetrecipients: + Antony.Lee
2018-10-25 08:51:44Antony.Leesetmessageid: <1540457504.91.0.788709270274.issue35063@psf.upfronthosting.co.za>
2018-10-25 08:51:44Antony.Leelinkissue35063 messages
2018-10-25 08:51:44Antony.Leecreate