Skip to content

Support type aliases in union passthrough#753

Open
yzhkali wants to merge 2 commits into
python-attrs:mainfrom
yzhkali:fix/union-passthrough-type-alias
Open

Support type aliases in union passthrough#753
yzhkali wants to merge 2 commits into
python-attrs:mainfrom
yzhkali:fix/union-passthrough-type-alias

Conversation

@yzhkali
Copy link
Copy Markdown

@yzhkali yzhkali commented May 31, 2026

Summary

  • unwrap PEP 695 type aliases when union passthrough checks native union members
  • keep spillover handling for non-passthrough union members intact
  • add Python 3.12 coverage for a type alias mixed with a dataclass in the target union

Closes #742

Tests

  • .venv/bin/python -m pytest tests/strategies/test_tagged_unions_695.py tests/strategies/test_union_passthrough.py tests/strategies/test_union_passthrough_695.py
  • .venv/bin/ruff check src/cattrs/strategies/_unions.py tests/strategies/test_union_passthrough_695.py
  • .venv/bin/ruff format --check src/cattrs/strategies/_unions.py tests/strategies/test_union_passthrough_695.py
  • git diff --check

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jun 1, 2026

Merging this PR will improve performance by 21.14%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

#### 🎉 Hooray! `pytest-codspeed` just leveled up to 5.0.3!

A heads-up, this is a breaking change and it might affect your current performance baseline a bit. But here's the exciting part - it's packed with new, cool features and promises improved result stability 🥳!
Curious about what's new? Visit our releases page to delve into all the awesome details about this new version.

⚡ 38 improved benchmarks
✅ 26 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_unstructure_hetero_tuple[BaseConverter] 14.9 µs 11 µs +35.42%
test_unstructure_int[BaseConverter] 15.5 µs 11.9 µs +29.98%
test_unstructure_simple_str_enum[MsgspecJsonConverter] 16.2 µs 12.4 µs +29.93%
test_unstructure_float[Converter] 16.2 µs 12.5 µs +29.52%
test_unstructure_simple_str_enum[BaseConverter] 15.5 µs 12 µs +29.28%
test_unstructure_simple_str_enum[Converter] 16.1 µs 12.4 µs +29.22%
test_unstructure_float[BaseConverter] 15.5 µs 12 µs +29.12%
test_unstructure_int[Converter] 16.1 µs 12.5 µs +29.11%
test_unstructure_simple_str_enum[OrjsonConverter] 16.2 µs 12.6 µs +27.82%
test_unstructure_hetero_tuple[Converter] 17.4 µs 13.8 µs +26.31%
test_unstructure_simple_enum[BaseConverter] 20.6 µs 16.4 µs +25.15%
test_unstructure_simple_enum[Converter] 19.4 µs 15.5 µs +25.13%
test_unstructure_simple_int_enum[Converter] 19.9 µs 16 µs +24.76%
test_unstructure_simple_enum[MsgspecJsonConverter] 19.4 µs 15.6 µs +24.75%
test_unstructure_simple_int_enum[OrjsonConverter] 19.7 µs 15.8 µs +24.66%
test_unstructure_simple_int_enum[MsgspecJsonConverter] 19.7 µs 15.8 µs +24.66%
test_unstructure_simple_enum[OrjsonConverter] 19.3 µs 15.6 µs +23.49%
test_unstructure_simple_int_enum[BaseConverter] 20.9 µs 17.1 µs +22.19%
test_structure_simple_str_enum[BaseConverter] 21.5 µs 17.9 µs +20.2%
test_structure_simple_enum[Converter] 22.5 µs 18.8 µs +20.01%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing yzhkali:fix/union-passthrough-type-alias (81a1dc6) with main (087e1ce)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (a735ca6) during the generation of this report, so 087e1ce was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Tinche
Copy link
Copy Markdown
Member

Tinche commented Jun 1, 2026

Looks ok to me. Please add a changelog entry, and versionchanged blocks to both the function and the docs.

The test failure looks like a GitHub issue?

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.

Union passthrough does not work when target union uses a new-style type alias as element for passthrough

2 participants