Enable Ruff C rules and autofix

Signed-off-by: Aarni Koskela <akx@iki.fi>
This commit is contained in:
Aarni Koskela 2023-05-11 15:55:09 +03:00
parent 64fe747f3e
commit 475bf69dbf
14 changed files with 147 additions and 140 deletions

View File

@ -476,7 +476,7 @@ class APIClient(
return self._multiplexed_response_stream_helper(res) return self._multiplexed_response_stream_helper(res)
else: else:
return sep.join( return sep.join(
[x for x in self._multiplexed_buffer_helper(res)] list(self._multiplexed_buffer_helper(res))
) )
def _unmount(self, *args): def _unmount(self, *args):

View File

@ -217,7 +217,7 @@ def parse_host(addr, is_win32=False, tls=False):
parsed_url = urlparse(addr) parsed_url = urlparse(addr)
proto = parsed_url.scheme proto = parsed_url.scheme
if not proto or any([x not in f"{string.ascii_letters}+" for x in proto]): if not proto or any(x not in f"{string.ascii_letters}+" for x in proto):
# https://bugs.python.org/issue754016 # https://bugs.python.org/issue754016
parsed_url = urlparse(f"//{addr}", 'tcp') parsed_url = urlparse(f"//{addr}", 'tcp')
proto = 'tcp' proto = 'tcp'

View File

@ -4,5 +4,16 @@ requires = ["setuptools>=45", "setuptools_scm[toml]>=6.2"]
[tool.setuptools_scm] [tool.setuptools_scm]
write_to = 'docker/_version.py' write_to = 'docker/_version.py'
[tool.ruff]
target-version = "py37"
extend-select = [
"C",
"F",
"W",
]
ignore = [
"C901", # too complex (there's a whole bunch of these)
]
[tool.ruff.per-file-ignores] [tool.ruff.per-file-ignores]
"**/__init__.py" = ["F401"] "**/__init__.py" = ["F401"]

View File

@ -30,7 +30,7 @@ extras_require = {
} }
with open('./test-requirements.txt') as test_reqs_txt: with open('./test-requirements.txt') as test_reqs_txt:
test_requirements = [line for line in test_reqs_txt] test_requirements = list(test_reqs_txt)
long_description = '' long_description = ''

View File

@ -142,7 +142,7 @@ class BuildTest(BaseAPIIntegrationTest):
logs = logs.decode('utf-8') logs = logs.decode('utf-8')
assert sorted(list(filter(None, logs.split('\n')))) == sorted([ assert sorted(filter(None, logs.split('\n'))) == sorted([
'/test/#file.txt', '/test/#file.txt',
'/test/ignored/subdir/excepted-with-spaces', '/test/ignored/subdir/excepted-with-spaces',
'/test/ignored/subdir/excepted-file', '/test/ignored/subdir/excepted-file',
@ -312,7 +312,7 @@ class BuildTest(BaseAPIIntegrationTest):
) )
self.tmp_imgs.append('dockerpytest_nonebuild') self.tmp_imgs.append('dockerpytest_nonebuild')
logs = [chunk for chunk in stream] logs = list(stream)
assert 'errorDetail' in logs[-1] assert 'errorDetail' in logs[-1]
assert logs[-1]['errorDetail']['code'] == 1 assert logs[-1]['errorDetail']['code'] == 1
@ -392,7 +392,7 @@ class BuildTest(BaseAPIIntegrationTest):
expected = '{0}{2}\n{1}'.format( expected = '{0}{2}\n{1}'.format(
control_chars[0], control_chars[1], snippet control_chars[0], control_chars[1], snippet
) )
assert any([line == expected for line in lines]) assert any(line == expected for line in lines)
def test_build_gzip_encoding(self): def test_build_gzip_encoding(self):
base_dir = tempfile.mkdtemp() base_dir = tempfile.mkdtemp()

View File

@ -16,7 +16,7 @@ class HealthcheckTest(BaseAPIIntegrationTest):
@helpers.requires_api_version('1.24') @helpers.requires_api_version('1.24')
def test_healthcheck_shell_command(self): def test_healthcheck_shell_command(self):
container = self.client.create_container( container = self.client.create_container(
TEST_IMG, 'top', healthcheck=dict(test='echo "hello world"')) TEST_IMG, 'top', healthcheck={'test': 'echo "hello world"'})
self.tmp_containers.append(container) self.tmp_containers.append(container)
res = self.client.inspect_container(container) res = self.client.inspect_container(container)
@ -27,12 +27,12 @@ class HealthcheckTest(BaseAPIIntegrationTest):
@helpers.requires_api_version('1.24') @helpers.requires_api_version('1.24')
def test_healthcheck_passes(self): def test_healthcheck_passes(self):
container = self.client.create_container( container = self.client.create_container(
TEST_IMG, 'top', healthcheck=dict( TEST_IMG, 'top', healthcheck={
test="true", 'test': "true",
interval=1 * SECOND, 'interval': 1 * SECOND,
timeout=1 * SECOND, 'timeout': 1 * SECOND,
retries=1, 'retries': 1,
)) })
self.tmp_containers.append(container) self.tmp_containers.append(container)
self.client.start(container) self.client.start(container)
wait_on_health_status(self.client, container, "healthy") wait_on_health_status(self.client, container, "healthy")
@ -40,12 +40,12 @@ class HealthcheckTest(BaseAPIIntegrationTest):
@helpers.requires_api_version('1.24') @helpers.requires_api_version('1.24')
def test_healthcheck_fails(self): def test_healthcheck_fails(self):
container = self.client.create_container( container = self.client.create_container(
TEST_IMG, 'top', healthcheck=dict( TEST_IMG, 'top', healthcheck={
test="false", 'test': "false",
interval=1 * SECOND, 'interval': 1 * SECOND,
timeout=1 * SECOND, 'timeout': 1 * SECOND,
retries=1, 'retries': 1,
)) })
self.tmp_containers.append(container) self.tmp_containers.append(container)
self.client.start(container) self.client.start(container)
wait_on_health_status(self.client, container, "unhealthy") wait_on_health_status(self.client, container, "unhealthy")
@ -53,14 +53,14 @@ class HealthcheckTest(BaseAPIIntegrationTest):
@helpers.requires_api_version('1.29') @helpers.requires_api_version('1.29')
def test_healthcheck_start_period(self): def test_healthcheck_start_period(self):
container = self.client.create_container( container = self.client.create_container(
TEST_IMG, 'top', healthcheck=dict( TEST_IMG, 'top', healthcheck={
test="echo 'x' >> /counter.txt && " 'test': "echo 'x' >> /counter.txt && "
"test `cat /counter.txt | wc -l` -ge 3", "test `cat /counter.txt | wc -l` -ge 3",
interval=1 * SECOND, 'interval': 1 * SECOND,
timeout=1 * SECOND, 'timeout': 1 * SECOND,
retries=1, 'retries': 1,
start_period=3 * SECOND 'start_period': 3 * SECOND
) }
) )
self.tmp_containers.append(container) self.tmp_containers.append(container)

View File

@ -263,10 +263,8 @@ class ImportImageTest(BaseAPIIntegrationTest):
data = self.client.get_image(test_img) data = self.client.get_image(test_img)
assert data assert data
output = self.client.load_image(data) output = self.client.load_image(data)
assert any([ assert any(line for line in output
line for line in output if f'Loaded image: {test_img}' in line.get('stream', ''))
if f'Loaded image: {test_img}' in line.get('stream', '')
])
@contextlib.contextmanager @contextlib.contextmanager
def temporary_http_file_server(self, stream): def temporary_http_file_server(self, stream):

View File

@ -118,7 +118,7 @@ class PluginTest(BaseAPIIntegrationTest):
pass pass
prv = self.client.plugin_privileges(SSHFS) prv = self.client.plugin_privileges(SSHFS)
logs = [d for d in self.client.pull_plugin(SSHFS, prv)] logs = list(self.client.pull_plugin(SSHFS, prv))
assert filter(lambda x: x['status'] == 'Download complete', logs) assert filter(lambda x: x['status'] == 'Download complete', logs)
assert self.client.inspect_plugin(SSHFS) assert self.client.inspect_plugin(SSHFS)
assert self.client.enable_plugin(SSHFS) assert self.client.enable_plugin(SSHFS)
@ -128,7 +128,7 @@ class PluginTest(BaseAPIIntegrationTest):
pl_data = self.ensure_plugin_installed(SSHFS) pl_data = self.ensure_plugin_installed(SSHFS)
assert pl_data['Enabled'] is False assert pl_data['Enabled'] is False
prv = self.client.plugin_privileges(SSHFS) prv = self.client.plugin_privileges(SSHFS)
logs = [d for d in self.client.upgrade_plugin(SSHFS, SSHFS, prv)] logs = list(self.client.upgrade_plugin(SSHFS, SSHFS, prv))
assert filter(lambda x: x['status'] == 'Download complete', logs) assert filter(lambda x: x['status'] == 'Download complete', logs)
assert self.client.inspect_plugin(SSHFS) assert self.client.inspect_plugin(SSHFS)
assert self.client.enable_plugin(SSHFS) assert self.client.enable_plugin(SSHFS)

View File

@ -29,7 +29,7 @@ class ContextLifecycleTest(BaseAPIIntegrationTest):
"test", tls_cfg=docker_tls) "test", tls_cfg=docker_tls)
# check for a context 'test' in the context store # check for a context 'test' in the context store
assert any([ctx.Name == "test" for ctx in ContextAPI.contexts()]) assert any(ctx.Name == "test" for ctx in ContextAPI.contexts())
# retrieve a context object for 'test' # retrieve a context object for 'test'
assert ContextAPI.get_context("test") assert ContextAPI.get_context("test")
# remove context # remove context

View File

@ -109,7 +109,7 @@ class ContainerCollectionTest(BaseIntegrationTest):
out = client.containers.run( out = client.containers.run(
"alpine", "echo hello", "alpine", "echo hello",
log_config=dict(type='none') log_config={"type": 'none'}
) )
assert out is None assert out is None
@ -118,7 +118,7 @@ class ContainerCollectionTest(BaseIntegrationTest):
out = client.containers.run( out = client.containers.run(
"alpine", "echo hello", "alpine", "echo hello",
log_config=dict(type='json-file') log_config={"type": 'json-file'}
) )
assert out == b'hello\n' assert out == b'hello\n'
@ -150,7 +150,7 @@ class ContainerCollectionTest(BaseIntegrationTest):
out = client.containers.run( out = client.containers.run(
'alpine', 'sh -c "echo hello && echo world"', stream=True 'alpine', 'sh -c "echo hello && echo world"', stream=True
) )
logs = [line for line in out] logs = list(out)
assert logs[0] == b'hello\n' assert logs[0] == b'hello\n'
assert logs[1] == b'world\n' assert logs[1] == b'world\n'
@ -165,7 +165,7 @@ class ContainerCollectionTest(BaseIntegrationTest):
threading.Timer(1, out.close).start() threading.Timer(1, out.close).start()
logs = [line for line in out] logs = list(out)
assert len(logs) == 2 assert len(logs) == 2
assert logs[0] == b'hello\n' assert logs[0] == b'hello\n'

View File

@ -88,9 +88,7 @@ class ImageCollectionTest(BaseIntegrationTest):
client = docker.from_env(version=TEST_API_VERSION) client = docker.from_env(version=TEST_API_VERSION)
images = client.images.pull('hello-world', all_tags=True) images = client.images.pull('hello-world', all_tags=True)
assert len(images) >= 1 assert len(images) >= 1
assert any([ assert any('hello-world:latest' in img.attrs['RepoTags'] for img in images)
'hello-world:latest' in img.attrs['RepoTags'] for img in images
])
def test_load_error(self): def test_load_error(self):
client = docker.from_env(version=TEST_API_VERSION) client = docker.from_env(version=TEST_API_VERSION)

View File

@ -59,11 +59,11 @@ class NetworkTest(BaseIntegrationTest):
network.connect(container) network.connect(container)
container.start() container.start()
assert client.networks.get(network.id).containers == [container] assert client.networks.get(network.id).containers == [container]
network_containers = list( network_containers = [
c c
for net in client.networks.list(ids=[network.id], greedy=True) for net in client.networks.list(ids=[network.id], greedy=True)
for c in net.containers for c in net.containers
) ]
assert network_containers == [container] assert network_containers == [container]
network.disconnect(container) network.disconnect(container)
assert network.containers == [] assert network.containers == []

View File

@ -134,7 +134,7 @@ class BuildTest(BaseAPIIntegrationTest):
logs = logs.decode('utf-8') logs = logs.decode('utf-8')
assert sorted(list(filter(None, logs.split('\n')))) == sorted([ assert sorted(filter(None, logs.split('\n'))) == sorted([
'/test/#file.txt', '/test/#file.txt',
'/test/ignored/subdir/excepted-file', '/test/ignored/subdir/excepted-file',
'/test/not-ignored' '/test/not-ignored'
@ -303,7 +303,7 @@ class BuildTest(BaseAPIIntegrationTest):
) )
self.tmp_imgs.append('dockerpytest_nonebuild') self.tmp_imgs.append('dockerpytest_nonebuild')
logs = [chunk for chunk in stream] logs = list(stream)
assert 'errorDetail' in logs[-1] assert 'errorDetail' in logs[-1]
assert logs[-1]['errorDetail']['code'] == 1 assert logs[-1]['errorDetail']['code'] == 1
@ -383,7 +383,7 @@ class BuildTest(BaseAPIIntegrationTest):
expected = '{0}{2}\n{1}'.format( expected = '{0}{2}\n{1}'.format(
control_chars[0], control_chars[1], snippet control_chars[0], control_chars[1], snippet
) )
assert any([line == expected for line in lines]) assert any(line == expected for line in lines)
def test_build_gzip_encoding(self): def test_build_gzip_encoding(self):
base_dir = tempfile.mkdtemp() base_dir = tempfile.mkdtemp()

View File

@ -31,77 +31,77 @@ class ContainerCollectionTest(unittest.TestCase):
) )
def test_create_container_args(self): def test_create_container_args(self):
create_kwargs = _create_container_args(dict( create_kwargs = _create_container_args({
image='alpine', "image": 'alpine',
command='echo hello world', "command": 'echo hello world',
blkio_weight_device=[{'Path': 'foo', 'Weight': 3}], "blkio_weight_device": [{'Path': 'foo', 'Weight': 3}],
blkio_weight=2, "blkio_weight": 2,
cap_add=['foo'], "cap_add": ['foo'],
cap_drop=['bar'], "cap_drop": ['bar'],
cgroup_parent='foobar', "cgroup_parent": 'foobar',
cgroupns='host', "cgroupns": 'host',
cpu_period=1, "cpu_period": 1,
cpu_quota=2, "cpu_quota": 2,
cpu_shares=5, "cpu_shares": 5,
cpuset_cpus='0-3', "cpuset_cpus": '0-3',
detach=False, "detach": False,
device_read_bps=[{'Path': 'foo', 'Rate': 3}], "device_read_bps": [{'Path': 'foo', 'Rate': 3}],
device_read_iops=[{'Path': 'foo', 'Rate': 3}], "device_read_iops": [{'Path': 'foo', 'Rate': 3}],
device_write_bps=[{'Path': 'foo', 'Rate': 3}], "device_write_bps": [{'Path': 'foo', 'Rate': 3}],
device_write_iops=[{'Path': 'foo', 'Rate': 3}], "device_write_iops": [{'Path': 'foo', 'Rate': 3}],
devices=['/dev/sda:/dev/xvda:rwm'], "devices": ['/dev/sda:/dev/xvda:rwm'],
dns=['8.8.8.8'], "dns": ['8.8.8.8'],
domainname='example.com', "domainname": 'example.com',
dns_opt=['foo'], "dns_opt": ['foo'],
dns_search=['example.com'], "dns_search": ['example.com'],
entrypoint='/bin/sh', "entrypoint": '/bin/sh',
environment={'FOO': 'BAR'}, "environment": {'FOO': 'BAR'},
extra_hosts={'foo': '1.2.3.4'}, "extra_hosts": {'foo': '1.2.3.4'},
group_add=['blah'], "group_add": ['blah'],
ipc_mode='foo', "ipc_mode": 'foo',
kernel_memory=123, "kernel_memory": 123,
labels={'key': 'value'}, "labels": {'key': 'value'},
links={'foo': 'bar'}, "links": {'foo': 'bar'},
log_config={'Type': 'json-file', 'Config': {}}, "log_config": {'Type': 'json-file', 'Config': {}},
lxc_conf={'foo': 'bar'}, "lxc_conf": {'foo': 'bar'},
healthcheck={'test': 'true'}, "healthcheck": {'test': 'true'},
hostname='somehost', "hostname": 'somehost',
mac_address='abc123', "mac_address": 'abc123',
mem_limit=123, "mem_limit": 123,
mem_reservation=123, "mem_reservation": 123,
mem_swappiness=2, "mem_swappiness": 2,
memswap_limit=456, "memswap_limit": 456,
name='somename', "name": 'somename',
network_disabled=False, "network_disabled": False,
network='foo', "network": 'foo',
network_driver_opt={'key1': 'a'}, "network_driver_opt": {'key1': 'a'},
oom_kill_disable=True, "oom_kill_disable": True,
oom_score_adj=5, "oom_score_adj": 5,
pid_mode='host', "pid_mode": 'host',
pids_limit=500, "pids_limit": 500,
platform='linux', "platform": 'linux',
ports={ "ports": {
1111: 4567, 1111: 4567,
2222: None 2222: None
}, },
privileged=True, "privileged": True,
publish_all_ports=True, "publish_all_ports": True,
read_only=True, "read_only": True,
restart_policy={'Name': 'always'}, "restart_policy": {'Name': 'always'},
security_opt=['blah'], "security_opt": ['blah'],
shm_size=123, "shm_size": 123,
stdin_open=True, "stdin_open": True,
stop_signal=9, "stop_signal": 9,
sysctls={'foo': 'bar'}, "sysctls": {'foo': 'bar'},
tmpfs={'/blah': ''}, "tmpfs": {'/blah': ''},
tty=True, "tty": True,
ulimits=[{"Name": "nofile", "Soft": 1024, "Hard": 2048}], "ulimits": [{"Name": "nofile", "Soft": 1024, "Hard": 2048}],
user='bob', "user": 'bob',
userns_mode='host', "userns_mode": 'host',
uts_mode='host', "uts_mode": 'host',
version='1.23', "version": '1.23',
volume_driver='some_driver', "volume_driver": 'some_driver',
volumes=[ "volumes": [
'/home/user1/:/mnt/vol2', '/home/user1/:/mnt/vol2',
'/var/www:/mnt/vol1:ro', '/var/www:/mnt/vol1:ro',
'volumename:/mnt/vol3r', 'volumename:/mnt/vol3r',
@ -109,18 +109,18 @@ class ContainerCollectionTest(unittest.TestCase):
'/anothervolumewithnohostpath:ro', '/anothervolumewithnohostpath:ro',
'C:\\windows\\path:D:\\hello\\world:rw' 'C:\\windows\\path:D:\\hello\\world:rw'
], ],
volumes_from=['container'], "volumes_from": ['container'],
working_dir='/code' "working_dir": '/code'
)) })
expected = dict( expected = {
image='alpine', "image": 'alpine',
command='echo hello world', "command": 'echo hello world',
domainname='example.com', "domainname": 'example.com',
detach=False, "detach": False,
entrypoint='/bin/sh', "entrypoint": '/bin/sh',
environment={'FOO': 'BAR'}, "environment": {'FOO': 'BAR'},
host_config={ "host_config": {
'Binds': [ 'Binds': [
'/home/user1/:/mnt/vol2', '/home/user1/:/mnt/vol2',
'/var/www:/mnt/vol1:ro', '/var/www:/mnt/vol1:ro',
@ -183,20 +183,20 @@ class ContainerCollectionTest(unittest.TestCase):
'VolumeDriver': 'some_driver', 'VolumeDriver': 'some_driver',
'VolumesFrom': ['container'], 'VolumesFrom': ['container'],
}, },
healthcheck={'test': 'true'}, "healthcheck": {'test': 'true'},
hostname='somehost', "hostname": 'somehost',
labels={'key': 'value'}, "labels": {'key': 'value'},
mac_address='abc123', "mac_address": 'abc123',
name='somename', "name": 'somename',
network_disabled=False, "network_disabled": False,
networking_config={'foo': {'driver_opt': {'key1': 'a'}}}, "networking_config": {'foo': {'driver_opt': {'key1': 'a'}}},
platform='linux', "platform": 'linux',
ports=[('1111', 'tcp'), ('2222', 'tcp')], "ports": [('1111', 'tcp'), ('2222', 'tcp')],
stdin_open=True, "stdin_open": True,
stop_signal=9, "stop_signal": 9,
tty=True, "tty": True,
user='bob', "user": 'bob',
volumes=[ "volumes": [
'/mnt/vol2', '/mnt/vol2',
'/mnt/vol1', '/mnt/vol1',
'/mnt/vol3r', '/mnt/vol3r',
@ -204,8 +204,8 @@ class ContainerCollectionTest(unittest.TestCase):
'/anothervolumewithnohostpath', '/anothervolumewithnohostpath',
'D:\\hello\\world' 'D:\\hello\\world'
], ],
working_dir='/code' "working_dir": '/code'
) }
assert create_kwargs == expected assert create_kwargs == expected