Skip to content

Commit

Permalink
Fix: Allow the usage of frozenset in the isin(...) and `notin(...…
Browse files Browse the repository at this point in the history
…)` functions (kayak#744)

* Allow the usage of frozenset in the isin(...) and notin(...) functions

* Added more tests for the isin function
  • Loading branch information
antoninkriz authored Sep 26, 2023
1 parent 5087884 commit 351981a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pypika/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ def as_of(self, expr: str) -> "BasicCriterion":
def all_(self) -> "All":
return All(self)

def isin(self, arg: Union[list, tuple, set, "Term"]) -> "ContainsCriterion":
if isinstance(arg, (list, tuple, set)):
def isin(self, arg: Union[list, tuple, set, frozenset, "Term"]) -> "ContainsCriterion":
if isinstance(arg, (list, tuple, set, frozenset)):
return ContainsCriterion(self, Tuple(*[self.wrap_constant(value) for value in arg]))
return ContainsCriterion(self, arg)

def notin(self, arg: Union[list, tuple, set, "Term"]) -> "ContainsCriterion":
def notin(self, arg: Union[list, tuple, set, frozenset, "Term"]) -> "ContainsCriterion":
return self.isin(arg).negate()

def bin_regex(self, pattern: str) -> "BasicCriterion":
Expand Down
6 changes: 6 additions & 0 deletions pypika/tests/test_criterions.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ def test__function_isin(self):
self.assertEqual('COALESCE("foo",0) IN (0,1)', str(c1))
self.assertEqual('COALESCE("isin"."foo",0) IN (0,1)', str(c2))

for t in (list, tuple, set, frozenset):
c_type = fn.Coalesce(Field("foo"), 0).isin(t([0, 1]))
self.assertEqual('COALESCE("foo",0) IN (0,1)', str(c_type))

self.assertRaises(AttributeError, lambda: str(fn.Coalesce(Field("foo"), 0).isin('SHOULD_FAIL')))

def test__in_unicode(self):
c1 = Field("foo").isin([u"a", u"b"])
c2 = Field("foo", table=self.t).isin([u"a", u"b"])
Expand Down

0 comments on commit 351981a

Please sign in to comment.