Skip to content

Commit 89a071a

Browse files
authored
pylint: fix W0613,W0621,W1406 (#1282)
1 parent 46cfa92 commit 89a071a

18 files changed

+72
-45
lines changed

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ disable = [
3232
"R0913", # too-many-arguments
3333
"R0914", # too-many-locals
3434
"R0915", # too-many-statements
35+
"W0511", # fixme
36+
"W0718", # broad-exception-caught
37+
"W0719", # broad-exception-raised
3538
]
3639

3740
enable = [
@@ -58,7 +61,10 @@ enable = [
5861
"W0201", # attribute-defined-outside-init
5962
"W0212", # protected-access
6063
"W0612", # unused-variable
64+
"W0613", # unused-argument
65+
"W0621", # redefined-outer-name
6166
"W0707", # raise-missing-from
6267
"W1202", # logging-format-interpolation
6368
"W1203", # logging-fstring-interpolation
69+
"W1406", # redundant-u-string-prefix
6470
]

src/ansible_runner/config/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def __init__(self,
6666
container_image=None, container_volume_mounts=None, container_options=None, container_workdir=None, container_auth_data=None,
6767
ident=None, rotate_artifacts=0, timeout=None, ssh_key=None, quiet=False, json_mode=False,
6868
check_job_event_data=False, suppress_env_files=False, keepalive_seconds=None):
69+
# pylint: disable=W0613
70+
6971
# common params
7072
self.host_cwd = host_cwd
7173
self.envvars = envvars

src/ansible_runner/runner.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,10 @@ def run(self):
336336
# extra_update_fields['job_explanation'] = "Job terminated due to timeout"
337337
if self.canceled or self.timed_out or self.errored:
338338
self.kill_container()
339-
Runner.handle_termination(child.pid, is_cancel=self.canceled)
339+
Runner.handle_termination(child.pid)
340340
if self.config.idle_timeout and (time.time() - self.last_stdout_update) > self.config.idle_timeout:
341341
self.kill_container()
342-
Runner.handle_termination(child.pid, is_cancel=False)
342+
Runner.handle_termination(child.pid)
343343
self.timed_out = True
344344

345345
stdout_handle.close()
@@ -526,14 +526,12 @@ def kill_container(self):
526526
logger.info("Killed container %s", container_name)
527527

528528
@classmethod
529-
def handle_termination(cls, pid, pidfile=None, is_cancel=True):
529+
def handle_termination(cls, pid, pidfile=None):
530530
'''
531531
Internal method to terminate a subprocess spawned by ``pexpect`` representing an invocation of runner.
532532
533533
:param pid: the process id of the running the job.
534534
:param pidfile: the daemon's PID file
535-
:param is_cancel: flag showing whether this termination is caused by
536-
instance's cancel_flag.
537535
'''
538536

539537
try:

src/ansible_runner/streaming.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def run(self):
223223

224224
@_synchronize_output_reset_keepalive # type: ignore
225225
def status_handler(self, status_data, runner_config):
226+
# pylint: disable=W0613
226227
self.status = status_data['status']
227228
self._output.write(json.dumps(status_data).encode('utf-8'))
228229
self._output.write(b'\n')
@@ -241,6 +242,7 @@ def artifacts_handler(self, artifact_dir):
241242

242243
@_synchronize_output_reset_keepalive # type: ignore
243244
def finished_callback(self, runner_obj):
245+
# pylint: disable=W0613
244246
self._end_keepalive() # ensure that we can't splat a keepalive event after the eof event
245247
self._output.write(json.dumps({'eof': True}).encode('utf-8'))
246248
self._output.write(b'\n')
@@ -304,15 +306,15 @@ def status_callback(self, status_data):
304306
def event_callback(self, event_data):
305307
# FIXME: this needs to be more defensive to not blow up on "malformed" events or new values it doesn't recognize
306308
counter = event_data.get('counter')
307-
uuid = event_data.get('uuid')
309+
uuid_val = event_data.get('uuid')
308310

309-
if not counter or not uuid:
311+
if not counter or not uuid_val:
310312
# FIXME: log a warning about a malformed event?
311313
return
312314

313315
full_filename = os.path.join(self.artifact_dir,
314316
'job_events',
315-
f'{counter}-{uuid}.json')
317+
f'{counter}-{uuid_val}.json')
316318

317319
if not self.quiet and 'stdout' in event_data:
318320
print(event_data['stdout'])

src/ansible_runner/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ def signal_handler():
500500

501501
# closure to set signal event
502502
def _handler(number, frame):
503+
# pylint: disable=W0613
503504
signal_event.set()
504505

505506
signal.signal(signal.SIGTERM, _handler)

test/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pylint: disable=W0621
2+
13
import shutil
24

35
from pathlib import Path

test/unit/config/test__base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
def load_file_side_effect(path, value=None, *args, **kwargs):
18+
# pylint: disable=W0613
1819
if args[0] == path:
1920
if value:
2021
return value
@@ -36,6 +37,7 @@ def test_base_config_init_defaults(tmp_path):
3637

3738

3839
def test_base_config_with_artifact_dir(tmp_path, patch_private_data_dir):
40+
# pylint: disable=W0613
3941
rc = BaseConfig(artifact_dir=tmp_path.joinpath('this-is-some-dir').as_posix())
4042
assert rc.artifact_dir == tmp_path.joinpath('this-is-some-dir').joinpath(rc.ident).as_posix()
4143

test/unit/config/test_ansible_cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
def test_ansible_cfg_init_defaults(tmp_path, patch_private_data_dir):
13+
# pylint: disable=W0613
1314
rc = AnsibleCfgConfig()
1415

1516
# Check that the private data dir is placed in our default location with our default prefix

test/unit/config/test_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
def test_ansible_config_defaults(tmp_path, patch_private_data_dir):
12+
# pylint: disable=W0613
1213
rc = CommandConfig()
1314

1415
# Check that the private data dir is placed in our default location with our default prefix

test/unit/config/test_container_volmount_generation.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" Ensure the generation of container volume mounts is handled
22
predictably and consistently """
33

4-
# pylint: disable=W0212
4+
# pylint: disable=W0212,W0621
55

66
import os
77
from typing import NamedTuple
@@ -60,13 +60,13 @@ def resolve_path(path):
6060
return os.path.abspath(os.path.expanduser(os.path.expandvars(path)))
6161

6262

63-
def generate_volmount_args(src_str, dst_str, labels):
63+
def generate_volmount_args(src_str, dst_str, vol_labels):
6464
"""Generate a podman style volmount string"""
6565
vol_mount_str = f"{src_str}:{dst_str}"
66-
if labels:
67-
if not labels.startswith(":"):
66+
if vol_labels:
67+
if not vol_labels.startswith(":"):
6868
vol_mount_str += ":"
69-
vol_mount_str += labels
69+
vol_mount_str += vol_labels
7070
return ["-v", vol_mount_str]
7171

7272

@@ -95,12 +95,12 @@ def test_check_not_safe_to_mount_file(not_safe, mocker):
9595

9696
@pytest.mark.parametrize("path", dir_variations, ids=id_for_src)
9797
def test_duplicate_detection_dst(path, mocker):
98+
"""Ensure no duplicate volume mount entries are created"""
9899
mocker.patch("os.path.exists", return_value=True)
99100
mocker.patch("os.path.isdir", return_value=True)
100-
"""Ensure no duplicate volume mount entries are created"""
101101
base_config = BaseConfig()
102102

103-
def generate(args_list):
103+
def generate():
104104
for entry in dir_variations:
105105
for label in labels:
106106
base_config._update_volume_mount_paths(
@@ -111,9 +111,9 @@ def generate(args_list):
111111
)
112112

113113
first_pass = []
114-
generate(first_pass)
114+
generate()
115115
second_pass = first_pass[:]
116-
generate(second_pass)
116+
generate()
117117
assert first_pass == second_pass
118118

119119

@@ -125,7 +125,7 @@ def test_no_dst_all_dirs(path, labels, mocker):
125125
"""Ensure dst == src when not provided"""
126126
src_str = os.path.join(resolve_path(path.path), "")
127127
dst_str = src_str
128-
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, labels=labels)
128+
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, vol_labels=labels)
129129

130130
result = []
131131
BaseConfig()._update_volume_mount_paths(
@@ -150,7 +150,7 @@ def test_src_dst_all_dirs(src, dst, labels, mocker):
150150
"""Ensure src and dest end with trailing slash"""
151151
src_str = os.path.join(resolve_path(src.path), "")
152152
dst_str = os.path.join(resolve_path(dst.path), "")
153-
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, labels=labels)
153+
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, vol_labels=labels)
154154

155155
result = []
156156
BaseConfig()._update_volume_mount_paths(
@@ -172,7 +172,7 @@ def test_src_dst_all_files(path, labels, mocker):
172172
"""Ensure file paths are transformed correctly into dir paths"""
173173
src_str = os.path.join(resolve_path(path.path), "")
174174
dst_str = src_str
175-
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, labels=labels)
175+
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, vol_labels=labels)
176176

177177
result = []
178178
src_file = os.path.join(path.path, "", "file.txt")
@@ -207,7 +207,7 @@ def test_src_dst_all_relative_dirs(src, dst, labels, relative, mocker):
207207
workdir = "/workdir"
208208
src_str = os.path.join(resolve_path(relative_src), "")
209209
dst_str = os.path.join(resolve_path(os.path.join(workdir, relative_dst)), "")
210-
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, labels=labels)
210+
expected = generate_volmount_args(src_str=src_str, dst_str=dst_str, vol_labels=labels)
211211

212212
result = []
213213
BaseConfig(container_workdir=workdir)._update_volume_mount_paths(

test/unit/config/test_doc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
def test_ansible_doc_defaults(tmp_path, patch_private_data_dir):
13+
# pylint: disable=W0613
1314
rc = DocConfig()
1415

1516
# Check that the private data dir is placed in our default location with our default prefix

test/unit/config/test_inventory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
def test_ansible_inventory_init_defaults(tmp_path, patch_private_data_dir):
13+
# pylint: disable=W0613
1314
rc = InventoryConfig()
1415

1516
# Check that the private data dir is placed in our default location with our default prefix

test/unit/config/test_runner.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
def load_file_side_effect(path, value=None, *args, **kwargs):
19+
# pylint: disable=W0613
1920
if args[0] == path:
2021
if value:
2122
return value
@@ -373,8 +374,8 @@ def test_generate_ansible_command_with_dict_extravars(mocker):
373374

374375

375376
@pytest.mark.parametrize('cmdline,tokens', [
376-
(u'--tags foo --skip-tags', ['--tags', 'foo', '--skip-tags']),
377-
(u'--limit "䉪ቒ칸ⱷ?噂폄蔆㪗輥"', ['--limit', '䉪ቒ칸ⱷ?噂폄蔆㪗輥']),
377+
('--tags foo --skip-tags', ['--tags', 'foo', '--skip-tags']),
378+
('--limit "䉪ቒ칸ⱷ?噂폄蔆㪗輥"', ['--limit', '䉪ቒ칸ⱷ?噂폄蔆㪗輥']),
378379
])
379380
def test_generate_ansible_command_with_cmdline_args(cmdline, tokens, mocker):
380381
mocker.patch('os.makedirs', return_value=True)
@@ -400,7 +401,7 @@ def test_prepare_command_defaults(mocker):
400401
cmd_side_effect = partial(load_file_side_effect, 'args')
401402

402403
def generate_side_effect():
403-
return StringIO(u'test "string with spaces"')
404+
return StringIO('test "string with spaces"')
404405

405406
mocker.patch.object(rc.loader, 'load_file', side_effect=cmd_side_effect)
406407
mocker.patch.object(rc, 'generate_ansible_command', side_effect=generate_side_effect)
@@ -552,6 +553,7 @@ def test_bwrap_process_isolation_defaults(mocker):
552553

553554

554555
def test_bwrap_process_isolation_and_directory_isolation(mocker, patch_private_data_dir, tmp_path):
556+
# pylint: disable=W0613
555557

556558
def mock_exists(path):
557559
if path == "/project":
@@ -565,7 +567,7 @@ def __init__(self, base_path):
565567
def load_file(self, path, objtype=None, encoding='utf-8'):
566568
raise ConfigurationError
567569

568-
def isfile(self, path):
570+
def isfile(self, _):
569571
return False
570572

571573
mocker.patch('ansible_runner.config.runner.os.makedirs', return_value=True)

0 commit comments

Comments
 (0)