-
Notifications
You must be signed in to change notification settings - Fork 9
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
Use of super() in base class raises TypeError #36
Comments
This is very curious, I will look into it. |
Ah, my guess is that This may not be what you want, depending on how you want to subclass @dataclass
class Dummy:
def __setattr__(self, name, value):
# do some checks
object.__setattr__(self, name, value)
d = Dummy()
d.x = None which works fine. |
ok thanks! |
Did that solve your issue fully? I still want to get to the bottom of this so I will keep this open regardless. |
Not really, I'm building a lib heavy on mixins so need Since then, I've better understood how super with explicit arguments work so it might be solvable that way. For now I'm doing it manually. Thx for the followup! Brief feedback on the lib: The reason why I went with dataclassy were: Wishlist:
|
Thanks for your detailed reply.
This was nice to read! It's why I advertise dataclassy's small size (25% of the LOC of dataclasses, excluding comments). I place great importance on being able to quickly find your way around and understand how something is done.
Yes, this is a problem with
Everyone is asking for this so it will be added, hopefully soon! |
See #34 (comment) for progress on the last wish. |
By the way, I really like your converter idea too. Better workaround for this issue: from dataclassy import dataclass
@dataclass
class DummyBase:
pass
class Dummy(DummyBase):
def __setattr__(self, name, value):
# do some checks
super().__setattr__(name, value)
d = Dummy()
d.x = None This works because the problem is specific to |
Interesting, I was searching for solutions to this and found that attrs had the same issue python-attrs/attrs#102. I didn't even know it supported slots! The fix is messy, but I expected to have to do something with the function's cell python-attrs/attrs@cd0cc7f. |
I've run into this as well with dataclassy 1.0.1 and python 3.10, making use of >>> from dataclassy import dataclass
>>> @dataclass
... class QuestBase:
... def __init_subclass__(cls, **kwargs):
... super().__init_subclass__(**kwargs)
>>> class Quest(QuestBase):
... pass
...
Traceback (most recent call last):
File "[...]/.pyenv/versions/3.10.7/lib/python3.10/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "[...]/venv/lib/python3.10/site-packages/dataclassy/dataclass.py", line 140, in __new__
return super().__new__(mcs, name, bases, dict_)
File "<console>", line 4, in __init_subclass__
TypeError: super(type, obj): obj must be an instance or subtype of type If you reproduce the original example exactly, a different error but at the same line: >>> from dataclassy import dataclass
>>> @dataclass
... class QuestBase:
... def __init_subclass__(cls, swallow, **kwargs):
... cls.swallow = swallow
... super().__init_subclass__(**kwargs)
...
>>> class Quest(QuestBase, swallow="african"):
... pass
...
Traceback (most recent call last):
File "[...]/.pyenv/versions/3.10.7/lib/python3.10/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "[...]/venv/lib/python3.10/site-packages/dataclassy/dataclass.py", line 140, in __new__
return super().__new__(mcs, name, bases, dict_)
TypeError: QuestBase.__init_subclass__() missing 1 required positional argument: 'swallow' I can make a separate new issue instead if this doesn't belong here. ETA: the suggestion of an empty |
Your comment is in the right place I think. Disappointed this hasn't been fixed in newer CPython versions, assuming it can be. The reason I never worked around this was simply that the fix is hideous and involves rewriting the closure cell with Not tried this but presumably, in lieu of the dummy class, you could just change the call to |
Gotcha - yes I would say not worth rewriting all that for this library. Your suggestion of |
Maybe a note in the readme would be good, as you're the second person at least to run into this. |
IMO cloning is bad practice. This should be avoidable in the first place by using a metaclass rather than decorator interface. |
First of all thanks for a great tool!
For some reason, this raises
TypeError: super(type, obj): obj must be an instance or subtype of type
:The text was updated successfully, but these errors were encountered: