You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong NotImplementedError documentation regarding abstract base classes #158911
The current documentation for NotImplementedError on docs.python.org contains a conceptual and technical contradiction regarding its use in abstract class methods.
Current cpython Documentation
From /Doc/builtins/exceptions.rst:
"In user-defined base classes, abstract methods should raise this exception when they require derived classes to override the method, or while the class is being developed to indicate that the real implementation still needs to be added."
The Problem
This documentation text advises developers to raise `NotImplementedError inside abstract class methods.,This is incorrect, as the Python interpreter will never execute the exception-raising code.
Basic Example (Schematic Code)
classBase(metaclass=ABCMeta):
@abstractmethoddefmeow(self):
raiseNotImplementedError("I am not able to meow right now, this will change in version 0.3")
The Python Interpreter will never output the "I am not able to ..." message which the following code will prove.
PoC Code 1
fromabcimportABCMeta, abstractmethodclassBase(metaclass=ABCMeta):
@abstractmethoddefmeow(self):
raiseNotImplementedError("Must override") # This code is unreachablei=Base()
i.meow()
Calling i.meow()raises: TypeError: Can't instantiate abstract class Base without an implementation for abstract method 'meow', the NotImplementedError exception code never will be executed.
PoC Code 2 (Multi-Inheritance)
fromabcimportABCMeta, abstractmethodclassBase(metaclass=ABCMeta):
@abstractmethoddefmeow(self):
raiseNotImplementedError("Must override") # This code is unreachableclassTiger(Base):
defother(self):
print('other')
i=Tiger()
i.meow()
Calling i.meow()raises: TypeError: Can't instantiate abstract class Tiger without an implementation for abstract method 'meow', the NotImplementedError exception code never will be executed.
Correct Context
The Python interpreter behaves 100% correctly. The error lies solely within the Pythondocumentation, which incorrectly recommends the pattern of raising NotImplementedErrorinsideabstract methods.
Raising NotImplementedError only makes sense in non-abstractclass-methods
Real-World Impact (Pylint)
The current Python documentation text has a cascading effect on the Python ecosystem. Most notably, Pylint relies heavily on this exact wording for its code checks. Because the documentation explicitly commands that abstract methods should raise this exception, Pylint's abstract method checking has been incorrectly implemented.
Proposed Changes
Suggested Replacement Text:
"In user-defined base classes, any non-abstract method should raise this exception when derived classes are required to override the method, indicating that the real implementation still needs to be added."
Documentation
Python Versions
Python 3.10 / 3.11 / 3.12 / 3.13 / 3.14 / 3.15 / 3.16 beta
Description
The current documentation for
NotImplementedErroron docs.python.org contains a conceptual and technical contradiction regarding its use in abstract class methods.Current cpython Documentation
From
/Doc/builtins/exceptions.rst:"In user-defined base classes, abstract methods should raise this exception when they require derived classes to override the method, or while the class is being developed to indicate that the real implementation still needs to be added."
The Problem
This documentation text advises developers to raise `NotImplementedError inside abstract class methods.,This is incorrect, as the Python interpreter will never execute the exception-raising code.
Basic Example (Schematic Code)
The Python Interpreter will never output the "I am not able to ..." message which the following code will prove.
PoC Code 1
Calling
i.meow()raises: TypeError: Can't instantiate abstract class Base without an implementation for abstract method 'meow', theNotImplementedErrorexception code never will be executed.PoC Code 2 (Multi-Inheritance)
Calling
i.meow()raises: TypeError: Can't instantiate abstract class Tiger without an implementation for abstract method 'meow', theNotImplementedErrorexception code never will be executed.Correct Context
The Python interpreter behaves 100% correctly. The error lies solely within the Python documentation, which incorrectly recommends the pattern of raising
NotImplementedErrorinside abstract methods.Real-World Impact (Pylint)
The current Python documentation text has a cascading effect on the Python ecosystem. Most notably, Pylint relies heavily on this exact wording for its code checks. Because the documentation explicitly commands that abstract methods should raise this exception, Pylint's abstract method checking has been incorrectly implemented.
Proposed Changes
Suggested Replacement Text:
"In user-defined base classes, any non-abstract method should raise this exception when derived classes are required to override the method, indicating that the real implementation still needs to be added."
References
abstract-method? pylint-dev/pylint#10054 (comment) (current Pylint discussion)Linked PRs