Skip to content

Should an ABC fail if no abstract methods are defined? #76241

Closed

Description

@AlexCorcoles
BPO 32060
Nosy @rhettinger, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-11-17.17:50:27.874>
created_at = <Date 2017-11-17.12:35:54.986>
labels = ['type-bug', 'invalid']
title = 'Should an ABC fail if no abstract methods are defined?'
updated_at = <Date 2017-11-17.19:45:27.563>
user = 'https://bugs.python.org/AlexCorcoles'

bugs.python.org fields:

activity = <Date 2017-11-17.19:45:27.563>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2017-11-17.17:50:27.874>
closer = 'rhettinger'
components = []
creation = <Date 2017-11-17.12:35:54.986>
creator = 'Alex Corcoles'
dependencies = []
files = []
hgrepos = []
issue_num = 32060
keywords = []
message_count = 6.0
messages = ['306433', '306437', '306449', '306451', '306453', '306455']
nosy_count = 3.0
nosy_names = ['rhettinger', 'r.david.murray', 'Alex Corcoles']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32060'
versions = ['Python 3.5']

Activity

  1. AlexCorcoles commented on Nov 17, 2017

    AlexCorcolesmannequin
    MannequinAuthor

    Hi,

    $ python3
    Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
    >>> import abc
    >>> class Foo(abc.ABC):
    ...     pass
    ... 
    >>> Foo()
    <__main__.Foo object at 0x7f253e6dcb38>

    I think declaring a class as ABC without declaring any abstract method is an error.

    I've tried searching if this was already discussed, but did not find it. Maybe it is related to https://bugs.python.org/issue9731

    Cheers,

    Álex

  2. added
    type-bug
    on Nov 17, 2017
  3. bitdancer commented on Nov 17, 2017

    @bitdancer
    Member

    No, you could create an ABC whose only purpose was to be a target of register calls. As with the issue you reference, this is something better implemented in a linter. I'll leave this open for at least one other dev to concur and close, though.

  4. rhettinger commented on Nov 17, 2017

    @rhettinger
    Contributor

    I concur.

  5. AlexCorcoles commented on Nov 17, 2017

    AlexCorcolesmannequin
    MannequinAuthor

    Are you referring to something akin to Java's marker interfaces? That is, a class with no methods which you inherit from just for semantic meaning (e.g. Java's serializable interface that you implement to indicate that the class is designed for pickling).

    In that case, I suppose the docs could mention this clearly so this is more "discoverable" to noobs like me and prevent further issues like this (I think mine wasn't an unreasonable expectation).

    In any case, it's a minor point and I don't feel I have enough authority to discuss this topic- it's fine to close this.

  6. bitdancer commented on Nov 17, 2017

    @bitdancer
    Member

    The docs start out with "as outlined in PEP-3119; see the PEP for why this was added to Python". I think it would be a good doc enhancement request to extract the motivation and other description out of that pep and put it in the main docs in a suitable form, and make sure this point is included. We used to just treat the peps as extensions of the documentation, but we've found over time that it is better to actually move the important parts of the text into the main docs, since the actual implementation and sometimes even how we talk about the feature tends to evolve away from the original PEP over time.

  7. rhettinger commented on Nov 17, 2017

    @rhettinger
    Contributor

    FWIW, I should have add a bit more explanation. The logic for ABCs instantiation is roughly:

        if any(method.is_abstract() for method in dir(cls)):
            raise TypeError('I refuse to instantiate')
        inst = instantiate(cls)

    The edge case for any() is to default to False for an empty input sequence and this logic applies to ABCs as well.

    Alternatively, you can think of the it as using all() style logic, "instantiate this class only if all of the methods are non-abstract even if the class has no methods at all". The edge case for all() is that it returns True for an empty input.

    Another way of describing the logic is that the normal case for instantiating a class is to create an instance unless there is still work to be done (as evidenced by the presence of one or more abstract methods).

    The abstract methods are essentially a todo list for subclassers. If there are no open todos (even if there were never any todos), instantiation can go forward -- "You can't build that building until you have all the permits, but there are no permit requirements in this case, so go ahead with the construction."

  8. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions