From 84f03bcefec7f07840a4c5a433b36e09b60d1aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20K=C5=99=C3=AD=C5=BE?= <15214494+antoninkriz@users.noreply.github.com> Date: Fri, 1 Sep 2023 10:08:04 +0200 Subject: [PATCH 1/2] Allow the usage of frozenset in the isin(...) and notin(...) functions --- pypika/terms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pypika/terms.py b/pypika/terms.py index c522550a..36c9a06f 100644 --- a/pypika/terms.py +++ b/pypika/terms.py @@ -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": From d009e7d54d257c9eea8620368ba27c2379765538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20K=C5=99=C3=AD=C5=BE?= <15214494+antoninkriz@users.noreply.github.com> Date: Mon, 25 Sep 2023 15:31:07 +0200 Subject: [PATCH 2/2] Added more tests for the isin function --- pypika/tests/test_criterions.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pypika/tests/test_criterions.py b/pypika/tests/test_criterions.py index 2ecaa13a..111732d5 100644 --- a/pypika/tests/test_criterions.py +++ b/pypika/tests/test_criterions.py @@ -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"])