pickling of Exceptions and kw_only

Open
#734 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start by running the attrs_kwonly.py reproducer and compare the Positional and KwOnly exception round trips through pickle. Trace the interaction between auto_exc, kw_only, and exception initialization, then establish whether the expected outcome is a working round trip or documented limitation and add the corresponding regression coverage or documentation.

Written by the indexing model from the issue text.

Description

Bug Fly paper

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.

Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-attrs/attrs

All issues in python-attrs/attrs

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.