From 1c88d05229e7508b856fbd625e5ec8c3d4436958 Mon Sep 17 00:00:00 2001 From: njabulo ndlovu Date: Fri, 4 Apr 2025 19:50:55 +0200 Subject: [PATCH 1/2] fix: update is_union function to support python 3.10+ union syntax --- mode/utils/objects.py | 1 + tests/unit/utils/test_objects.py | 1 + 2 files changed, 2 insertions(+) diff --git a/mode/utils/objects.py b/mode/utils/objects.py index 70c73c3..8524841 100644 --- a/mode/utils/objects.py +++ b/mode/utils/objects.py @@ -472,6 +472,7 @@ def is_union(typ: Type) -> bool: name = typ.__class__.__name__ return any( [ + name == "UnionType", # 3.10 name == "_UnionGenericAlias", # 3.9 name == "_GenericAlias" and typ.__origin__ is typing.Union, # 3.7 name == "_Union", # 3.6 diff --git a/tests/unit/utils/test_objects.py b/tests/unit/utils/test_objects.py index 0e384af..a62f979 100644 --- a/tests/unit/utils/test_objects.py +++ b/tests/unit/utils/test_objects.py @@ -384,6 +384,7 @@ def test_label_pass(): (int, False), (Union[int, bytes], True), (Optional[str], True), + (int | None, True), ], ) def test_is_union(input, expected): From e074b95f1a3c4f5cff953a2906f6dcd377599f41 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 17 Jul 2026 01:25:42 +0000 Subject: [PATCH 2/2] test: only use X | Y union syntax on Python 3.10+ The int | None parametrize case from PR #65 raises TypeError at collection time on Python 3.8/3.9, which is what failed CI on that PR. Guard the case behind a runtime version check. Assisted-by: Claude Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01HgnKFtXZbCjXNoVNWa5JLd --- tests/unit/utils/test_objects.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/unit/utils/test_objects.py b/tests/unit/utils/test_objects.py index a62f979..119cfa7 100644 --- a/tests/unit/utils/test_objects.py +++ b/tests/unit/utils/test_objects.py @@ -1,6 +1,7 @@ import abc import collections.abc import pickle +import sys import typing from typing import ( AbstractSet, @@ -377,15 +378,17 @@ def test_label_pass(): assert label(s) is s -@pytest.mark.parametrize( - "input,expected", - [ - (str, False), - (int, False), - (Union[int, bytes], True), - (Optional[str], True), - (int | None, True), - ], -) +IS_UNION_CASES = [ + (str, False), + (int, False), + (Union[int, bytes], True), + (Optional[str], True), +] +if sys.version_info >= (3, 10): + # X | Y syntax is only usable at runtime on Python 3.10+ + IS_UNION_CASES.append((int | None, True)) + + +@pytest.mark.parametrize("input,expected", IS_UNION_CASES) def test_is_union(input, expected): assert is_union(input) == expected