Skip to content
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

dataclass __foo__ methods inheritance is not working #81666

Closed
Colin-b mannequin opened this issue Jul 2, 2019 · 4 comments
Closed

dataclass __foo__ methods inheritance is not working #81666

Colin-b mannequin opened this issue Jul 2, 2019 · 4 comments
Assignees
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Colin-b
Copy link
Mannequin

Colin-b mannequin commented Jul 2, 2019

BPO 37485
Nosy @ericvsmith, @tirkarthi, @Colin-b
Files
  • inheritance_dataclass_issue.py: Sample
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ericvsmith'
    closed_at = <Date 2019-07-02.16:22:27.693>
    created_at = <Date 2019-07-02.13:44:03.277>
    labels = ['3.7', 'invalid', 'type-bug', 'library']
    title = 'dataclass __foo__ methods inheritance is not working'
    updated_at = <Date 2019-07-02.16:36:56.595>
    user = 'https://github.com/Colin-b'

    bugs.python.org fields:

    activity = <Date 2019-07-02.16:36:56.595>
    actor = 'colin-b'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2019-07-02.16:22:27.693>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2019-07-02.13:44:03.277>
    creator = 'colin-b'
    dependencies = []
    files = ['48453']
    hgrepos = []
    issue_num = 37485
    keywords = []
    message_count = 4.0
    messages = ['347142', '347146', '347149', '347150']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'xtreak', 'colin-b']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37485'
    versions = ['Python 3.7']

    @Colin-b
    Copy link
    Mannequin Author

    Colin-b mannequin commented Jul 2, 2019

    Hi,

    When using inheritance with dataclass, "standard" instance methods that are provided with a default behavior thanks to dataclass are not overridable using inheritance.

    Please see the sample below (or the attached file):

    import dataclasses
    
    
    @dataclasses.dataclass
    class A:
        def __eq__(self, other):
            return True
    
    
    @dataclasses.dataclass
    class B(A):
        pass

    print(A() == 1) # Returns True as expected
    print(B() == 1) # Returns False instead of True as expected via inheritance

    Thanks again

    @Colin-b Colin-b mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 2, 2019
    @tirkarthi
    Copy link
    Member

    https://docs.python.org/3/library/dataclasses.html#inheritance

    When the dataclass is being created by the dataclass() decorator, it looks through all of the class’s base classes in reverse MRO (that is, starting at object) and, for each dataclass that it finds, adds the fields from that base class to an ordered mapping of fields. After all of the base class fields are added, it adds its own fields to the ordered mapping. All of the generated methods will use this combined, calculated ordered mapping of fields. Because the fields are in insertion order, derived classes override base classes.

    I think here in the example __eq__ is defined in A and is inherited by B but since eq=False is not explicitly defined in the decorator the dataclass will use it's own definition of eq and override it at [0] . So using eq=False will work as expected. I propose closing this as not a bug.

    import dataclasses
    
    
    @dataclasses.dataclass
    class A:
        def __eq__(self, other):
            return True

    @dataclasses.dataclass(eq=False) # By default eq=True
    class B(A):
    pass

    print(A() == 1) # Returns True as expected
    print(B() == 1) # Returns True since inherited A.__eq__ is not overriden with eq=False

    Output

    ./python.exe ../backups/bpo37485.py
    True
    True

    [0]

    if eq:

    @ericvsmith
    Copy link
    Member

    I agree with xtreak that this works as designed and isn't a bug.

    @ericvsmith ericvsmith self-assigned this Jul 2, 2019
    @Colin-b
    Copy link
    Mannequin Author

    Colin-b mannequin commented Jul 2, 2019

    I personally find it easier to understand if inheritance is the default behaviour, instead of having to explicitly disable a feature that's seems primarily designed for non-herited dataclasses.

    But whatever suits best the community :-)

    Thanks for the quick reply

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants