Skip to content
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

Support declaring a Parameterized class as an ABC #1031

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

maximlt
Copy link
Member

@maximlt maximlt commented Feb 16, 2025

Param has its own way of declaring "abstract" classes by annotating them with __abstract = True. Parameterized classes all have an abstract property inherited from the metaclass that returns whether this attribute (mangled) was set. The concrete_descendents function uses _is_abstract to build a collection of the non-abstract descendents only. #84 suggests replacing this approach by Abstract Bases Classes. This MR doesn't replace the current mechanism (we'd need to deprecate it first) but instead attempts to add support to ABCs, allowing users to declare a Parameterized ABC.

To avoid metaclass conflicts, it introduces the ParamaterizedABCclass that users must inherit from when they want to declare an ABC.

import abc
import param

class ModelABC(param.parameterized.ParameterizedABC):

    x = param.Number()
    y = param.Number()

    @abc.abstractmethod
    def run(self):
        """This must be implemented by subclasses."""

class BadModel(ModelABC): pass

class GoodModel(ModelABC):
    def run(self):
        return self.x * self.y

try:
    BadModel()
except Exception as e:
    print(repr(e))
    # TypeError("Can't instantiate abstract class BadModel without an implementation for abstract method 'run'")

gm = GoodModel(x=10, y=2)
print(gm.run())
# 20

print(param.concrete_descendents(ModelABC))
# {'GoodModel': <class '__main__.GoodModel'>}

@sdc50 I know your comment on #84 dates a little (5 years :) ). If you're still interested in this feature, let me know what you think about it. On the HoloViz code bases side, I think we'll need to see whether we can effectively replace __abstract = True.

  • Documentation
  • Add more tests

Copy link

codecov bot commented Feb 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.28%. Comparing base (30edc3f) to head (f065a88).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1031      +/-   ##
==========================================
+ Coverage   87.26%   87.28%   +0.02%     
==========================================
  Files           9        9              
  Lines        4939     4947       +8     
==========================================
+ Hits         4310     4318       +8     
  Misses        629      629              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@jlstevens jlstevens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

The only thing I want to see before this is merged are some tests making sure the decorators used to declare abstract methods, properties etc. work as expected.

In an ideal world we would run the whole test suite again using ParameterizedABC to check everything else works but that seems overkill, especially as this base class is opt-in.

@maximlt
Copy link
Member Author

maximlt commented Feb 21, 2025

@jlstevens I have added some tests and updated the documentation. I also added ParameterizedABC to the top-level module.

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.

2 participants