Skip to content

Commit 20e8408

Browse files
committed
Add deprecation warnings supporting old form of filters
1 parent c7cb511 commit 20e8408

File tree

7 files changed

+176
-37
lines changed

7 files changed

+176
-37
lines changed

python_on_whales/components/container/cli_wrapper.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import shlex
66
import textwrap
7+
import warnings
78
from datetime import datetime, timedelta
89
from pathlib import Path
910
from typing import (
@@ -1173,18 +1174,27 @@ def logs(
11731174
def list(
11741175
self,
11751176
all: bool = False,
1176-
filters: Iterable[ContainerListFilter] = (),
1177+
filters: Union[Iterable[ContainerListFilter], Mapping[str, Any]] = (),
11771178
) -> List[Container]:
11781179
"""List the containers on the host.
11791180
11801181
Alias: `docker.ps(...)`
11811182
11821183
Parameters:
11831184
all: If `True`, also returns containers that are not running.
1185+
filters: Filters to apply when listing containers.
11841186
11851187
# Returns
11861188
A `List[python_on_whales.Container]`
11871189
"""
1190+
if isinstance(filters, Mapping):
1191+
filters = filters.items()
1192+
warnings.warn(
1193+
"Passing filters as a mapping is deprecated, replace with an "
1194+
"iterable of tuples instead, as so:\n"
1195+
f"filters={list(filters)}",
1196+
DeprecationWarning,
1197+
)
11881198
full_cmd = self.docker_cmd
11891199
full_cmd += ["container", "list", "-q", "--no-trunc"]
11901200
full_cmd.add_flag("--all", all)
@@ -1222,20 +1232,20 @@ def pause(
12221232
@overload
12231233
def prune(
12241234
self,
1225-
filters: Iterable[ContainerListFilter] = (),
1235+
filters: Union[Iterable[ContainerListFilter], Mapping[str, Any]] = (),
12261236
stream_logs: Literal[True] = ...,
12271237
) -> Iterable[Tuple[str, bytes]]: ...
12281238

12291239
@overload
12301240
def prune(
12311241
self,
1232-
filters: Iterable[ContainerListFilter] = (),
1242+
filters: Union[Iterable[ContainerListFilter], Mapping[str, Any]] = (),
12331243
stream_logs: Literal[False] = ...,
12341244
) -> None: ...
12351245

12361246
def prune(
12371247
self,
1238-
filters: Iterable[ContainerListFilter] = (),
1248+
filters: Union[Iterable[ContainerListFilter], Mapping[str, Any]] = (),
12391249
stream_logs: bool = False,
12401250
):
12411251
"""Remove containers that are not running.
@@ -1247,11 +1257,13 @@ def prune(
12471257
the function returns `None`, but when it returns, then the prune operation has already been
12481258
done.
12491259
"""
1250-
if isinstance(filter, list):
1251-
raise TypeError(
1252-
"since python-on-whales 0.38.0, the filter argument is expected to be "
1253-
"a dict, not a list, please replace your function call by "
1254-
"docker.container.prune(filters={...})"
1260+
if isinstance(filters, Mapping):
1261+
filters = filters.items()
1262+
warnings.warn(
1263+
"Passing filters as a mapping is deprecated, replace with an "
1264+
"iterable of tuples instead, as so:\n"
1265+
f"filters={list(filters)}",
1266+
DeprecationWarning,
12551267
)
12561268
full_cmd = self.docker_cmd + ["container", "prune", "--force"]
12571269
full_cmd.add_args_iterable("--filter", (f"{f[0]}={f[1]}" for f in filters))

python_on_whales/components/image/cli_wrapper.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def _load_from_generator(self, full_cmd: List[str], input: Iterator[bytes]):
433433
def list(
434434
self,
435435
repository_or_tag: Optional[str] = None,
436-
filters: Iterable[ImageListFilter] = [],
436+
filters: Union[Iterable[ImageListFilter], Mapping[str, Any]] = (),
437437
all: bool = False,
438438
) -> List[Image]:
439439
"""Returns the list of Docker images present on the machine.
@@ -445,21 +445,14 @@ def list(
445445
# Returns
446446
A `List[python_on_whales.Image]` object.
447447
"""
448-
# previously the signature was
449-
# def list(self,filters: Dict[str, str] = {}) -> List[Image]:
450-
# so to avoid breakages when people used positional arguments, we can check the types and send a warning
451-
if isinstance(repository_or_tag, dict):
452-
# after a while, we can convert that to an error. No hurry though.
448+
if isinstance(filters, Mapping):
449+
filters = filters.items()
453450
warnings.warn(
454-
f"You are calling docker.image.list({repository_or_tag}) with the filter as the first argument."
455-
f"Since Python-on-whales v0.51.0, the first argument has be changed to `repository_or_tag`."
456-
f"To fix this warning, please add the filters keyword argument, "
457-
f"like so: docker.image.list(filters={repository_or_tag}) ",
451+
"Passing filters as a mapping is deprecated, replace with an "
452+
"iterable of tuples instead, as so:\n"
453+
f"filters={list(filters)}",
458454
DeprecationWarning,
459455
)
460-
filters = repository_or_tag
461-
repository_or_tag = None
462-
463456
full_cmd = self.docker_cmd + [
464457
"image",
465458
"list",
@@ -478,7 +471,11 @@ def list(
478471

479472
return [Image(self.client_config, x, is_immutable_id=True) for x in ids]
480473

481-
def prune(self, all: bool = False, filters: List[ImageListFilter] = []) -> str:
474+
def prune(
475+
self,
476+
all: bool = False,
477+
filters: Union[Iterable[ImageListFilter], Mapping[str, Any]] = (),
478+
) -> str:
482479
"""Remove unused images
483480
484481
Parameters:
@@ -488,6 +485,14 @@ def prune(self, all: bool = False, filters: List[ImageListFilter] = []) -> str:
488485
Returns:
489486
The output of the CLI (the layers removed).
490487
"""
488+
if isinstance(filters, Mapping):
489+
filters = filters.items()
490+
warnings.warn(
491+
"Passing filters as a mapping is deprecated, replace with an "
492+
"iterable of tuples instead, as so:\n"
493+
f"filters={list(filters)}",
494+
DeprecationWarning,
495+
)
491496
full_cmd = self.docker_cmd + ["image", "prune", "--force"]
492497
full_cmd.add_flag("--all", all)
493498
full_cmd.add_args_iterable("--filter", (f"{f[0]}={f[1]}" for f in filters))

python_on_whales/components/network/cli_wrapper.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
from __future__ import annotations
22

33
import json
4+
import warnings
45
from datetime import datetime
5-
from typing import Any, Dict, List, Literal, Optional, Tuple, TypeAlias, Union, overload
6+
from typing import (
7+
Any,
8+
Dict,
9+
Iterable,
10+
List,
11+
Literal,
12+
Mapping,
13+
Optional,
14+
Tuple,
15+
TypeAlias,
16+
Union,
17+
overload,
18+
)
619

720
import python_on_whales.components.container.cli_wrapper
821
from python_on_whales.client_config import (
@@ -265,27 +278,47 @@ def inspect(self, x: Union[str, List[str]]) -> Union[Network, List[Network]]:
265278
else:
266279
return [Network(self.client_config, reference) for reference in x]
267280

268-
def list(self, filters: List[NetworkListFilter] = []) -> List[Network]:
281+
def list(
282+
self, filters: Union[Iterable[NetworkListFilter], Mapping[str, Any]] = ()
283+
) -> List[Network]:
269284
"""List all the networks available.
270285
271286
Parameters:
272-
filters: Filters as strings or list of strings.
287+
filters: Filters to apply when listing networks.
273288
274289
# Returns
275290
List of `python_on_whales.Network`.
276291
"""
292+
if isinstance(filters, Mapping):
293+
filters = filters.items()
294+
warnings.warn(
295+
"Passing filters as a mapping is deprecated, replace with an "
296+
"iterable of tuples instead, as so:\n"
297+
f"filters={list(filters)}",
298+
DeprecationWarning,
299+
)
277300
full_cmd = self.docker_cmd + ["network", "ls", "--no-trunc", "--quiet"]
278301
full_cmd.add_args_iterable("--filter", (f"{f[0]}={f[1]}" for f in filters))
279302

280303
ids = run(full_cmd).splitlines()
281304
return [Network(self.client_config, id_, is_immutable_id=True) for id_ in ids]
282305

283-
def prune(self, filters: List[NetworkListFilter] = []) -> None:
306+
def prune(
307+
self, filters: Union[Iterable[NetworkListFilter], Mapping[str, Any]] = ()
308+
) -> None:
284309
"""Remove Docker networks which are not used by any containers.
285310
286311
Parameters:
287312
filters: Filters to apply when finding networks to prune.
288313
"""
314+
if isinstance(filters, Mapping):
315+
filters = filters.items()
316+
warnings.warn(
317+
"Passing filters as a mapping is deprecated, replace with an "
318+
"iterable of tuples instead, as so:\n"
319+
f"filters={list(filters)}",
320+
DeprecationWarning,
321+
)
289322
full_cmd = self.docker_cmd + ["network", "prune", "--force"]
290323
full_cmd.add_args_iterable("--filter", (f"{f[0]}={f[1]}" for f in filters))
291324
run(full_cmd)

python_on_whales/components/pod/cli_wrapper.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import json
4+
import warnings
45
from datetime import datetime, timedelta
56
from typing import (
67
Any,
@@ -33,7 +34,6 @@
3334
from python_on_whales.utils import (
3435
ValidPath,
3536
ValidPortMapping,
36-
format_mapping_for_cli,
3737
format_port_arg,
3838
format_signal_arg,
3939
format_time_arg,
@@ -515,7 +515,9 @@ def kill(
515515
full_cmd.extend([str(p) for p in pods])
516516
run(full_cmd)
517517

518-
def list(self, *, filters: List[PodListFilter] = []) -> List[Pod]:
518+
def list(
519+
self, *, filters: Union[Iterable[PodListFilter], Mapping[str, Any]] = ()
520+
) -> List[Pod]:
519521
"""List the pods on the host.
520522
521523
Parameters:
@@ -524,6 +526,14 @@ def list(self, *, filters: List[PodListFilter] = []) -> List[Pod]:
524526
# Returns
525527
A `List[python_on_whales.Pod]`
526528
"""
529+
if isinstance(filters, Mapping):
530+
filters = filters.items()
531+
warnings.warn(
532+
"Passing filters as a mapping is deprecated, replace with an "
533+
"iterable of tuples instead, as so:\n"
534+
f"filters={list(filters)}",
535+
DeprecationWarning,
536+
)
527537
full_cmd = self.docker_cmd + ["pod", "ps", "-q", "--no-trunc"]
528538
full_cmd.add_args_iterable("--filter", (f"{f[0]}={f[1]}" for f in filters))
529539

python_on_whales/components/secret/cli_wrapper.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import json
2-
from typing import Any, Dict, List, Literal, Optional, Tuple, TypeAlias, Union
2+
import warnings
3+
from typing import (
4+
Any,
5+
Dict,
6+
Iterable,
7+
List,
8+
Literal,
9+
Mapping,
10+
Optional,
11+
Tuple,
12+
TypeAlias,
13+
Union,
14+
)
315

416
from python_on_whales.client_config import (
517
ClientConfig,
@@ -87,8 +99,18 @@ def inspect(self, x: Union[str, List[str]]) -> Union[Secret, List[Secret]]:
8799
else:
88100
return Secret(self.client_config, x)
89101

90-
def list(self, filters: List[SecretListFilter] = []) -> List[Secret]:
102+
def list(
103+
self, filters: Union[Iterable[SecretListFilter], Mapping[str, Any]] = ()
104+
) -> List[Secret]:
91105
"""Returns all secrets as a `List[python_on_whales.Secret]`."""
106+
if isinstance(filters, Mapping):
107+
filters = filters.items()
108+
warnings.warn(
109+
"Passing filters as a mapping is deprecated, replace with an "
110+
"iterable of tuples instead, as so:\n"
111+
f"filters={list(filters)}",
112+
DeprecationWarning,
113+
)
92114
full_cmd = self.docker_cmd + ["secret", "list", "--quiet"]
93115
full_cmd.add_args_iterable("--filter", (f"{f[0]}={f[1]}" for f in filters))
94116
ids = run(full_cmd).splitlines()

python_on_whales/components/service/cli_wrapper.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
from __future__ import annotations
22

33
import json
4+
import warnings
45
from datetime import datetime, timedelta
5-
from typing import Any, Dict, List, Literal, Optional, Tuple, TypeAlias, Union, overload
6+
from typing import (
7+
Any,
8+
Dict,
9+
Iterable,
10+
List,
11+
Literal,
12+
Mapping,
13+
Optional,
14+
Tuple,
15+
TypeAlias,
16+
Union,
17+
overload,
18+
)
619

720
import python_on_whales.components.task.cli_wrapper
821
from python_on_whales.client_config import (
@@ -359,7 +372,9 @@ def logs(
359372
else:
360373
return "".join(x[1].decode() for x in iterator)
361374

362-
def list(self, filters: List[ServiceListFilter] = []) -> List[Service]:
375+
def list(
376+
self, filters: Union[Iterable[ServiceListFilter], Mapping[str, Any]] = ()
377+
) -> List[Service]:
363378
"""Returns the list of services
364379
365380
Parameters:
@@ -369,6 +384,14 @@ def list(self, filters: List[ServiceListFilter] = []) -> List[Service]:
369384
# Returns
370385
A `List[python_on_whales.Services]`
371386
"""
387+
if isinstance(filters, Mapping):
388+
filters = filters.items()
389+
warnings.warn(
390+
"Passing filters as a mapping is deprecated, replace with an "
391+
"iterable of tuples instead, as so:\n"
392+
f"filters={list(filters)}",
393+
DeprecationWarning,
394+
)
372395
full_cmd = self.docker_cmd + ["service", "list", "--quiet"]
373396
full_cmd.add_args_iterable("--filter", (f"{f[0]}={f[1]}" for f in filters))
374397

0 commit comments

Comments
 (0)