Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Allow the usage of frozenset in the isin(...) and notin(...) functions #744

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading