Skip to content

Commit

Permalink
fix(patterns): support passing mappings to Getitem builder
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs authored and cpcloud committed Sep 27, 2023
1 parent 58b2d0e commit 25864cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 4 additions & 2 deletions ibis/common/patterns.py
Expand Up @@ -421,7 +421,8 @@ class Getattr(Slotted, Builder):
__slots__ = ("instance", "name")

def __init__(self, instance, name):
super().__init__(instance=builder(instance), name=name)
instance = instance if isinstance(instance, Builder) else Just(instance)
super().__init__(instance=instance, name=name)

def build(self, context):
instance = self.instance.build(context)
Expand All @@ -432,7 +433,8 @@ class Getitem(Slotted, Builder):
__slots__ = ("instance", "name")

def __init__(self, instance, name):
super().__init__(instance=builder(instance), name=name)
instance = instance if isinstance(instance, Builder) else Just(instance)
super().__init__(instance=instance, name=name)

def build(self, context):
instance = self.instance.build(context)
Expand Down
16 changes: 15 additions & 1 deletion ibis/common/tests/test_patterns.py
Expand Up @@ -1265,16 +1265,30 @@ def fn(a, b, c=1):


def test_getattr():
class MyType:
def __init__(self, a, b):
self.a = a
self.b = b

def __hash__(self):
return hash((type(self), self.a, self.b))

v = Variable("v")
b = Getattr(v, "b")
assert b.build({v: Foo(1, 2)}) == 2
assert b.build({v: MyType(1, 2)}) == 2

b = Getattr(MyType(1, 2), "a")
assert b.build({}) == 1


def test_getitem():
v = Variable("v")
b = Getitem(v, 1)
assert b.build({v: [1, 2, 3]}) == 2

b = Getitem(FrozenDict(a=1, b=2), "a")
assert b.build({}) == 1


def test_builder():
def fn(x, ctx):
Expand Down

0 comments on commit 25864cf

Please sign in to comment.