CCL icon indicating copy to clipboard operation
CCL copied to clipboard

`CCLNamedClass` does not play nice with `dataclass`

Open tilmantroester opened this issue 1 year ago • 3 comments

I tried making a dataclass that derived from CCLNamedClass (analogously to the Baryons base class) but this causes an obscure error:

from dataclasses import dataclass
from pyccl import CCLNamedClass

class A(CCLNamedClass):
    pass

@dataclass
class B(A):
    a = 0

B()
Traceback (most recent call last):
  File "pyccl/_core/schema.py", line 262, in __new__
    instance = super().__new__(cls)
TypeError: Can't instantiate abstract class B with abstract method name

tilmantroester avatar Jul 28 '23 21:07 tilmantroester

I think that's because any subclass of CCLNamedClass needs to implement the name method (see here)

damonge avatar Jul 31 '23 07:07 damonge

We should probably add some documentation on when and how to use these classes. Is there a reason for name not defaulting to something like __class__.__name__?

tilmantroester avatar Jul 31 '23 20:07 tilmantroester

It was so you can have a from_name method that you can use with more memorable/meaningful names (e.g. ccl.MassFunction.from_name("Tinker08")). I agree it may be a bit superfluous, since modern IDEs make finding these things quite easy.

damonge avatar Aug 01 '23 08:08 damonge