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: frozen should not be inherited for non-dataclass derived classes #77134

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

Comments

@ericvsmith
Copy link
Member

BPO 32953
Nosy @rhettinger, @ericvsmith
PRs
  • bpo-32953: Dataclasses: frozen should not be inherited for non-dataclass derived classes #6147
  • [3.7] bpo-32953: Dataclasses: frozen should not be inherited for non-dataclass derived classes (GH-6147) #6148
  • 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-19.01:04:16.336>
    created_at = <Date 2018-02-25.22:23:51.445>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'Dataclasses: frozen should not be inherited for non-dataclass derived classes'
    updated_at = <Date 2018-03-19.01:04:16.312>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2018-03-19.01:04:16.312>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2018-03-19.01:04:16.336>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2018-02-25.22:23:51.445>
    creator = 'eric.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32953
    keywords = ['patch']
    message_count = 5.0
    messages = ['312866', '312867', '313075', '314070', '314071']
    nosy_count = 2.0
    nosy_names = ['rhettinger', 'eric.smith']
    pr_nums = ['6147', '6148']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32953'
    versions = ['Python 3.7', 'Python 3.8']

    @ericvsmith
    Copy link
    Member Author

    Reported by Raymond Hettinger:

    When working on the docs for dataclasses, something unexpected came up. If a dataclass is specified to be frozen, that characteristic is inherited by subclasses which prevents them from assigning additional attributes:

        >>> @dataclass(frozen=True)
        class D:
                x: int = 10
    
        >>> class S(D):
                pass
    
        >>> s = S()
        >>> s.cached = True
        Traceback (most recent call last):
          File "<pyshell#49>", line 1, in <module>
            s.cached = True
          File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/dataclasses.py", line 448, in _frozen_setattr
            raise FrozenInstanceError(f'cannot assign to field {name!r}')
        dataclasses.FrozenInstanceError: cannot assign to field 'cached'

    Other immutable classes in Python don't behave the same way:

        >>> class T(tuple):
                pass
    >>> t = T([10, 20, 30])
    >>> t.cached = True
    
        >>> class F(frozenset):
                pass
    >>> f = F([10, 20, 30])
    >>> f.cached = True
    
        >>> class B(bytes):
                pass
    >>> b = B()
    >>> b.cached = True
    

    Raymond

    @ericvsmith ericvsmith added 3.7 (EOL) end of life 3.8 only security fixes labels Feb 25, 2018
    @ericvsmith ericvsmith self-assigned this Feb 25, 2018
    @ericvsmith ericvsmith added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Feb 25, 2018
    @ericvsmith
    Copy link
    Member Author

    A related issue is that dataclasses derived from frozen dataclasses are automatically "promoted" to being frozen.

    >>> @dataclass(frozen=True)
    ... class A:
    ...     i: int
    ...
    >>> @dataclass
    ... class B(A):
    ...     j: int
    ...
    >>> b = B(1, 2)
    >>> b.j = 3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\home\eric\local\python\cpython\lib\dataclasses.py", line 452, in _frozen_setattr
        raise FrozenInstanceError(f'cannot assign to field {name!r}')
    dataclasses.FrozenInstanceError: cannot assign to field 'j'

    If this can't be addressed before 3.7, maybe it should be an error to declare B as non-frozen and then we can properly fix it in a future release.

    @ericvsmith
    Copy link
    Member Author

    @ericvsmith ericvsmith changed the title Dataclasses: frozen should not be inherited Dataclasses: frozen should not be inherited for non-dataclass derived classes Feb 28, 2018
    @ericvsmith
    Copy link
    Member Author

    New changeset f199bc6 by Eric V. Smith in branch 'master':
    bpo-32953: Dataclasses: frozen should not be inherited for non-dataclass derived classes (bpo-6147)
    f199bc6

    @ericvsmith
    Copy link
    Member Author

    New changeset 4564831 by Eric V. Smith (Miss Islington (bot)) in branch '3.7':
    bpo-32953: Dataclasses: frozen should not be inherited for non-dataclass derived classes (GH-6147) (GH-6148)
    4564831

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