Not planned
Description
Bug report
Bug description:
from dataclasses import dataclass
from fractions import Fraction
print(Fraction('30/1'))
dataclass(Fraction)
print(Fraction('30/1'))
If the dataclass
function is called with argument Fraction
, it brokes __init__
for Fraction
output is:
30
Traceback (most recent call last):
File "/Users/tiunovnn/git/video-compression-model/web/api/src/tasks/test.py", line 6, in <module>
print(Fraction('30/1'))
~~~~~~~~^^^^^^^^
TypeError: Fraction.__init__() takes 1 positional argument but 2 were given
expected output is:
30
30
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Activity
JelleZijlstra commentedon May 14, 2025
Why would you expect this to work? The
dataclass
decorator shouldn't be called on arbitrary library classes.ericvsmith commentedon May 14, 2025
I agree with Jelle. Further, what would you expect using
@dataclass
to do? What are you trying to accomplish?TiunovNN commentedon May 14, 2025
Actually, i've came across this issue, when using marshmallow-dataclass
But the question is why
@dataclass
mutates the original class rather than create the new one?ericvsmith commentedon May 14, 2025
Ah, that’s a different question. It’s because it was the least surprising behavior (to me, at least). The idea is that it just some adds stuff to a class, so you don’t have to type it yourself. In any event, it’s not going to change at this point, since it would break code.