Skip to content

dataclasses.dataclass wrapper brokes Fraction #134014

Not planned
@TiunovNN

Description

@TiunovNN

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

JelleZijlstra commented on May 14, 2025

@JelleZijlstra
Member

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

added
pendingThe issue will be closed if no feedback is provided
on May 14, 2025
ericvsmith

ericvsmith commented on May 14, 2025

@ericvsmith
Member

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

TiunovNN

TiunovNN commented on May 14, 2025

@TiunovNN
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

ericvsmith commented on May 14, 2025

@ericvsmith
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.

removed
pendingThe issue will be closed if no feedback is provided
on May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      dataclasses.dataclass wrapper brokes Fraction · Issue #134014 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant