Skip to content

dataclasses.dataclass wrapper brokes Fraction #134014

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

Closed
TiunovNN opened this issue May 14, 2025 · 4 comments
Closed

dataclasses.dataclass wrapper brokes Fraction #134014

TiunovNN opened this issue May 14, 2025 · 4 comments
Labels
stdlib Python modules in the Lib dir topic-dataclasses type-bug An unexpected behavior, bug, or error

Comments

@TiunovNN
Copy link

TiunovNN commented May 14, 2025

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

@TiunovNN TiunovNN added the type-bug An unexpected behavior, bug, or error label May 14, 2025
@JelleZijlstra
Copy link
Member

Why would you expect this to work? The dataclass decorator shouldn't be called on arbitrary library classes.

@skirpichev skirpichev added the pending The issue will be closed if no feedback is provided label May 14, 2025
@ericvsmith
Copy link
Member

I agree with Jelle. Further, what would you expect using @dataclass to do? What are you trying to accomplish?

@TiunovNN
Copy link
Author

What are you trying to accomplish?

Actually, i've came across this issue, when using marshmallow-dataclass

from dataclasses import dataclass
from fractions import Fraction

import marshmallow_dataclass


@dataclass
class CustomClass:
    value: Fraction

print(Fraction('30'))
marshmallow_dataclass.class_schema(CustomClass)
print(Fraction('30'))

But the question is why @dataclass mutates the original class rather than create the new one?

@ericvsmith
Copy link
Member

But the question is why @DataClass mutates the original class rather than create the new one?

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.

@picnixz picnixz added the stdlib Python modules in the Lib dir label May 15, 2025
@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2025
@skirpichev skirpichev removed the pending The issue will be closed if no feedback is provided label May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-dataclasses type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants