Skip to content

Fix pickling auto_exc exceptions with kw_only fields#1573

Open
lin-hongkuan wants to merge 1 commit into
python-attrs:mainfrom
lin-hongkuan:codex/exception-kw-only-reduce
Open

Fix pickling auto_exc exceptions with kw_only fields#1573
lin-hongkuan wants to merge 1 commit into
python-attrs:mainfrom
lin-hongkuan:codex/exception-kw-only-reduce

Conversation

@lin-hongkuan

Copy link
Copy Markdown

Summary

Fixes #734.

This adds an attrs-generated __reduce__ for auto_exc=True exception classes that have init=True keyword-only fields. The generated reducer reconstructs the exception without calling __init__, then restores the normal pickle state, so required keyword-only fields work across plain, frozen, slotted, and frozen-slotted exception classes.

The generation is intentionally narrow: it only applies to attrs-generated exception __init__ methods with keyword-only init fields, leaves positional-only exceptions on BaseException.__reduce__, and does not override custom __reduce__ implementations, including inherited ones.

Tests

  • PYTHONPATH=src python -m pytest -q
  • PYTHONPATH=src python -m ruff check src/attr/_make.py tests/test_functional.py
  • PYTHONPATH=src python -m ruff format --check src/attr/_make.py tests/test_functional.py
  • git diff --check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pickling of Exceptions and kw_only

1 participant