Skip to content

pickling of Exceptions and kw_only #734

Description

@arnimarj

Just stumbled across an interesting issue. The kw_only flag seems to break pickling of objects. E.g.:

import attr
import pickle


@attr.s(auto_exc=True, kw_only=False)
class Positional(Exception):
    field: int = attr.ib()


@attr.s(slots=False, auto_exc=True, kw_only=True)
class KwOnly(Exception):
    field: int = attr.ib()


A = Positional(field=1)
B = KwOnly(field=1)

print('A', pickle.loads(pickle.dumps(A)))
print('B', pickle.loads(pickle.dumps(B)))

raises this error

Traceback (most recent call last):
  File "attrs_kwonly.py", line 19, in <module>
    print('B', pickle.loads(pickle.dumps(B)))
TypeError: __init__() takes 1 positional argument but 2 were given

I couldn't find any mention in previous issues, or current documentation about this behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugFly paperIssue that magically attracts sloperators.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions