From a0477b8aa421e8be445a724b2046bd5cb28f1acd Mon Sep 17 00:00:00 2001 From: q0w <43147888+q0w@users.noreply.github.com> Date: Wed, 10 Nov 2021 13:07:06 +0300 Subject: [PATCH 1/6] Replace BinaryIO with IO[bytes] --- aiodocker/images.py | 10 +++++----- aiodocker/utils.py | 16 +++------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/aiodocker/images.py b/aiodocker/images.py index c8e309fe..a15877e9 100644 --- a/aiodocker/images.py +++ b/aiodocker/images.py @@ -2,9 +2,9 @@ import json import warnings from typing import ( + IO, Any, AsyncIterator, - BinaryIO, Dict, List, Mapping, @@ -230,7 +230,7 @@ async def delete( ) @staticmethod - async def _stream(fileobj: BinaryIO) -> AsyncIterator[bytes]: + async def _stream(fileobj: IO[bytes]) -> AsyncIterator[bytes]: chunk = fileobj.read(io.DEFAULT_BUFFER_SIZE) while chunk: yield chunk @@ -241,7 +241,7 @@ async def build( self, *, remote: str = None, - fileobj: BinaryIO = None, + fileobj: IO[bytes] = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, @@ -261,7 +261,7 @@ def build( self, *, remote: str = None, - fileobj: BinaryIO = None, + fileobj: IO[bytes] = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, @@ -280,7 +280,7 @@ def build( # noqa: F811 self, *, remote: str = None, - fileobj: BinaryIO = None, + fileobj: IO[bytes] = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, diff --git a/aiodocker/utils.py b/aiodocker/utils.py index 45c188a5..a63c03c0 100644 --- a/aiodocker/utils.py +++ b/aiodocker/utils.py @@ -6,17 +6,7 @@ import tarfile import tempfile from io import BytesIO -from typing import ( - IO, - Any, - BinaryIO, - Iterable, - Mapping, - MutableMapping, - Optional, - Tuple, - Union, -) +from typing import IO, Any, Iterable, Mapping, MutableMapping, Optional, Tuple, Union async def parse_result(response, response_type=None, *, encoding="utf-8"): @@ -227,12 +217,12 @@ def clean_filters(filters: Mapping = None) -> str: return json.dumps(filters) -def mktar_from_dockerfile(fileobject: BinaryIO) -> IO: +def mktar_from_dockerfile(fileobject: Union[BytesIO, IO[bytes]]) -> IO[bytes]: """ Create a zipped tar archive from a Dockerfile **Remember to close the file object** Args: - fileobj: a Dockerfile + fileobject: a Dockerfile Returns: a NamedTemporaryFile() object """ From 06fa358416d94117ea5980de28cc2dfa5d4927c4 Mon Sep 17 00:00:00 2001 From: q0w <43147888+q0w@users.noreply.github.com> Date: Sun, 14 Nov 2021 21:35:28 +0300 Subject: [PATCH 2/6] Use SupportsRead as fileobj type --- aiodocker/images.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/aiodocker/images.py b/aiodocker/images.py index a15877e9..c2c14d02 100644 --- a/aiodocker/images.py +++ b/aiodocker/images.py @@ -2,7 +2,6 @@ import json import warnings from typing import ( - IO, Any, AsyncIterator, Dict, @@ -14,6 +13,7 @@ overload, ) +from _typeshed import SupportsRead from typing_extensions import Literal from .jsonstream import json_stream_list, json_stream_stream @@ -230,7 +230,7 @@ async def delete( ) @staticmethod - async def _stream(fileobj: IO[bytes]) -> AsyncIterator[bytes]: + async def _stream(fileobj: SupportsRead[bytes]) -> AsyncIterator[bytes]: chunk = fileobj.read(io.DEFAULT_BUFFER_SIZE) while chunk: yield chunk @@ -241,7 +241,7 @@ async def build( self, *, remote: str = None, - fileobj: IO[bytes] = None, + fileobj: SupportsRead[bytes] = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, @@ -261,7 +261,7 @@ def build( self, *, remote: str = None, - fileobj: IO[bytes] = None, + fileobj: SupportsRead[bytes] = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, @@ -280,7 +280,7 @@ def build( # noqa: F811 self, *, remote: str = None, - fileobj: IO[bytes] = None, + fileobj: SupportsRead[bytes] = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, From aeb2c14fe7434f7d5dfc261d77f94afe00a35ee8 Mon Sep 17 00:00:00 2001 From: q0w <43147888+q0w@users.noreply.github.com> Date: Sun, 14 Nov 2021 21:41:27 +0300 Subject: [PATCH 3/6] Fix imports --- aiodocker/images.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/aiodocker/images.py b/aiodocker/images.py index c2c14d02..591c9401 100644 --- a/aiodocker/images.py +++ b/aiodocker/images.py @@ -2,6 +2,7 @@ import json import warnings from typing import ( + TYPE_CHECKING, Any, AsyncIterator, Dict, @@ -13,7 +14,10 @@ overload, ) -from _typeshed import SupportsRead + +if TYPE_CHECKING: + from _typeshed import SupportsRead + from typing_extensions import Literal from .jsonstream import json_stream_list, json_stream_stream @@ -230,7 +234,7 @@ async def delete( ) @staticmethod - async def _stream(fileobj: SupportsRead[bytes]) -> AsyncIterator[bytes]: + async def _stream(fileobj: "SupportsRead[bytes]") -> AsyncIterator[bytes]: chunk = fileobj.read(io.DEFAULT_BUFFER_SIZE) while chunk: yield chunk @@ -241,7 +245,7 @@ async def build( self, *, remote: str = None, - fileobj: SupportsRead[bytes] = None, + fileobj: "SupportsRead[bytes]" = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, @@ -261,7 +265,7 @@ def build( self, *, remote: str = None, - fileobj: SupportsRead[bytes] = None, + fileobj: "SupportsRead[bytes]" = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, @@ -280,7 +284,7 @@ def build( # noqa: F811 self, *, remote: str = None, - fileobj: SupportsRead[bytes] = None, + fileobj: "SupportsRead[bytes]" = None, path_dockerfile: str = None, tag: str = None, quiet: bool = False, From adcdcd492f2157a844101e78ff326d840cfaa365 Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Tue, 21 May 2024 01:22:14 +0900 Subject: [PATCH 4/6] Make the argument naming consistent with other modules --- aiodocker/utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/aiodocker/utils.py b/aiodocker/utils.py index e8e09fc0..3ec9601e 100644 --- a/aiodocker/utils.py +++ b/aiodocker/utils.py @@ -210,12 +210,12 @@ def clean_filters(filters: Optional[Mapping] = None) -> str: return json.dumps(filters) -def mktar_from_dockerfile(fileobject: Union[BytesIO, IO[bytes]]) -> IO[bytes]: +def mktar_from_dockerfile(fileobj: Union[BytesIO, IO[bytes]]) -> IO[bytes]: """ Create a zipped tar archive from a Dockerfile **Remember to close the file object** Args: - fileobject: a Dockerfile + fileobj: a Dockerfile Returns: a NamedTemporaryFile() object """ @@ -223,14 +223,14 @@ def mktar_from_dockerfile(fileobject: Union[BytesIO, IO[bytes]]) -> IO[bytes]: f = tempfile.NamedTemporaryFile() t = tarfile.open(mode="w:gz", fileobj=f) - if isinstance(fileobject, BytesIO): + if isinstance(fileobj, BytesIO): dfinfo = tarfile.TarInfo("Dockerfile") - dfinfo.size = len(fileobject.getvalue()) - fileobject.seek(0) + dfinfo.size = len(fileobj.getvalue()) + fileobj.seek(0) else: - dfinfo = t.gettarinfo(fileobj=fileobject, arcname="Dockerfile") + dfinfo = t.gettarinfo(fileobj=fileobj, arcname="Dockerfile") - t.addfile(dfinfo, fileobject) + t.addfile(dfinfo, fileobj) t.close() f.seek(0) return f From 6df6bd73beceaac178e4c1055b85b28ad136c54e Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Tue, 21 May 2024 01:26:49 +0900 Subject: [PATCH 5/6] Vendor _typeshed.SupportsRead as our own types module --- aiodocker/images.py | 8 +------- aiodocker/types.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 aiodocker/types.py diff --git a/aiodocker/images.py b/aiodocker/images.py index 7023b904..957a1bc8 100644 --- a/aiodocker/images.py +++ b/aiodocker/images.py @@ -1,10 +1,7 @@ -from __future__ import annotations - import io import json import warnings from typing import ( - TYPE_CHECKING, Any, AsyncIterator, Dict, @@ -16,13 +13,10 @@ overload, ) - -if TYPE_CHECKING: - from _typeshed import SupportsRead - from typing_extensions import Literal from .jsonstream import json_stream_list, json_stream_stream +from .types import SupportsRead from .utils import clean_map, compose_auth_header diff --git a/aiodocker/types.py b/aiodocker/types.py new file mode 100644 index 00000000..aa97a169 --- /dev/null +++ b/aiodocker/types.py @@ -0,0 +1,11 @@ +from typing import ( + Protocol, + TypeVar, +) + + +_T_co = TypeVar("_T_co", covariant=True) + + +class SupportsRead(Protocol[_T_co]): + def read(self, length: int = ..., /) -> _T_co: ... From 9cd7c74c52650280ac248105247e485bf49203a7 Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Tue, 21 May 2024 01:28:03 +0900 Subject: [PATCH 6/6] Fix formatting --- aiodocker/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/aiodocker/utils.py b/aiodocker/utils.py index 3ec9601e..68b74403 100644 --- a/aiodocker/utils.py +++ b/aiodocker/utils.py @@ -4,7 +4,16 @@ import tarfile import tempfile from io import BytesIO -from typing import IO, Any, Iterable, Mapping, MutableMapping, Optional, Tuple, Union +from typing import ( + IO, + Any, + Iterable, + Mapping, + MutableMapping, + Optional, + Tuple, + Union, +) async def parse_result(response, response_type=None, *, encoding="utf-8"):