micropython-lib icon indicating copy to clipboard operation
micropython-lib copied to clipboard

abc: add ABC base class

Open alonbl opened this issue 7 months ago • 1 comments

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

alonbl avatar Apr 30 '25 13:04 alonbl

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

rasjidw avatar May 22 '25 10:05 rasjidw

Ping, anyone?

alonbl avatar Jul 05 '25 23:07 alonbl

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?

mattytrentini avatar Jul 06 '25 04:07 mattytrentini

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.

rasjidw avatar Jul 06 '25 05:07 rasjidw

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): ....

dpgeorge avatar Jul 06 '25 05:07 dpgeorge

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.

rasjidw avatar Jul 06 '25 11:07 rasjidw

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.

Actually, this is not completely true, as the cpython ABC class has a method to register another class as a virtual subclass, which affects the result of issubclass and isinstance calls at runtime.

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

So in fact probably better to limit this to the dummy ABC class and the dummy abstractmethod decorator.

rasjidw avatar Jul 06 '25 21:07 rasjidw