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)
else:
return sep.join(
[x for x in self._multiplexed_buffer_helper(res)]
list(self._multiplexed_buffer_helper(res))
)
def _unmount(self, *args):

View File

@ -217,7 +217,7 @@ def parse_host(addr, is_win32=False, tls=False):
parsed_url = urlparse(addr)
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
parsed_url = urlparse(f"//{addr}", 'tcp')
proto = 'tcp'

View File

@ -4,5 +4,16 @@ requires = ["setuptools>=45", "setuptools_scm[toml]>=6.2"]
[tool.setuptools_scm]
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]
"**/__init__.py" = ["F401"]

View File

@ -30,7 +30,7 @@ extras_require = {
}
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 = ''

View File

@ -142,7 +142,7 @@ class BuildTest(BaseAPIIntegrationTest):
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/ignored/subdir/excepted-with-spaces',
'/test/ignored/subdir/excepted-file',
@ -312,7 +312,7 @@ class BuildTest(BaseAPIIntegrationTest):
)
self.tmp_imgs.append('dockerpytest_nonebuild')
logs = [chunk for chunk in stream]
logs = list(stream)
assert 'errorDetail' in logs[-1]
assert logs[-1]['errorDetail']['code'] == 1
@ -392,7 +392,7 @@ class BuildTest(BaseAPIIntegrationTest):
expected = '{0}{2}\n{1}'.format(
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):
base_dir = tempfile.mkdtemp()

View File

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

View File

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

View File

@ -118,7 +118,7 @@ class PluginTest(BaseAPIIntegrationTest):
pass
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 self.client.inspect_plugin(SSHFS)
assert self.client.enable_plugin(SSHFS)
@ -128,7 +128,7 @@ class PluginTest(BaseAPIIntegrationTest):
pl_data = self.ensure_plugin_installed(SSHFS)
assert pl_data['Enabled'] is False
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 self.client.inspect_plugin(SSHFS)
assert self.client.enable_plugin(SSHFS)

View File

@ -29,7 +29,7 @@ class ContextLifecycleTest(BaseAPIIntegrationTest):
"test", tls_cfg=docker_tls)
# 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'
assert ContextAPI.get_context("test")
# remove context

View File

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

View File

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

View File

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

View File

@ -134,7 +134,7 @@ class BuildTest(BaseAPIIntegrationTest):
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/ignored/subdir/excepted-file',
'/test/not-ignored'
@ -303,7 +303,7 @@ class BuildTest(BaseAPIIntegrationTest):
)
self.tmp_imgs.append('dockerpytest_nonebuild')
logs = [chunk for chunk in stream]
logs = list(stream)
assert 'errorDetail' in logs[-1]
assert logs[-1]['errorDetail']['code'] == 1
@ -383,7 +383,7 @@ class BuildTest(BaseAPIIntegrationTest):
expected = '{0}{2}\n{1}'.format(
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):
base_dir = tempfile.mkdtemp()

View File

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