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

CCLNamedClass does not play nice with dataclass #1118

Open
tilmantroester opened this issue Jul 28, 2023 · 3 comments
Open

CCLNamedClass does not play nice with dataclass #1118

tilmantroester opened this issue Jul 28, 2023 · 3 comments

Comments

@tilmantroester
Copy link
Contributor

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
@damonge
Copy link
Collaborator

damonge commented Jul 31, 2023

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

@tilmantroester
Copy link
Contributor Author

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__?

@damonge
Copy link
Collaborator

damonge commented Aug 1, 2023

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.

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

No branches or pull requests

2 participants