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.

classification
Title: Strict ABC classes
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, uriyyo
Priority: normal Keywords: patch

Created on 2021-02-17 17:28 by uriyyo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24558 closed uriyyo, 2021-02-17 17:31
Messages (5)
msg387169 - (view) Author: Yurii Karabas (uriyyo) * (Python triager) Date: 2021-02-17 17:28
Basically, the idea is to add the ability to mark abstract classes as strict.

What does this mean - in case when a class inherits from a strict abstract base class and doesn't implement all abstract methods then it will fail to create a class.

For instance:
>>> class Iterable(ABC, strict=True):
...   @abstractmethod
...   def __iter__(self):
...      pass

>>> class MyClass(Iterable):
...   pass
TypeError: Can't create class MyClass with unimplemented strict abstract method __iter__

That allows creating abc classes and be sure that class defines a required method, it will be perfrormed on a class declaration stage rather than class instance creation.
msg387172 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-02-17 17:43
Why is this needed?  Already, instantiation is blocked.
msg387181 - (view) Author: Yurii Karabas (uriyyo) * (Python triager) Date: 2021-02-17 18:35
When I work with ABC classes usually I faced a problem -  I forget to implement one of the methods or make a typo in the method name. In such case I will know about it only when I will try to instantiate a class.

In case when a hierarchy is big you should go through classes and find the exact class where the problem is.

With this feature, in a case when a class inherits from strict ABC and doesn't implement all abstract methods of strict classes it will fail at class declaration rather than at instance creation as with regular ABC classes.

As an option, I can run mypy every time before start the python interpreter.

The perfect behavior for me is a case when ABC class will be strict by default, but it will break the existing code.

Examples to explain the idea:
```
from abc import ABC, abstractmethod

class Base(ABC):
    @abstraabstractmethod
    def foo(self):
         pass

class A(Base, ABC):  # totally okay, because class directly inherits from ABC
     pass

class B(Base):  # will fail, because class doesn't implement foo method
    pass
```

Raymond, could you please explain why the current behavior is default.
msg387182 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-02-17 19:03
This discussion is not appropriate for the bug tracker. Try finding a user forum to discuss the pros and cons and history of the current functionality. It is clear that you have plenty of ways to discover the problem already.
msg387183 - (view) Author: Yurii Karabas (uriyyo) * (Python triager) Date: 2021-02-17 19:09
Sorry about this, in a future I will use forum for such discussions.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87409
2021-02-17 19:09:25uriyyosetmessages: + msg387183
2021-02-17 19:03:15gvanrossumsetnosy: - gvanrossum
2021-02-17 19:03:11gvanrossumsetstatus: open -> closed
resolution: rejected
messages: + msg387182

stage: patch review -> resolved
2021-02-17 18:35:04uriyyosetmessages: + msg387181
2021-02-17 17:43:16rhettingersetnosy: + rhettinger, gvanrossum
messages: + msg387172
2021-02-17 17:31:09uriyyosetkeywords: + patch
stage: patch review
pull_requests: + pull_request23339
2021-02-17 17:28:12uriyyocreate