From 24db4dc8760bb2e2b544392431690d4e1663b58f Mon Sep 17 00:00:00 2001 From: Stanislav Levin Date: Mon, 19 Dec 2022 17:22:10 +0300 Subject: [PATCH] pylint: Fix modified-iterating-list https://pylint.pycqa.org/en/latest/user_guide/messages/warning/modified-iterating-list.html: > Emitted when items are added or removed to a list being iterated through. Doing so can result in unexpected behaviour, that is why it is preferred to use a copy of the list. https://docs.python.org/3/tutorial/controlflow.html#for-statements: > Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new collection Fixes: https://pagure.io/freeipa/issue/9278 Signed-off-by: Stanislav Levin Reviewed-By: Stanislav Levin --- ipaserver/plugins/schema.py | 11 +++++++---- ipatests/pytest_ipa/integration/tasks.py | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ipaserver/plugins/schema.py b/ipaserver/plugins/schema.py index 774bf1aa5..8d2780331 100644 --- a/ipaserver/plugins/schema.py +++ b/ipaserver/plugins/schema.py @@ -802,16 +802,19 @@ class schema(Command): to_process = [data] fingerprint = hashlib.sha1() - for entry in to_process: + i = 0 + while i < len(to_process): + entry = to_process[i] if isinstance(entry, (list, tuple)): for item in entry: - to_process.append(item) # pylint: disable=W4701 + to_process.append(item) elif isinstance(entry, dict): for key in sorted(entry.keys()): - to_process.append(key) # pylint: disable=W4701 - to_process.append(entry[key]) # pylint: disable=W4701 + to_process.append(key) + to_process.append(entry[key]) else: fingerprint.update(unicode(entry).encode('utf-8')) + i += 1 return unicode(fingerprint.hexdigest()[:8]) diff --git a/ipatests/pytest_ipa/integration/tasks.py b/ipatests/pytest_ipa/integration/tasks.py index 76bf34a3f..8cacf8d15 100755 --- a/ipatests/pytest_ipa/integration/tasks.py +++ b/ipatests/pytest_ipa/integration/tasks.py @@ -1350,7 +1350,9 @@ def two_connected_topo(master, replicas): yield v1, v3 grow.append((v1, v3)) - for (r, s) in grow: + i = 0 + while i < len(grow): + r, s = grow[i] t = pool.pop(0) for (u, v) in [(r, t), (s, t)]: @@ -1360,7 +1362,8 @@ def two_connected_topo(master, replicas): x = pool.pop(0) yield v, x yield w, x - grow.append((w, x)) # pylint: disable=modified-iterating-list + grow.append((w, x)) + i += 1 except IndexError: return