Skip to content

Commit c8d53af

Browse files
ranjithguggillaRanjith Guggilla
authored andcommitted
Fix TypeError when passing more than two filters to a collection
Collection methods combine their filters with and_(*filters), but and_ only accepted two arguments, so a call such as client.linode.instances( Instance.label == "web", Instance.region == "us-east", Instance.tags.contains("prod"), ) raised "TypeError: and_() takes 2 positional arguments but 3 were given". and_ now accepts two or more filters and combines all of them into one "+and" list.
1 parent c204913 commit c8d53af

3 files changed

Lines changed: 101 additions & 8 deletions

File tree

‎linode_api4/linode_client.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,7 @@ def _get_and_filter(
317317
parsed_filters = None
318318
if filters:
319319
if len(filters) > 1:
320-
parsed_filters = and_(
321-
*filters
322-
).dct # pylint: disable=no-value-for-parameter
320+
parsed_filters = and_(*filters).dct
323321
else:
324322
parsed_filters = filters[0].dct
325323

‎linode_api4/objects/filtering.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,25 @@ def or_(a, b):
6666
return a.__or__(b)
6767

6868

69-
def and_(a, b):
69+
def and_(a, b, *filters):
7070
"""
71-
Combines two :any:`Filters<Filter>` with an "and" operation, matching
72-
any results that match both of the given filters.
71+
Combines two or more :any:`Filters<Filter>` with an "and" operation,
72+
matching any results that match all of the given filters.
7373
7474
:param a: The first filter to consider.
7575
:type a: Filter
7676
:param b: The second filter to consider.
7777
:type b: Filter
78+
:param filters: Any additional filters to consider.
79+
:type filters: Filter
7880
79-
:returns: A filter that matches both a and b
81+
:returns: A filter that matches all of the given filters
8082
:rtype: Filter
8183
"""
82-
return a.__and__(b)
84+
result = a.__and__(b)
85+
for f in filters:
86+
result = result.__and__(f)
87+
return result
8388

8489

8590
def order_by(field, desc=False):
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import json
2+
from test.unit.base import ClientBaseCase
3+
4+
from linode_api4 import Instance, and_, or_
5+
6+
7+
class FilteringTest(ClientBaseCase):
8+
"""
9+
Tests combining filters and passing them to collection methods
10+
"""
11+
12+
def test_and_two_filters(self):
13+
"""
14+
Tests that and_ combines two filters
15+
"""
16+
f = and_(Instance.label == "web", Instance.region == "us-east")
17+
18+
self.assertEqual(
19+
f.dct, {"+and": [{"label": "web"}, {"region": "us-east"}]}
20+
)
21+
22+
def test_and_many_filters(self):
23+
"""
24+
Tests that and_ accepts more than two filters
25+
"""
26+
f = and_(
27+
Instance.label == "web",
28+
Instance.region == "us-east",
29+
Instance.tags.contains("prod"),
30+
or_(Instance.group == "a", Instance.group == "b"),
31+
)
32+
33+
self.assertEqual(
34+
f.dct,
35+
{
36+
"+and": [
37+
{"label": "web"},
38+
{"region": "us-east"},
39+
{"tags": {"+contains": "prod"}},
40+
{"+or": [{"group": "a"}, {"group": "b"}]},
41+
]
42+
},
43+
)
44+
45+
def test_and_requires_two_filters(self):
46+
"""
47+
Tests that and_ still requires at least two filters
48+
"""
49+
with self.assertRaises(TypeError):
50+
and_(
51+
Instance.label == "web"
52+
) # pylint: disable=no-value-for-parameter
53+
54+
def test_collection_with_many_filters(self):
55+
"""
56+
Tests that a collection method accepts more than two filters and
57+
sends all of them in the X-Filter header
58+
"""
59+
cases = (
60+
(
61+
(Instance.label == "web",),
62+
{"label": "web"},
63+
),
64+
(
65+
(Instance.label == "web", Instance.region == "us-east"),
66+
{"+and": [{"label": "web"}, {"region": "us-east"}]},
67+
),
68+
(
69+
(
70+
Instance.label == "web",
71+
Instance.region == "us-east",
72+
Instance.tags.contains("prod"),
73+
),
74+
{
75+
"+and": [
76+
{"label": "web"},
77+
{"region": "us-east"},
78+
{"tags": {"+contains": "prod"}},
79+
]
80+
},
81+
),
82+
)
83+
84+
for filters, expected in cases:
85+
with self.mock_get("linode/instances") as m:
86+
self.client.linode.instances(*filters)
87+
88+
self.assertEqual(
89+
json.loads(m.call_headers["X-Filter"]), expected
90+
)

0 commit comments

Comments
 (0)