Avoid using mutable default parameter values
Summary:
Using mutable default arguments (lists, dicts) in python is usually a very bad idea.
https://florimond.dev/blog/articles/2018/08/python-mutable-defaults-are-the-source-of-all-evil/
This fixes a few instances in the main CI test directory, and adds a linter to detect new cases.
I found some more cases in contrib/buildbot and contrib/teamcity, that I would like to address in a separate diff because they are a bit trickier to investigate and harder to debug.
Backport of Core PR16726
Test Plan:
ninja && ninja check-functional
arc lint --everything, to make sure there are no false positive.
Example of detection (mutable default parameter added on purpose):
>>> Lint for test/functional/abc-transaction-ordering.py: Warning (PYTHON_MUTABLE_DEFAULT1) Mutable default arguments should generally not be used in python. Found mutable default argument in function. 49 self.blocks = {} 50 self.extra_args = [['-whitelist=127.0.0.1']] 51 >>> 52 def add_transactions_to_block(self, block, tx_list=[]): 53 [tx.rehash() for tx in tx_list] 54 block.vtx.extend(tx_list) 55
I also tested it with dictionaries or set literals, and functions with parameters on multiple lines.
Reviewers: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, Fabien
Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, deadalnix, Fabien
Subscribers: deadalnix, Fabien
Differential Revision: https://reviews.bitcoinabc.org/D7910