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

dataclasses slots with init=False field raises AttributeException #88815

Closed
tefra mannequin opened this issue Jul 15, 2021 · 7 comments
Closed

dataclasses slots with init=False field raises AttributeException #88815

tefra mannequin opened this issue Jul 15, 2021 · 7 comments
Assignees
Labels
3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tefra
Copy link
Mannequin

tefra mannequin commented Jul 15, 2021

BPO 44649
Nosy @ericvsmith, @miss-islington, @tefra
PRs
  • bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False #29692
  • [3.10] bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692) #29704
  • 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 2021-11-22.13:29:26.395>
    created_at = <Date 2021-07-15.19:06:03.054>
    labels = ['type-bug', 'library', '3.10', '3.11']
    title = 'dataclasses slots with init=False field raises AttributeException'
    updated_at = <Date 2021-11-22.13:47:49.731>
    user = 'https://github.com/tefra'

    bugs.python.org fields:

    activity = <Date 2021-11-22.13:47:49.731>
    actor = 'miss-islington'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2021-11-22.13:29:26.395>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2021-07-15.19:06:03.054>
    creator = 'tefra'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44649
    keywords = ['patch']
    message_count = 7.0
    messages = ['397575', '397653', '397678', '401039', '406772', '406774', '406776']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'miss-islington', 'tefra']
    pr_nums = ['29692', '29704']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44649'
    versions = ['Python 3.10', 'Python 3.11']

    @tefra
    Copy link
    Mannequin Author

    tefra mannequin commented Jul 15, 2021

    I am trying the new slots directive but I get an AttributeError when I try to access a field with init=False

    >>> from dataclasses import dataclass, field
    >>> 
    >>> @dataclass(slots=True)
    ... class Example:
    ...     a: str
    ...     b: str = field(default="b", init=False)
    ... 
    >>> obj = Example("a")
    >>> obj.__slots__
    ('a', 'b')
    >>> obj.
    obj.a  obj.b  
    >>> obj.a
    'a'
    >>> obj.b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: b. Did you mean: 'b'?
    >>> 

    ❯ python --version
    Python 3.10.0b4+

    @tefra tefra mannequin added 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 15, 2021
    @ericvsmith
    Copy link
    Member

    I created bpo-44655 for the confusing error message.

    The problem with dataclasses is that the instance variable 'b' needs to be initialized, instead of the current dataclasses behavior where it relies on the class variable when reading 'b'.

    @ericvsmith
    Copy link
    Member

    With Pablo's changes, the error now reads (in 3.11):

    obj.b
    ^^^^^
    

    AttributeError: 'Example' object has no attribute 'b'

    Which is a vast improvement!

    I'm working on a PR to initialize obj.b in __init__.

    @tefra
    Copy link
    Mannequin Author

    tefra mannequin commented Sep 4, 2021

    Thanks for the update eric, yeah the new error messages are much more comprehensive and I am glad to hear the original issue will be addressed as well.

    @ericvsmith ericvsmith added 3.11 only security fixes labels Nov 22, 2021
    @ericvsmith
    Copy link
    Member

    New changeset d3062f6 by Eric V. Smith in branch 'main':
    bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)
    d3062f6

    @ericvsmith
    Copy link
    Member

    Thanks for the bug report!

    @miss-islington
    Copy link
    Contributor

    New changeset 10343bd by Miss Islington (bot) in branch '3.10':
    bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)
    10343bd

    @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.10 only security fixes 3.11 only security fixes 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