Are you using python3 to run that code? If yes, you should know that declaring metaclass in python3 have changes you should do it like this instead:

import abc

class AbstractClass(metaclass=abc.ABCMeta):

  @abc.abstractmethod
  def abstractMethod(self):
      return

The full code and the explanation behind the answer is:

import abc

class AbstractClass(metaclass=abc.ABCMeta):

    @abc.abstractmethod
    def abstractMethod(self):
        return

class ConcreteClass(AbstractClass):

    def __init__(self):
        self.me = "me"

# Will get a TypeError without the following two lines:
#   def abstractMethod(self):
#       return 0

c = ConcreteClass()
c.abstractMethod()

If abstractMethod is not defined for ConcreteClass, the following exception will be raised when running the above code: TypeError: Can't instantiate abstract class ConcreteClass with abstract methods abstractMethod

Answer from mouad on Stack Overflow
🌐
Medium
medium.com › @ankitlodh2002 › exploring-pythons-abstraction-abstract-methods-decorators-and-magic-methods-demystified-2c6ec5e14dce
Exploring Python’s Abstraction: Abstract Methods, Decorators, and Magic Methods Demystified | by Ankit Lodh | Medium
December 16, 2023 - Now we can use @my_ABC.my_abstractmethod decorator on the area method of the shape. Now I need to prevent the instantiation of the Square class because the Square class does not implement the area method. To do this we pass the area method of the Shape class through the my_abstractmethod function of the my_ABC class.
Discussions

python - I used `__metaclass__` to set up `abc.ABCMeta` as the metaclass, but unimplemented `@abstractmethod`s still fail to raise an exception. Why? - Stack Overflow
I have read python docs about abstract base classes: From here: abc.abstractmethod(function) A decorator indicating abstract methods. Using this decorator requires that the class’s metacl... More on stackoverflow.com
🌐 stackoverflow.com
Abstract class methods?
This might not be the right place to put this, but is there any interest in adding a decorator for abstract class methods? (And potentially supporting it in MyPy?) Currently, annotating a method wi... More on github.com
🌐 github.com
7
August 1, 2018
Why does using property and abstractmethod not enforce properties in child?
Why does using property and abstractmethod not enforce properties in child · Why should it raise? You have done exactly what an abstract method is for - you implemented some_prop in in a child class · When some_prop is searched for in a ConcreteExample instance, it will look in the instance, ... More on discuss.python.org
🌐 discuss.python.org
0
October 14, 2021
Python override abstractmethod property setter and getter using decorators only - Stack Overflow
Your code raises an exception because ... the abstract method A.foo with the same signature. Remove the A. specification from the decorator, and your code would work: ... With the fix, you'll find that the class A does enforce that the child classes implement both the getter and the setter for foo (the exception you saw was actually a result of you not implementing the setter). ... Sign up to request clarification or add additional context in comments. ... The python doc page I ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › decorating an abstractmethod.
r/learnpython on Reddit: decorating an abstractmethod.
June 14, 2024 -

I have the class inheritance going on like this...

     Base
      |
     Sub
      |
  ---------
  |       |
TaskA   TaskB

Base implements some loggers and Sub implements the config and Tasks implements the run methods. I wanted to catch all the exceptions raised by the tasks and handle them and return False.

I was hoping I would decorate the abstractmethod with a custom decorator so that I don't need all the inheriting Tasks to decorate them. But it is not working as expected.

from abc import ABC, abstractmethod
from functools import wraps

def catch_exceptions_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            self = args[0]
            self.log_error("Error running the task.", e)
            return False
    return wrapper

class Base(ABC):

    def log_error(self, message, exception):
        """Log error."""
        print(f"ERROR: {message} {exception}")

    @abstractmethod
    def configure(self):
        """Configure."""
        pass

class Sub(Base):

    def configure(self):
        """Configure."""
        print("Configuring.")

    @abstractmethod
    @catch_exceptions_decorator
    def run(self):
        """Run."""
        pass

class TaskA(Sub):

    def run(self):
        """Run."""
        raise KeyError
        print("Running.")
        return True

task = TaskA()
assert not taskA.run()

I was expecting the KeyError to be caught by the catch_exceptions_decorator and return False insted of raising the exception as shown below.

Traceback (most recent call last):
  File "/Users/X/Desktop/abc_decorator.py", line 47, in <module>
    assert not task.run()
               ^^^^^^^^^^
  File "/Users/X/Desktop/abc_decorator.py", line 43, in run
    raise KeyError
KeyError

What am I doing wrong here ?

EDIT: Ended up using a metaclass and here is the final code if that's helpful to anyone.

from abc import ABC, ABCMeta, abstractmethod
from functools import wraps
from operator import call
from textwrap import wrap


class TaskMeta(type):

    def __new__(cls, name, bases, attrs):
        """Metaclass for tasks."""

        for attr_name, attr_value in attrs.items():
            if attr_name == "run" and callable(attr_value):
                attrs[attr_name] = cls.catch_exceptions_decorator(attr_value)
        return super().__new__(cls, name, bases, attrs)

    @staticmethod
    def catch_exceptions_decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except Exception as e:
                self = args[0]
                self.log_error("Error running the task.", e)
                return False
        return wrapper


class BaseMeta(ABCMeta, TaskMeta):
    pass


class Base(metaclass=BaseMeta):

    def log_error(self, message, exception):
        """Log error."""
        print(f"ERROR: {message} {exception}")

    @abstractmethod
    def configure(self):
        """Configure."""
        pass


class Sub(Base):

    def configure(self):
        """Configure."""
        print("Configuring.")

    @abstractmethod
    def run(self):
        """Run."""
        pass


class Task(Sub):

    def run(self):
        """Run."""
        raise KeyError
        print("Running.")
        return True


task = Task()
assert not task.run()

Prints the error instead of raising exception.

ERROR: Error running the task. 
🌐
30 Days Coding
30dayscoding.com › blog › abc-import-abc-abstractmethod-python
30 Days Coding
April 27, 2024 - The `abc` module provides two essential ... abstract class that cannot be instantiated on its own. The `abstractmethod` decorator is used to define abstract methods within an abstract class....
🌐
YouTube
youtube.com › watch
How To Use: "@abstractmethod" In Python (Tutorial 2023) - YouTube
Python's @abstractmethod is actually really useful. It helps us keep our code more consistent in certain situations. So let's learn more about it in this Pyt...
Published   February 23, 2023
🌐
YouTube
youtube.com › watch
@abstractmethod explained in Python
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Object-oriented-python
object-oriented-python.github.io › 10_further_object-oriented_features.html
10. Further object-oriented features — Object-oriented Programming documentation
Listing 10.2 An abstract base class version of the Group class. Note that the class itself inherits from ABC, and the methods and attribute to be implemented by the child classes have the abstractmethod decorator.¶
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_module_abc.asp
Python abc Module
Python Examples Python Compiler ... print(sq.area()) Try it Yourself » · The abc module provides tools for creating Abstract Base Classes (ABCs) and decorators for abstract methods....
🌐
GitHub
github.com › python › typing › issues › 577
Abstract class methods? · Issue #577 · python/typing
August 1, 2018 - Currently, annotating a method with both @abstractmethod and @classmethod results in a runtime error. I see some suggestions on how to make this work on StackOverflow, but of course those custom decorators would not be supported in MyPy.
Author   sid-kap
🌐
Astral
docs.astral.sh › ruff › rules › empty-method-without-abstract-decorator
empty-method-without-abstract-decorator (B027) | Ruff
If the method is meant to be abstract, add an @abstractmethod decorator to the method. from abc import ABC class Foo(ABC): def method(self): ... Use instead: from abc import ABC, abstractmethod class Foo(ABC): @abstractmethod def method(self): ... Python documentation: abc Back to top
🌐
Python.org
discuss.python.org › python help
Why does using property and abstractmethod not enforce properties in child? - Python Help - Discussions on Python.org
October 14, 2021 - Example: from abc import ABC, abstractmethod class AbstractExample(ABC): @property @abstractmethod def some_prop(self): pass class ConcreteExample(AbstractExample): def some_prop(self): …
🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
Using this decorator requires that the class’s metaclass is ABCMeta or is derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods and properties are overridden. The abstract methods can be called using any of the normal ‘super’ call mechanisms.
🌐
Python Course
python-course.eu › oop › the-abc-of-abstract-base-classes.php
20. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
We will define now a subclass using the previously defined abstract class. You will notice that we haven't implemented the do_something method, even though we are required to implement it, because this method is decorated as an abstract method with the decorator "abstractmethod".
🌐
DataCamp
datacamp.com › tutorial › python-abstract-classes
Python Abstract Classes: A Comprehensive Guide with Examples | DataCamp
January 22, 2025 - The class is abstract and cannot be instantiated directly, as indicated by this inheritance. By acting as blueprints, abstract classes make sure that any concrete subclass abides by a set of rules. You can define abstract methods inside an abstract class by using the abstractmethod decorator.
🌐
GeeksforGeeks
geeksforgeeks.org › python › abstract-classes-in-python
Abstract Classes in Python - GeeksforGeeks
Abstract properties work like abstract methods but are used for properties. These properties are declared with the @property decorator and marked as abstract using @abstractmethod.
Published   September 3, 2025
🌐
Ikriv
ikriv.com › blog
Python: the land of abstract static methods – Ivan Krivyakov
@abstractmethod is a regular decorator that can technically be applied to any function or class. It is only checked by the constructor of abc.ABC. Any occurrences of @abstractmethod outside of classes descending from abc.ABC are ignored. All of the following is legal in Python as of version ...
🌐
Stack Overflow
stackoverflow.com › questions › 51798285 › python-override-abstractmethod-property-setter-and-getter-using-decorators-only › 51798390
Python override abstractmethod property setter and getter using decorators only - Stack Overflow
Your code raises an exception because you're using the A.foo.setter decorator on the B.foo method, so the B.foo method does not actually implement the abstract method A.foo with the same signature. Remove the A. specification from the decorator, and your code would work: ... With the fix, you'll find that the class A does enforce that the child classes implement both the getter and the setter for foo (the exception you saw was actually a result of you not implementing the setter). ... Sign up to request clarification or add additional context in comments. ... The python doc page I referenced uses @C.x.setter, so I followed the example.