Skip to content

Commit

Permalink
fix(patterns): Object pattern should match on positional arguments …
Browse files Browse the repository at this point in the history
…first
  • Loading branch information
kszucs committed Nov 6, 2023
1 parent 6552639 commit 96c796f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ibis/common/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ def match(self, value, context):
if self.type.match(value, context) is NoMatch:
return NoMatch

patterns = {**self.kwargs, **dict(zip(value.__match_args__, self.args))}
patterns = {**dict(zip(value.__match_args__, self.args)), **self.kwargs}

fields = {}
changed = False
Expand Down
24 changes: 24 additions & 0 deletions ibis/common/tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,30 @@ def __coerce__(cls, other):
assert p_call == Object(MyCoercibleType, 1, 2)


def test_object_pattern_matching_order():
class Foo:
__match_args__ = ("a", "b", "c")

def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c

def __eq__(self, other):
return (
type(self) == type(other)
and self.a == other.a
and self.b == other.b
and self.c == other.c
)

a = var("a")
p = Object(Foo, a, c=EqualTo(a))

assert match(p, Foo(1, 2, 3)) is NoMatch
assert match(p, Foo(1, 2, 1)) == Foo(1, 2, 1)


def test_callable_with():
def func(a, b):
return str(a) + b
Expand Down

0 comments on commit 96c796f

Please # to comment.