Skip to content

abc: add ABC base class #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

abc: add ABC base class #1003

wants to merge 1 commit into from

Conversation

alonbl
Copy link

@alonbl alonbl commented Apr 30, 2025

this trivial addition will allow less code differences between standard python classes and micropython code.

This trivial addition will allow less code differences between standard
python classes and micropython code.

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
@rasjidw
Copy link

rasjidw commented May 22, 2025

Was about to create an pull request for exactly the same change. Would really like to see this merged into the master branch.

@alonbl
Copy link
Author

alonbl commented Jul 5, 2025

Ping, anyone?

@mattytrentini
Copy link
Contributor

It...doesn't really add much! :P If there were more of an implementation it would be more compelling.

Can you give an example of code that was easier to port with this?

@rasjidw
Copy link

rasjidw commented Jul 6, 2025

From the python docs (https://docs.python.org/3/library/abc.html#abc.abstractmethod), the @abstractmethod decorator requires that the class’s metaclass is abc.ABCMeta or is derived from it. A common way is just to use the abc.ABC class as a base. eg (from the docs):

class C(ABC):
    @abstractmethod
    def my_abstract_method(self, arg1):

My personal use case is at https://github.com/rasjidw/sparrowrpc-python/blob/main/src/sparrowrpc/bases.py where I define a number of base classes using both the ABC class and the @abstractmethod decorator. The codebase is designed to run on both normal cpython and micropython without changes, and having the ABC class available is an essential part of that.

The is no problems with the classes and decorators being no-op classes / functions in micropython since in cpython it is really just there for developer erganomics and doesn't actually affect the runtime operation in any way.

On reflection, it is probably worth adding in the other classes and functions from the abc module as dummy entries too.

@dpgeorge
Copy link
Member

dpgeorge commented Jul 6, 2025

Apart from using a metaclass and slots (neither of which MicroPython supports) this is actually the same as CPython which has:

class ABC(metaclass=ABCMeta):
    """Helper class that provides a standard way to create an ABC using
    inheritance.
    """
    __slots__ = ()

So I think this is a good addition. As mentioned above, it allows doing class MyClass(abc.ABC): ....

@rasjidw
Copy link

rasjidw commented Jul 6, 2025

Can you give an example of code that was easier to port with this?

Just to clarify - it is not so necessary for code that you are porting to micropython, but very useful if you want to have a single codebase that works both with cpython and micropython.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants