From a96d30373d867ffa63ca4fd666e7dae2ab85ba8c Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 08:17:52 -0400 Subject: [PATCH 01/13] Add PEP 604 union compatibility contract tests --- tests/unit/utils/test_union_contract.py | 88 +++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/unit/utils/test_union_contract.py diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py new file mode 100644 index 0000000..7386652 --- /dev/null +++ b/tests/unit/utils/test_union_contract.py @@ -0,0 +1,88 @@ +"""Union-introspection compatibility contract. + +The helpers in :mod:`mode.utils.objects` are consumed by downstream projects +such as Faust. These tests make changes in union recognition explicit so a +Mode upgrade cannot silently change which annotations downstream compilers +receive. +""" + +import sys +import typing +from typing import Optional, Union, get_args, get_origin + +import pytest + +from mode.utils.objects import _remove_optional, is_optional, is_union, remove_optional + + +@pytest.mark.parametrize( + "annotation", + [ + Union[str, int], + Optional[str], + Union[str, list, dict, None], + ], +) +def test_typing_union_is_recognized(annotation): + assert is_union(annotation) + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") +@pytest.mark.parametrize( + "annotation", + [ + str | int, + str | None, + str | list | dict | None, + list[str] | dict[str, object] | None, + ], +) +def test_pep604_union_is_recognized(annotation): + assert is_union(annotation) + + +@pytest.mark.parametrize( + "annotation,expected", + [ + (Optional[str], True), + (Union[str, int, None], True), + (Union[str, int], False), + (str, False), + ], +) +def test_typing_optional_detection(annotation, expected): + assert is_optional(annotation) is expected + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") +@pytest.mark.parametrize( + "annotation,expected", + [ + (str | None, True), + (str | int | None, True), + (str | int, False), + (list[str] | dict[str, object] | None, True), + ], +) +def test_pep604_optional_detection(annotation, expected): + assert is_optional(annotation) is expected + + +def test_remove_optional_preserves_multi_type_union(): + result = remove_optional(Union[str, list, dict, None]) + assert is_union(result) + assert set(get_args(result)) == {str, list, dict} + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") +def test_remove_optional_normalizes_pep604_multi_type_union(): + result = remove_optional(str | list | dict | None) + assert get_origin(result) is typing.Union + assert set(get_args(result)) == {str, list, dict} + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") +def test_remove_optional_with_origin_retains_all_non_none_members(): + args, origin = _remove_optional(str | list | dict | None, find_origin=True) + assert origin is typing.Union + assert set(args) == {str, list, dict} From 83873d157fb4d9d8eac9e9aed63605580ff5ac4d Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 08:19:28 -0400 Subject: [PATCH 02/13] Keep union contract tests compatible with Python 3.9 --- tests/unit/utils/test_union_contract.py | 41 ++++++++++++------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 7386652..89ce07f 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -1,7 +1,7 @@ """Union-introspection compatibility contract. The helpers in :mod:`mode.utils.objects` are consumed by downstream projects -such as Faust. These tests make changes in union recognition explicit so a +such as Faust. These tests make changes in union recognition explicit so a Mode upgrade cannot silently change which annotations downstream compilers receive. """ @@ -15,6 +15,23 @@ from mode.utils.objects import _remove_optional, is_optional, is_union, remove_optional +PEP604_UNION_CASES = [] +PEP604_OPTIONAL_CASES = [] +if sys.version_info >= (3, 10): + PEP604_UNION_CASES = [ + str | int, + str | None, + str | list | dict | None, + list[str] | dict[str, object] | None, + ] + PEP604_OPTIONAL_CASES = [ + (str | None, True), + (str | int | None, True), + (str | int, False), + (list[str] | dict[str, object] | None, True), + ] + + @pytest.mark.parametrize( "annotation", [ @@ -27,16 +44,7 @@ def test_typing_union_is_recognized(annotation): assert is_union(annotation) -@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") -@pytest.mark.parametrize( - "annotation", - [ - str | int, - str | None, - str | list | dict | None, - list[str] | dict[str, object] | None, - ], -) +@pytest.mark.parametrize("annotation", PEP604_UNION_CASES) def test_pep604_union_is_recognized(annotation): assert is_union(annotation) @@ -54,16 +62,7 @@ def test_typing_optional_detection(annotation, expected): assert is_optional(annotation) is expected -@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") -@pytest.mark.parametrize( - "annotation,expected", - [ - (str | None, True), - (str | int | None, True), - (str | int, False), - (list[str] | dict[str, object] | None, True), - ], -) +@pytest.mark.parametrize("annotation,expected", PEP604_OPTIONAL_CASES) def test_pep604_optional_detection(annotation, expected): assert is_optional(annotation) is expected From 8c9db0b5e1a26f079691080cec19cc18875fc59e Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 08:27:47 -0400 Subject: [PATCH 03/13] Fix union contract test formatting --- tests/unit/utils/test_union_contract.py | 78 +------------------------ 1 file changed, 1 insertion(+), 77 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 89ce07f..4261fd2 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -8,80 +8,4 @@ import sys import typing -from typing import Optional, Union, get_args, get_origin - -import pytest - -from mode.utils.objects import _remove_optional, is_optional, is_union, remove_optional - - -PEP604_UNION_CASES = [] -PEP604_OPTIONAL_CASES = [] -if sys.version_info >= (3, 10): - PEP604_UNION_CASES = [ - str | int, - str | None, - str | list | dict | None, - list[str] | dict[str, object] | None, - ] - PEP604_OPTIONAL_CASES = [ - (str | None, True), - (str | int | None, True), - (str | int, False), - (list[str] | dict[str, object] | None, True), - ] - - -@pytest.mark.parametrize( - "annotation", - [ - Union[str, int], - Optional[str], - Union[str, list, dict, None], - ], -) -def test_typing_union_is_recognized(annotation): - assert is_union(annotation) - - -@pytest.mark.parametrize("annotation", PEP604_UNION_CASES) -def test_pep604_union_is_recognized(annotation): - assert is_union(annotation) - - -@pytest.mark.parametrize( - "annotation,expected", - [ - (Optional[str], True), - (Union[str, int, None], True), - (Union[str, int], False), - (str, False), - ], -) -def test_typing_optional_detection(annotation, expected): - assert is_optional(annotation) is expected - - -@pytest.mark.parametrize("annotation,expected", PEP604_OPTIONAL_CASES) -def test_pep604_optional_detection(annotation, expected): - assert is_optional(annotation) is expected - - -def test_remove_optional_preserves_multi_type_union(): - result = remove_optional(Union[str, list, dict, None]) - assert is_union(result) - assert set(get_args(result)) == {str, list, dict} - - -@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") -def test_remove_optional_normalizes_pep604_multi_type_union(): - result = remove_optional(str | list | dict | None) - assert get_origin(result) is typing.Union - assert set(get_args(result)) == {str, list, dict} - - -@pytest.mark.skipif(sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10") -def test_remove_optional_with_origin_retains_all_non_none_members(): - args, origin = _remove_optional(str | list | dict | None, find_origin=True) - assert origin is typing.Union - assert set(args) == {str, list, dict} +from typing import Optional, Union, get_args, get \ No newline at end of file From 5f542c0c709a71821747361d703b2ccac00a17eb Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 08:31:05 -0400 Subject: [PATCH 04/13] Restore complete union contract test file --- tests/unit/utils/test_union_contract.py | 89 ++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 4261fd2..cdfd1c2 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -8,4 +8,91 @@ import sys import typing -from typing import Optional, Union, get_args, get \ No newline at end of file +from typing import Optional, Union, get_args, get_origin + +import pytest + +from mode.utils.objects import ( + _remove_optional, + is_optional, + is_union, + remove_optional, +) + +PEP604_UNION_CASES = [] +PEP604_OPTIONAL_CASES = [] +if sys.version_info >= (3, 10): + PEP604_UNION_CASES = [ + str | int, + str | None, + str | list | dict | None, + list[str] | dict[str, object] | None, + ] + PEP604_OPTIONAL_CASES = [ + (str | None, True), + (str | int | None, True), + (str | int, False), + (list[str] | dict[str, object] | None, True), + ] + + +@pytest.mark.parametrize( + "annotation", + [ + Union[str, int], + Optional[str], + Union[str, list, dict, None], + ], +) +def test_typing_union_is_recognized(annotation): + assert is_union(annotation) + + +@pytest.mark.parametrize("annotation", PEP604_UNION_CASES) +def test_pep604_union_is_recognized(annotation): + assert is_union(annotation) + + +@pytest.mark.parametrize( + "annotation,expected", + [ + (Optional[str], True), + (Union[str, int, None], True), + (Union[str, int], False), + (str, False), + ], +) +def test_typing_optional_detection(annotation, expected): + assert is_optional(annotation) is expected + + +@pytest.mark.parametrize("annotation,expected", PEP604_OPTIONAL_CASES) +def test_pep604_optional_detection(annotation, expected): + assert is_optional(annotation) is expected + + +def test_remove_optional_preserves_multi_type_union(): + result = remove_optional(Union[str, list, dict, None]) + assert is_union(result) + assert set(get_args(result)) == {str, list, dict} + + +@pytest.mark.skipif( + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" +) +def test_remove_optional_normalizes_pep604_multi_type_union(): + result = remove_optional(str | list | dict | None) + assert get_origin(result) is typing.Union + assert set(get_args(result)) == {str, list, dict} + + +@pytest.mark.skipif( + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" +) +def test_remove_optional_with_origin_retains_all_non_none_members(): + args, origin = _remove_optional( + str | list | dict | None, + find_origin=True, + ) + assert origin is typing.Union + assert set(args) == {str, list, dict} From 001d499eafe9b1ec70463e4499e2a5f541445f46 Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 08:31:44 -0400 Subject: [PATCH 05/13] Fix union contract type annotations --- tests/unit/utils/test_union_contract.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index cdfd1c2..2f99183 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -8,7 +8,7 @@ import sys import typing -from typing import Optional, Union, get_args, get_origin +from typing import Any, Optional, Union, get_args, get_origin import pytest @@ -19,8 +19,8 @@ remove_optional, ) -PEP604_UNION_CASES = [] -PEP604_OPTIONAL_CASES = [] +PEP604_UNION_CASES: list[Any] = [] +PEP604_OPTIONAL_CASES: list[tuple[Any, bool]] = [] if sys.version_info >= (3, 10): PEP604_UNION_CASES = [ str | int, From 7faa103693c6ea82ff325c8dcdfd41b2d869b609 Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 08:40:30 -0400 Subject: [PATCH 06/13] Format union contract decorators --- tests/unit/utils/test_union_contract.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 2f99183..8af7e54 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -78,7 +78,8 @@ def test_remove_optional_preserves_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" + sys.version_info < (3, 10), + reason="PEP 604 requires Python 3.10", ) def test_remove_optional_normalizes_pep604_multi_type_union(): result = remove_optional(str | list | dict | None) @@ -87,7 +88,8 @@ def test_remove_optional_normalizes_pep604_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" + sys.version_info < (3, 10), + reason="PEP 604 requires Python 3.10", ) def test_remove_optional_with_origin_retains_all_non_none_members(): args, origin = _remove_optional( From 35545b1a690dc20b62c2c2b5f05490618256a03b Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 09:33:33 -0400 Subject: [PATCH 07/13] Apply Ruff formatting to union contract tests --- tests/unit/utils/test_union_contract.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 8af7e54..2f99183 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -78,8 +78,7 @@ def test_remove_optional_preserves_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), - reason="PEP 604 requires Python 3.10", + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" ) def test_remove_optional_normalizes_pep604_multi_type_union(): result = remove_optional(str | list | dict | None) @@ -88,8 +87,7 @@ def test_remove_optional_normalizes_pep604_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), - reason="PEP 604 requires Python 3.10", + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" ) def test_remove_optional_with_origin_retains_all_non_none_members(): args, origin = _remove_optional( From d9c63ead4474f12ee07540246600d836df4cff19 Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 10:31:42 -0400 Subject: [PATCH 08/13] Fix test file final newline From e803249a3f7fb4bfee764483483032f525600860 Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 11:33:36 -0400 Subject: [PATCH 09/13] Simplify union contract tests for pre-commit --- tests/unit/utils/test_union_contract.py | 60 +++++++++++++------------ 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 2f99183..306867b 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -1,14 +1,8 @@ -"""Union-introspection compatibility contract. - -The helpers in :mod:`mode.utils.objects` are consumed by downstream projects -such as Faust. These tests make changes in union recognition explicit so a -Mode upgrade cannot silently change which annotations downstream compilers -receive. -""" +"""Union-introspection compatibility contract.""" import sys import typing -from typing import Any, Optional, Union, get_args, get_origin +from typing import Optional, Union, get_args, get_origin import pytest @@ -19,22 +13,6 @@ remove_optional, ) -PEP604_UNION_CASES: list[Any] = [] -PEP604_OPTIONAL_CASES: list[tuple[Any, bool]] = [] -if sys.version_info >= (3, 10): - PEP604_UNION_CASES = [ - str | int, - str | None, - str | list | dict | None, - list[str] | dict[str, object] | None, - ] - PEP604_OPTIONAL_CASES = [ - (str | None, True), - (str | int | None, True), - (str | int, False), - (list[str] | dict[str, object] | None, True), - ] - @pytest.mark.parametrize( "annotation", @@ -48,7 +26,19 @@ def test_typing_union_is_recognized(annotation): assert is_union(annotation) -@pytest.mark.parametrize("annotation", PEP604_UNION_CASES) +@pytest.mark.skipif( + sys.version_info < (3, 10), + reason="PEP 604 requires Python 3.10", +) +@pytest.mark.parametrize( + "annotation", + [ + str | int, + str | None, + str | list | dict | None, + list[str] | dict[str, object] | None, + ], +) def test_pep604_union_is_recognized(annotation): assert is_union(annotation) @@ -66,7 +56,19 @@ def test_typing_optional_detection(annotation, expected): assert is_optional(annotation) is expected -@pytest.mark.parametrize("annotation,expected", PEP604_OPTIONAL_CASES) +@pytest.mark.skipif( + sys.version_info < (3, 10), + reason="PEP 604 requires Python 3.10", +) +@pytest.mark.parametrize( + "annotation,expected", + [ + (str | None, True), + (str | int | None, True), + (str | int, False), + (list[str] | dict[str, object] | None, True), + ], +) def test_pep604_optional_detection(annotation, expected): assert is_optional(annotation) is expected @@ -78,7 +80,8 @@ def test_remove_optional_preserves_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" + sys.version_info < (3, 10), + reason="PEP 604 requires Python 3.10", ) def test_remove_optional_normalizes_pep604_multi_type_union(): result = remove_optional(str | list | dict | None) @@ -87,7 +90,8 @@ def test_remove_optional_normalizes_pep604_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" + sys.version_info < (3, 10), + reason="PEP 604 requires Python 3.10", ) def test_remove_optional_with_origin_retains_all_non_none_members(): args, origin = _remove_optional( From 04476297b0073c19da94dedaf879c8b0189cb09a Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 12:32:58 -0400 Subject: [PATCH 10/13] Avoid evaluating PEP 604 unions on Python 3.9 --- tests/unit/utils/test_union_contract.py | 43 +++++++++++-------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 306867b..f5f0a14 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -30,17 +30,14 @@ def test_typing_union_is_recognized(annotation): sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10", ) -@pytest.mark.parametrize( - "annotation", - [ - str | int, - str | None, - str | list | dict | None, - list[str] | dict[str, object] | None, - ], -) -def test_pep604_union_is_recognized(annotation): - assert is_union(annotation) +def test_pep604_union_is_recognized(): + annotations = [ + eval("str | int"), + eval("str | None"), + eval("str | list | dict | None"), + eval("list[str] | dict[str, object] | None"), + ] + assert all(is_union(annotation) for annotation in annotations) @pytest.mark.parametrize( @@ -60,17 +57,15 @@ def test_typing_optional_detection(annotation, expected): sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10", ) -@pytest.mark.parametrize( - "annotation,expected", - [ - (str | None, True), - (str | int | None, True), - (str | int, False), - (list[str] | dict[str, object] | None, True), - ], -) -def test_pep604_optional_detection(annotation, expected): - assert is_optional(annotation) is expected +def test_pep604_optional_detection(): + cases = [ + (eval("str | None"), True), + (eval("str | int | None"), True), + (eval("str | int"), False), + (eval("list[str] | dict[str, object] | None"), True), + ] + for annotation, expected in cases: + assert is_optional(annotation) is expected def test_remove_optional_preserves_multi_type_union(): @@ -84,7 +79,7 @@ def test_remove_optional_preserves_multi_type_union(): reason="PEP 604 requires Python 3.10", ) def test_remove_optional_normalizes_pep604_multi_type_union(): - result = remove_optional(str | list | dict | None) + result = remove_optional(eval("str | list | dict | None")) assert get_origin(result) is typing.Union assert set(get_args(result)) == {str, list, dict} @@ -95,7 +90,7 @@ def test_remove_optional_normalizes_pep604_multi_type_union(): ) def test_remove_optional_with_origin_retains_all_non_none_members(): args, origin = _remove_optional( - str | list | dict | None, + eval("str | list | dict | None"), find_origin=True, ) assert origin is typing.Union From dbe369e061a2ca2b63d8d826c39f6d3997625561 Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 13:33:39 -0400 Subject: [PATCH 11/13] Fix Ruff security lint in union tests --- tests/unit/utils/test_union_contract.py | 26 ++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index f5f0a14..d1c107a 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -2,7 +2,7 @@ import sys import typing -from typing import Optional, Union, get_args, get_origin +from typing import Any, Optional, Union, get_args, get_origin import pytest @@ -14,6 +14,10 @@ ) +def _eval_pep604(expression: str) -> Any: + return eval(expression) # noqa: S307 + + @pytest.mark.parametrize( "annotation", [ @@ -32,10 +36,10 @@ def test_typing_union_is_recognized(annotation): ) def test_pep604_union_is_recognized(): annotations = [ - eval("str | int"), - eval("str | None"), - eval("str | list | dict | None"), - eval("list[str] | dict[str, object] | None"), + _eval_pep604("str | int"), + _eval_pep604("str | None"), + _eval_pep604("str | list | dict | None"), + _eval_pep604("list[str] | dict[str, object] | None"), ] assert all(is_union(annotation) for annotation in annotations) @@ -59,10 +63,10 @@ def test_typing_optional_detection(annotation, expected): ) def test_pep604_optional_detection(): cases = [ - (eval("str | None"), True), - (eval("str | int | None"), True), - (eval("str | int"), False), - (eval("list[str] | dict[str, object] | None"), True), + (_eval_pep604("str | None"), True), + (_eval_pep604("str | int | None"), True), + (_eval_pep604("str | int"), False), + (_eval_pep604("list[str] | dict[str, object] | None"), True), ] for annotation, expected in cases: assert is_optional(annotation) is expected @@ -79,7 +83,7 @@ def test_remove_optional_preserves_multi_type_union(): reason="PEP 604 requires Python 3.10", ) def test_remove_optional_normalizes_pep604_multi_type_union(): - result = remove_optional(eval("str | list | dict | None")) + result = remove_optional(_eval_pep604("str | list | dict | None")) assert get_origin(result) is typing.Union assert set(get_args(result)) == {str, list, dict} @@ -90,7 +94,7 @@ def test_remove_optional_normalizes_pep604_multi_type_union(): ) def test_remove_optional_with_origin_retains_all_non_none_members(): args, origin = _remove_optional( - eval("str | list | dict | None"), + _eval_pep604("str | list | dict | None"), find_origin=True, ) assert origin is typing.Union From abe1ea10bd494b2538b645b5a4fd6cc06a6ce265 Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 14:30:51 -0400 Subject: [PATCH 12/13] Remove redundant Ruff suppression in union tests --- tests/unit/utils/test_union_contract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index d1c107a..61d2d7e 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -15,7 +15,7 @@ def _eval_pep604(expression: str) -> Any: - return eval(expression) # noqa: S307 + return eval(expression) @pytest.mark.parametrize( From 1d4a5b8d78c59a066a05e704135ef760570a2800 Mon Sep 17 00:00:00 2001 From: William Barnhart Date: Wed, 22 Jul 2026 19:00:45 +0000 Subject: [PATCH 13/13] Apply Ruff formatter to union contract tests Collapse hand-formatted multi-line decorators and the parametrize list onto single lines so `ruff format --check` passes. No behavior change; all 12 contract tests still pass. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01Lt14zRDjGJv9YU9QZC3PqY --- tests/unit/utils/test_union_contract.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/tests/unit/utils/test_union_contract.py b/tests/unit/utils/test_union_contract.py index 61d2d7e..a660881 100644 --- a/tests/unit/utils/test_union_contract.py +++ b/tests/unit/utils/test_union_contract.py @@ -20,19 +20,14 @@ def _eval_pep604(expression: str) -> Any: @pytest.mark.parametrize( "annotation", - [ - Union[str, int], - Optional[str], - Union[str, list, dict, None], - ], + [Union[str, int], Optional[str], Union[str, list, dict, None]], ) def test_typing_union_is_recognized(annotation): assert is_union(annotation) @pytest.mark.skipif( - sys.version_info < (3, 10), - reason="PEP 604 requires Python 3.10", + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" ) def test_pep604_union_is_recognized(): annotations = [ @@ -58,8 +53,7 @@ def test_typing_optional_detection(annotation, expected): @pytest.mark.skipif( - sys.version_info < (3, 10), - reason="PEP 604 requires Python 3.10", + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" ) def test_pep604_optional_detection(): cases = [ @@ -79,8 +73,7 @@ def test_remove_optional_preserves_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), - reason="PEP 604 requires Python 3.10", + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" ) def test_remove_optional_normalizes_pep604_multi_type_union(): result = remove_optional(_eval_pep604("str | list | dict | None")) @@ -89,13 +82,11 @@ def test_remove_optional_normalizes_pep604_multi_type_union(): @pytest.mark.skipif( - sys.version_info < (3, 10), - reason="PEP 604 requires Python 3.10", + sys.version_info < (3, 10), reason="PEP 604 requires Python 3.10" ) def test_remove_optional_with_origin_retains_all_non_none_members(): args, origin = _remove_optional( - _eval_pep604("str | list | dict | None"), - find_origin=True, + _eval_pep604("str | list | dict | None"), find_origin=True ) assert origin is typing.Union assert set(args) == {str, list, dict}