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

Error when subclassing a dataclass with a field that uses a defaultfactory #77077

Closed
JohnDidion mannequin opened this issue Feb 21, 2018 · 5 comments
Closed

Error when subclassing a dataclass with a field that uses a defaultfactory #77077

JohnDidion mannequin opened this issue Feb 21, 2018 · 5 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@JohnDidion
Copy link
Mannequin

JohnDidion mannequin commented Feb 21, 2018

BPO 32896
Nosy @ericvsmith, @ned-deily
PRs
  • bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory #6170
  • [3.7] bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory (GH-6170) #6171
  • 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 2018-03-21.09:23:12.424>
    created_at = <Date 2018-02-21.16:21:45.999>
    labels = ['3.7', '3.8', 'type-bug', 'library', 'release-blocker']
    title = 'Error when subclassing a dataclass with a field that uses a defaultfactory'
    updated_at = <Date 2018-03-21.09:23:12.424>
    user = 'https://bugs.python.org/JohnDidion'

    bugs.python.org fields:

    activity = <Date 2018-03-21.09:23:12.424>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2018-03-21.09:23:12.424>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2018-02-21.16:21:45.999>
    creator = 'John Didion'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32896
    keywords = ['patch']
    message_count = 5.0
    messages = ['312496', '312517', '314182', '314188', '314189']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'ned.deily', 'John Didion']
    pr_nums = ['6170', '6171']
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32896'
    versions = ['Python 3.7', 'Python 3.8']

    @JohnDidion
    Copy link
    Mannequin Author

    JohnDidion mannequin commented Feb 21, 2018

    @DataClass
    class Foo:
    x: dict = field(default_factory=dict)

    @DataClass
    class Bar(Foo):
    y: int = 1

    @DataClass
    class Baz(Foo):
    def blorf(self):
    print('hello')

    Foo().x
    {}

    Bar().x
    {}

    Baz().x

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __init__() missing 1 required positional argument: 'x'

    I understand that this is desired behavior when the subclass contains non-default attributes. But subclasses that define no additional attributes should work just the same as those that define only additional default attributes.

    A similar issue was raised and dismissed when dataclasses was in development on GitHub: ericvsmith/dataclasses#112, but that only concerned the case of subclasses defining non-default attributes.

    @JohnDidion JohnDidion mannequin added type-crash A hard crash of the interpreter, possibly with a core dump 3.7 (EOL) end of life labels Feb 21, 2018
    @ericvsmith ericvsmith added the stdlib Python modules in the Lib dir label Feb 21, 2018
    @ericvsmith ericvsmith self-assigned this Feb 21, 2018
    @ericvsmith ericvsmith added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Feb 21, 2018
    @ericvsmith
    Copy link
    Member

    That's a great bug report. Thanks for the tiny code to replicate it.

    It turns out the code isn't quite doing what I thought. I'll have to give some thought to exactly how I'm going to handle this without breaking other cases, but I should have it fixed soon.

    Thanks again.

    @ericvsmith
    Copy link
    Member

    New changeset 8f6eccd by Eric V. Smith in branch 'master':
    bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory (GH-6170)
    8f6eccd

    @ericvsmith
    Copy link
    Member

    New changeset 22136c9 by Eric V. Smith (Miss Islington (bot)) in branch '3.7':
    bpo-32896: Fix error when subclassing a dataclass with a field that uses a default_factory (GH-6170) (GH-6171)
    22136c9

    @ericvsmith
    Copy link
    Member

    Thanks again for the bug report. This ended up being a simple fix, but an important one.

    @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 3.8 only security fixes release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant