param icon indicating copy to clipboard operation
param copied to clipboard

Support declaring a Parameterized class as an ABC

Open maximlt opened this issue 1 year ago • 2 comments

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. https://github.com/holoviz/param/issues/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 https://github.com/holoviz/param/issues/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.

  • [x] Documentation
  • [x] Add more tests

maximlt avatar Feb 16 '25 21:02 maximlt

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 87.93%. Comparing base (b95d476) to head (ce27b13). :warning: Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1031      +/-   ##
==========================================
- Coverage   88.12%   87.93%   -0.19%     
==========================================
  Files           9        9              
  Lines        4857     4875      +18     
==========================================
+ Hits         4280     4287       +7     
- Misses        577      588      +11     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Feb 16 '25 21:02 codecov[bot]

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

maximlt avatar Feb 21 '25 11:02 maximlt

The additional tests and documentation as well as the import change all look good to me!

Made one comment to address but otherwise happy to approve and see this merge once the tests and issue merging to main are sorted.

jlstevens avatar Sep 24 '25 08:09 jlstevens