Skip to content

Commit

Permalink
Automatically add many type hints, with ruff
Browse files Browse the repository at this point in the history
If this library is to ship type hints, it is important that those type hints are valid.
A type checker (e.g. mypy or pyright) can check that those type hints are valid.

This PR is one small step towards that goal.

I ran:

```
ruff --extend-select=ANN201,ANN202,ANN204,ANN205,ANN206 --fi
x --unsafe-fixes docker
```

and I did not make any manual changes.

I chose not to also type other directories (such as `tests`) as that is a decision I'd first like to hear from core maintainers on.

Signed-off-by: Adam Dangoor <[email protected]>
  • Loading branch information
adamtheturtle committed Jan 26, 2024
1 parent bd164f9 commit 4716ee3
Show file tree
Hide file tree
Showing 41 changed files with 185 additions and 185 deletions.
2 changes: 1 addition & 1 deletion docker/api/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def prune_builds(self, filters=None, keep_storage=None, all=None):
params['all'] = all
return self._result(self._post(url, params=params), True)

def _set_auth_headers(self, headers):
def _set_auth_headers(self, headers) -> None:
log.debug('Looking for auth config')

# If we don't have any auth data so far, try reloading the config
Expand Down
12 changes: 6 additions & 6 deletions docker/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self, base_url=None, version=None,
timeout=DEFAULT_TIMEOUT_SECONDS, tls=False,
user_agent=DEFAULT_USER_AGENT, num_pools=None,
credstore_env=None, use_ssh_client=False,
max_pool_size=DEFAULT_MAX_POOL_SIZE):
max_pool_size=DEFAULT_MAX_POOL_SIZE) -> None:
super().__init__()

if tls and not base_url:
Expand Down Expand Up @@ -243,7 +243,7 @@ def _put(self, url, **kwargs):
def _delete(self, url, **kwargs):
return self.delete(url, **self._set_request_timeout(kwargs))

def _url(self, pathfmt, *args, **kwargs):
def _url(self, pathfmt, *args, **kwargs) -> str:
for arg in args:
if not isinstance(arg, str):
raise ValueError(
Expand All @@ -259,7 +259,7 @@ def _url(self, pathfmt, *args, **kwargs):
else:
return f'{self.base_url}{formatted_path}'

def _raise_for_status(self, response):
def _raise_for_status(self, response) -> None:
"""Raises stored :class:`APIError`, if one occurred."""
try:
response.raise_for_status()
Expand Down Expand Up @@ -436,7 +436,7 @@ def _read_from_socket(self, response, stream, tty=True, demux=False):
finally:
response.close()

def _disable_socket_timeout(self, socket):
def _disable_socket_timeout(self, socket) -> None:
""" Depending on the combination of python version and whether we're
connecting over http or https, we might need to access _sock, which
may or may not exist; or we may need to just settimeout on socket
Expand Down Expand Up @@ -488,7 +488,7 @@ def _get_result_tty(self, stream, res, is_tty):
list(self._multiplexed_buffer_helper(res))
)

def _unmount(self, *args):
def _unmount(self, *args) -> None:
for proto in args:
self.adapters.pop(proto)

Expand All @@ -505,7 +505,7 @@ def get_adapter(self, url):
def api_version(self):
return self._version

def reload_config(self, dockercfg_path=None):
def reload_config(self, dockercfg_path=None) -> None:
"""
Force a reload of the auth configuration
Expand Down
2 changes: 1 addition & 1 deletion docker/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def inspect_config(self, id):

@utils.minimum_version('1.30')
@utils.check_resource('id')
def remove_config(self, id):
def remove_config(self, id) -> bool:
"""
Remove a config
Expand Down
18 changes: 9 additions & 9 deletions docker/api/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def inspect_container(self, container):
)

@utils.check_resource('container')
def kill(self, container, signal=None):
def kill(self, container, signal=None) -> None:
"""
Kill a container or send a signal to a container.
Expand Down Expand Up @@ -900,7 +900,7 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
return output

@utils.check_resource('container')
def pause(self, container):
def pause(self, container) -> None:
"""
Pauses all processes within a container.
Expand Down Expand Up @@ -1013,7 +1013,7 @@ def prune_containers(self, filters=None):
return self._result(self._post(url, params=params), True)

@utils.check_resource('container')
def remove_container(self, container, v=False, link=False, force=False):
def remove_container(self, container, v=False, link=False, force=False) -> None:
"""
Remove a container. Similar to the ``docker rm`` command.
Expand All @@ -1036,7 +1036,7 @@ def remove_container(self, container, v=False, link=False, force=False):
self._raise_for_status(res)

@utils.check_resource('container')
def rename(self, container, name):
def rename(self, container, name) -> None:
"""
Rename a container. Similar to the ``docker rename`` command.
Expand All @@ -1054,7 +1054,7 @@ def rename(self, container, name):
self._raise_for_status(res)

@utils.check_resource('container')
def resize(self, container, height, width):
def resize(self, container, height, width) -> None:
"""
Resize the tty session.
Expand All @@ -1073,7 +1073,7 @@ def resize(self, container, height, width):
self._raise_for_status(res)

@utils.check_resource('container')
def restart(self, container, timeout=10):
def restart(self, container, timeout=10) -> None:
"""
Restart a container. Similar to the ``docker restart`` command.
Expand All @@ -1097,7 +1097,7 @@ def restart(self, container, timeout=10):
self._raise_for_status(res)

@utils.check_resource('container')
def start(self, container, *args, **kwargs):
def start(self, container, *args, **kwargs) -> None:
"""
Start a container. Similar to the ``docker start`` command, but
doesn't support attach options.
Expand Down Expand Up @@ -1183,7 +1183,7 @@ def stats(self, container, decode=None, stream=True, one_shot=None):
return self._result(self._get(url, params=params), json=True)

@utils.check_resource('container')
def stop(self, container, timeout=None):
def stop(self, container, timeout=None) -> None:
"""
Stops a container. Similar to the ``docker stop`` command.
Expand Down Expand Up @@ -1233,7 +1233,7 @@ def top(self, container, ps_args=None):
return self._result(self._get(u, params=params), True)

@utils.check_resource('container')
def unpause(self, container):
def unpause(self, container) -> None:
"""
Unpause all processes within a container.
Expand Down
2 changes: 1 addition & 1 deletion docker/api/exec_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def exec_inspect(self, exec_id):
res = self._get(self._url("/exec/{0}/json", exec_id))
return self._result(res, True)

def exec_resize(self, exec_id, height=None, width=None):
def exec_resize(self, exec_id, height=None, width=None) -> None:
"""
Resize the tty session used by the specified exec command.
Expand Down
6 changes: 3 additions & 3 deletions docker/api/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def prune_networks(self, filters=None):
return self._result(self._post(url, params=params), True)

@check_resource('net_id')
def remove_network(self, net_id):
def remove_network(self, net_id) -> None:
"""
Remove a network. Similar to the ``docker network rm`` command.
Expand Down Expand Up @@ -217,7 +217,7 @@ def connect_container_to_network(self, container, net_id,
ipv4_address=None, ipv6_address=None,
aliases=None, links=None,
link_local_ips=None, driver_opt=None,
mac_address=None):
mac_address=None) -> None:
"""
Connect a container to a network.
Expand Down Expand Up @@ -255,7 +255,7 @@ def connect_container_to_network(self, container, net_id,

@check_resource('container')
def disconnect_container_from_network(self, container, net_id,
force=False):
force=False) -> None:
"""
Disconnect a container from a network.
Expand Down
10 changes: 5 additions & 5 deletions docker/api/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class PluginApiMixin:
@utils.minimum_version('1.25')
@utils.check_resource('name')
def configure_plugin(self, name, options):
def configure_plugin(self, name, options) -> bool:
"""
Configure a plugin.
Expand All @@ -25,7 +25,7 @@ def configure_plugin(self, name, options):
return True

@utils.minimum_version('1.25')
def create_plugin(self, name, plugin_data_dir, gzip=False):
def create_plugin(self, name, plugin_data_dir, gzip=False) -> bool:
"""
Create a new plugin.
Expand All @@ -51,7 +51,7 @@ def create_plugin(self, name, plugin_data_dir, gzip=False):
return True

@utils.minimum_version('1.25')
def disable_plugin(self, name, force=False):
def disable_plugin(self, name, force=False) -> bool:
"""
Disable an installed plugin.
Expand All @@ -69,7 +69,7 @@ def disable_plugin(self, name, force=False):
return True

@utils.minimum_version('1.25')
def enable_plugin(self, name, timeout=0):
def enable_plugin(self, name, timeout=0) -> bool:
"""
Enable an installed plugin.
Expand Down Expand Up @@ -206,7 +206,7 @@ def push_plugin(self, name):

@utils.minimum_version('1.25')
@utils.check_resource('name')
def remove_plugin(self, name, force=False):
def remove_plugin(self, name, force=False) -> bool:
"""
Remove an installed plugin.
Expand Down
2 changes: 1 addition & 1 deletion docker/api/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def inspect_secret(self, id):

@utils.minimum_version('1.25')
@utils.check_resource('id')
def remove_secret(self, id):
def remove_secret(self, id) -> bool:
"""
Remove a secret
Expand Down
6 changes: 3 additions & 3 deletions docker/api/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


def _check_api_features(version, task_template, update_config, endpoint_spec,
rollback_config):
rollback_config) -> None:

def raise_version_error(param, min_version):
def raise_version_error(param, min_version) -> None:
raise errors.InvalidVersion(
f'{param} is not supported in API version < {min_version}'
)
Expand Down Expand Up @@ -239,7 +239,7 @@ def inspect_task(self, task):

@utils.minimum_version('1.24')
@utils.check_resource('service')
def remove_service(self, service):
def remove_service(self, service) -> bool:
"""
Stop and remove a service.
Expand Down
12 changes: 6 additions & 6 deletions docker/api/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def inspect_node(self, node_id):

@utils.minimum_version('1.24')
def join_swarm(self, remote_addrs, join_token, listen_addr='0.0.0.0:2377',
advertise_addr=None, data_path_addr=None):
advertise_addr=None, data_path_addr=None) -> bool:
"""
Make this Engine join a swarm that has already been created.
Expand Down Expand Up @@ -266,7 +266,7 @@ def join_swarm(self, remote_addrs, join_token, listen_addr='0.0.0.0:2377',
return True

@utils.minimum_version('1.24')
def leave_swarm(self, force=False):
def leave_swarm(self, force=False) -> bool:
"""
Leave a swarm.
Expand Down Expand Up @@ -319,7 +319,7 @@ def nodes(self, filters=None):

@utils.check_resource('node_id')
@utils.minimum_version('1.24')
def remove_node(self, node_id, force=False):
def remove_node(self, node_id, force=False) -> bool:
"""
Remove a node from the swarm.
Expand All @@ -345,7 +345,7 @@ def remove_node(self, node_id, force=False):
return True

@utils.minimum_version('1.24')
def unlock_swarm(self, key):
def unlock_swarm(self, key) -> bool:
"""
Unlock a locked swarm.
Expand Down Expand Up @@ -381,7 +381,7 @@ def unlock_swarm(self, key):
return True

@utils.minimum_version('1.24')
def update_node(self, node_id, version, node_spec=None):
def update_node(self, node_id, version, node_spec=None) -> bool:
"""
Update the node's configuration
Expand Down Expand Up @@ -420,7 +420,7 @@ def update_node(self, node_id, version, node_spec=None):
def update_swarm(self, version, swarm_spec=None,
rotate_worker_token=False,
rotate_manager_token=False,
rotate_manager_unlock_key=False):
rotate_manager_unlock_key=False) -> bool:
"""
Update the Swarm's configuration
Expand Down
2 changes: 1 addition & 1 deletion docker/api/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def prune_volumes(self, filters=None):
url = self._url('/volumes/prune')
return self._result(self._post(url, params=params), True)

def remove_volume(self, name, force=False):
def remove_volume(self, name, force=False) -> None:
"""
Remove a volume. Similar to the ``docker volume rm`` command.
Expand Down
6 changes: 3 additions & 3 deletions docker/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_credential_store(authconfig, registry):


class AuthConfig(dict):
def __init__(self, dct, credstore_env=None):
def __init__(self, dct, credstore_env=None) -> None:
if 'auths' not in dct:
dct['auths'] = {}
self.update(dct)
Expand Down Expand Up @@ -202,7 +202,7 @@ def cred_helpers(self):
return self.get('credHelpers', {})

@property
def is_empty(self):
def is_empty(self) -> bool:
return (
not self.auths and not self.creds_store and not self.cred_helpers
)
Expand Down Expand Up @@ -303,7 +303,7 @@ def get_all_credentials(self):

return auth_data

def add_auth(self, reg, data):
def add_auth(self, reg, data) -> None:
self['auths'][reg] = data


Expand Down
2 changes: 1 addition & 1 deletion docker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DockerClient:
max_pool_size (int): The maximum number of connections
to save in the pool.
"""
def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs) -> None:
self.api = APIClient(*args, **kwargs)

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions docker/context/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def get_current_context(cls):
return cls.get_context()

@classmethod
def set_current_context(cls, name="default"):
def set_current_context(cls, name="default") -> None:
ctx = cls.get_context(name)
if not ctx:
raise errors.ContextNotFound(name)
Expand All @@ -141,7 +141,7 @@ def set_current_context(cls, name="default"):
f'Failed to set current context: {err}')

@classmethod
def remove_context(cls, name):
def remove_context(cls, name) -> None:
"""Remove a context. Similar to the ``docker context rm`` command.
Args:
Expand Down
Loading

0 comments on commit 4716ee3

Please sign in to comment.