-
Notifications
You must be signed in to change notification settings - Fork 10
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
HelpTheme
as dataclass
rather than NamedTuple
#163
Conversation
I tried to directly inherits from your new
|
Do you need it to be a non-frozen dataclass? |
Unfreezing it would allow me to streamline my code with: @dataclass
class HelpExtraTheme(cloup.HelpTheme):
critical: IStyle = identity
error: IStyle = identity
warning: IStyle = identity
info: IStyle = identity
debug: IStyle = identity
"""Log levels from Python's logging module."""
... Else I have to build up a new class programmatically out of Freezing that dataclass is tempting and I understand the appeal. But I guess that's not critical. I'm probably the only one on the planet trying to play with the internals of |
Sorry, it's still not clear to me why you can't @dataclass(frozen=True)
class HelpExtraTheme(cloup.HelpTheme):
... |
Because I'm stupid! 😓 I apologize for the back and forth, I'm just discovering this implementation detail. You are right: it is indeed possible to subclass a frozen dataclass. All you need to make it frozen too. And mine wasn't. Here is the proof: Python 3.11.4 (main, Jun 20 2023, 17:23:00) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from dataclasses import dataclass
>>> @dataclass(frozen=True)
... class HelpTheme:
... ...
...
>>> HelpTheme
<class '__main__.HelpTheme'>
>>> @dataclass
... class HelpExtraTheme(HelpTheme):
... ...
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../python3.11/dataclasses.py", line 1230, in dataclass
return wrap(cls)
^^^^^^^^^
File ".../python3.11/dataclasses.py", line 1220, in wrap
return _process_class(cls, init, repr, eq, order, unsafe_hash,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../python3.11/dataclasses.py", line 988, in _process_class
raise TypeError('cannot inherit non-frozen dataclass from a '
TypeError: cannot inherit non-frozen dataclass from a frozen one
>>> @dataclass(frozen=True)
... class HelpExtraTheme(HelpTheme):
... ...
...
>>> HelpExtraTheme
<class '__main__. HelpExtraTheme'> Just tried this branch against my code and it works! 🎉 See: https://github.com/kdeldycke/click-extra/pull/704/files So yeah, you can merge this PR. LGTM! |
Also, I had to redefine the |
Thanks for the merge! 🎉 |
Fixes #159.
HelpTheme
type to allow sub-classing #159@kdeldycke Does this solve your issue?