Skip to content

Commit

Permalink
Fix use of union notation for Python < 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Oct 26, 2024
1 parent 56f4522 commit 613effa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 13 additions & 3 deletions iguala/helpers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
from __future__ import annotations
from collections.abc import MutableSet
from itertools import chain
from types import UnionType

try:
from types import UnionType

def is_union(o):
return isinstance(o, UnionType)

except ImportError:
from typing import Union, get_origin

def is_union(o):
return get_origin(o) is Union


class match(object):
def __init__(self, cls, *classes):
from .matchers import ObjectMatcher

if isinstance(cls, UnionType):
if is_union(cls):
all_classes = cls.__args__
else:
all_classes = [cls, *classes]
Expand Down
3 changes: 3 additions & 0 deletions tests/test_object_matchers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
from iguala import as_matcher, match
import sys

from .data_for_tests import ATest, BTest, InnerTest, obj_test

Expand Down Expand Up @@ -145,6 +147,7 @@ def test_match_multiple_types():
assert pattern.match(BTest(0, 0, 'foo', InnerTest('bar', 5), [], 5)).is_match is True


@pytest.mark.skipif(sys.version_info < (3, 10), reason="Union notation is not available for Python < 3.10")
def test_match_union_type():
pattern = match(ATest) % {}
assert pattern.match(BTest(0, 0, 'foo', InnerTest('bar', 5), [], 5)).is_match is False
Expand Down

0 comments on commit 613effa

Please sign in to comment.