Skip to content

Commit d217ee2

Browse files
committed
Merge bitcoin/bitcoin#23420: test: Correct MyPy typing for subtest decorator
467fe57 test: Correct MyPy typing for subtest decorator (Pavel Safronov) Pull request description: This is the part of the effort to make python typing correct bitcoin/bitcoin#19389 The typing of the `subtest` decorator within `p2p_segwit.py` test file was incorrect. Since `subtest` function is defined as a member of the class, it expects `self` as a first argument, and it is not provided. Hence the typing errors (that are currently suppressed by `type: ignore`). ``` (venv) vagrant@ubuntu-focal:/vagrant/test/functional$ mypy p2p_segwit.py p2p_segwit.py:298: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:327: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:358: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:447: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:519: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:561: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:659: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:670: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:737: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:826: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:866: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:941: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:977: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1052: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1089: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1136: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1220: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1312: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1406: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1440: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1543: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1729: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1782: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1881: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:1983: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" p2p_segwit.py:2027: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest" Found 26 errors in 1 file (checked 1 source file) ``` However, the tests are passing, because there is no `self` argument passed when it is called as a decorator. There is also suppressed pylint error `# noqa: N805` pointing to the same issue. ``` N805 first argument of a method should be named 'self' ``` So the solution is to move the `subtest` definition outside the class, so the `self` argument is no longer required. After doing so, both mypy and unittests are successfully passing: ``` (venv) vagrant@ubuntu-focal:/vagrant/test/functional$ mypy p2p_segwit.py Success: no issues found in 1 source file ``` ``` (venv) vagrant@ubuntu-focal:/vagrant/test/functional$ ./test_runner.py p2p_segwit Temporary test directory at /tmp/test_runner__🏃_20211103_011449 Running Unit Tests for Test Framework Modules .......... ---------------------------------------------------------------------- Ran 10 tests in 0.546s OK Remaining jobs: [p2p_segwit.py] 1/1 - p2p_segwit.py passed, Duration: 81 s TEST | STATUS | DURATION p2p_segwit.py | ✓ Passed | 81 s ALL | ✓ Passed | 81 s (accumulated) Runtime: 81 s ``` ``` ACKs for top commit: fanquake: ACK 467fe57 Tree-SHA512: e4c3e2d284f47a6bfbf4af22d4021123cdd9c2ea16ec90a91b466ad1a5af615bb4e15959e6cf56c788701d7e7cbda91a8ffc4347965095c3384eae3d28f261af
2 parents f63bf05 + 467fe57 commit d217ee2

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

test/functional/p2p_segwit.py

+43-41
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ def __init__(self, sha256, n, value):
101101
self.n = n
102102
self.nValue = value
103103

104+
105+
def subtest(func):
106+
"""Wraps the subtests for logging and state assertions."""
107+
def func_wrapper(self, *args, **kwargs):
108+
self.log.info("Subtest: {} (Segwit active = {})".format(func.__name__, self.segwit_active))
109+
# Assert segwit status is as expected
110+
assert_equal(softfork_active(self.nodes[0], 'segwit'), self.segwit_active)
111+
func(self, *args, **kwargs)
112+
# Each subtest should leave some utxos for the next subtest
113+
assert self.utxo
114+
self.sync_blocks()
115+
# Assert segwit status is as expected at end of subtest
116+
assert_equal(softfork_active(self.nodes[0], 'segwit'), self.segwit_active)
117+
118+
return func_wrapper
119+
120+
104121
def sign_p2pk_witness_input(script, tx_to, in_idx, hashtype, value, key):
105122
"""Add signature for a P2PK witness script."""
106123
tx_hash = SegwitV0SignatureHash(script, tx_to, in_idx, hashtype, value)
@@ -280,22 +297,7 @@ def run_test(self):
280297

281298
# Individual tests
282299

283-
def subtest(func): # noqa: N805
284-
"""Wraps the subtests for logging and state assertions."""
285-
def func_wrapper(self, *args, **kwargs):
286-
self.log.info("Subtest: {} (Segwit active = {})".format(func.__name__, self.segwit_active))
287-
# Assert segwit status is as expected
288-
assert_equal(softfork_active(self.nodes[0], 'segwit'), self.segwit_active)
289-
func(self, *args, **kwargs)
290-
# Each subtest should leave some utxos for the next subtest
291-
assert self.utxo
292-
self.sync_blocks()
293-
# Assert segwit status is as expected at end of subtest
294-
assert_equal(softfork_active(self.nodes[0], 'segwit'), self.segwit_active)
295-
296-
return func_wrapper
297-
298-
@subtest # type: ignore
300+
@subtest
299301
def test_non_witness_transaction(self):
300302
"""See if sending a regular transaction works, and create a utxo to use in later tests."""
301303
# Mine a block with an anyone-can-spend coinbase,
@@ -324,7 +326,7 @@ def test_non_witness_transaction(self):
324326
self.utxo.append(UTXO(tx.sha256, 0, 49 * 100000000))
325327
self.generate(self.nodes[0], 1)
326328

327-
@subtest # type: ignore
329+
@subtest
328330
def test_unnecessary_witness_before_segwit_activation(self):
329331
"""Verify that blocks with witnesses are rejected before activation."""
330332

@@ -355,7 +357,7 @@ def test_unnecessary_witness_before_segwit_activation(self):
355357
self.utxo.pop(0)
356358
self.utxo.append(UTXO(tx.sha256, 0, tx.vout[0].nValue))
357359

358-
@subtest # type: ignore
360+
@subtest
359361
def test_block_relay(self):
360362
"""Test that block requests to NODE_WITNESS peer are with MSG_WITNESS_FLAG.
361363
@@ -444,7 +446,7 @@ def test_block_relay(self):
444446
self.old_node.announce_tx_and_wait_for_getdata(block4.vtx[0])
445447
assert block4.sha256 not in self.old_node.getdataset
446448

447-
@subtest # type: ignore
449+
@subtest
448450
def test_v0_outputs_arent_spendable(self):
449451
"""Test that v0 outputs aren't spendable before segwit activation.
450452
@@ -516,7 +518,7 @@ def test_v0_outputs_arent_spendable(self):
516518
self.utxo.pop(0)
517519
self.utxo.append(UTXO(txid, 2, value))
518520

519-
@subtest # type: ignore
521+
@subtest
520522
def test_witness_tx_relay_before_segwit_activation(self):
521523

522524
# Generate a transaction that doesn't require a witness, but send it
@@ -558,7 +560,7 @@ def test_witness_tx_relay_before_segwit_activation(self):
558560
self.utxo.pop(0)
559561
self.utxo.append(UTXO(tx_hash, 0, tx_value))
560562

561-
@subtest # type: ignore
563+
@subtest
562564
def test_standardness_v0(self):
563565
"""Test V0 txout standardness.
564566
@@ -656,7 +658,7 @@ def test_standardness_v0(self):
656658
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
657659
assert_equal(len(self.nodes[1].getrawmempool()), 0)
658660

659-
@subtest # type: ignore
661+
@subtest
660662
def advance_to_segwit_active(self):
661663
"""Mine enough blocks to activate segwit."""
662664
assert not softfork_active(self.nodes[0], 'segwit')
@@ -667,7 +669,7 @@ def advance_to_segwit_active(self):
667669
assert softfork_active(self.nodes[0], 'segwit')
668670
self.segwit_active = True
669671

670-
@subtest # type: ignore
672+
@subtest
671673
def test_p2sh_witness(self):
672674
"""Test P2SH wrapped witness programs."""
673675

@@ -734,7 +736,7 @@ def test_p2sh_witness(self):
734736
self.utxo.pop(0)
735737
self.utxo.append(UTXO(spend_tx.sha256, 0, spend_tx.vout[0].nValue))
736738

737-
@subtest # type: ignore
739+
@subtest
738740
def test_witness_commitments(self):
739741
"""Test witness commitments.
740742
@@ -823,7 +825,7 @@ def test_witness_commitments(self):
823825
self.utxo.pop(0)
824826
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
825827

826-
@subtest # type: ignore
828+
@subtest
827829
def test_block_malleability(self):
828830

829831
# Make sure that a block that has too big a virtual size
@@ -863,7 +865,7 @@ def test_block_malleability(self):
863865
block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(0)]
864866
test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
865867

866-
@subtest # type: ignore
868+
@subtest
867869
def test_witness_block_size(self):
868870
# TODO: Test that non-witness carrying blocks can't exceed 1MB
869871
# Skipping this test for now; this is covered in feature_block.py
@@ -938,7 +940,7 @@ def test_witness_block_size(self):
938940
self.utxo.pop(0)
939941
self.utxo.append(UTXO(block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue))
940942

941-
@subtest # type: ignore
943+
@subtest
942944
def test_submit_block(self):
943945
"""Test that submitblock adds the nonce automatically when possible."""
944946
block = self.build_next_block()
@@ -974,7 +976,7 @@ def test_submit_block(self):
974976
# Tip should not advance!
975977
assert self.nodes[0].getbestblockhash() != block_2.hash
976978

977-
@subtest # type: ignore
979+
@subtest
978980
def test_extra_witness_data(self):
979981
"""Test extra witness data in a transaction."""
980982

@@ -1049,7 +1051,7 @@ def test_extra_witness_data(self):
10491051
self.utxo.pop(0)
10501052
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
10511053

1052-
@subtest # type: ignore
1054+
@subtest
10531055
def test_max_witness_push_length(self):
10541056
"""Test that witness stack can only allow up to 520 byte pushes."""
10551057

@@ -1086,7 +1088,7 @@ def test_max_witness_push_length(self):
10861088
self.utxo.pop()
10871089
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
10881090

1089-
@subtest # type: ignore
1091+
@subtest
10901092
def test_max_witness_script_length(self):
10911093
"""Test that witness outputs greater than 10kB can't be spent."""
10921094

@@ -1133,7 +1135,7 @@ def test_max_witness_script_length(self):
11331135
self.utxo.pop()
11341136
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
11351137

1136-
@subtest # type: ignore
1138+
@subtest
11371139
def test_witness_input_length(self):
11381140
"""Test that vin length must match vtxinwit length."""
11391141

@@ -1217,7 +1219,7 @@ def serialize_with_witness(self):
12171219
self.utxo.pop()
12181220
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
12191221

1220-
@subtest # type: ignore
1222+
@subtest
12211223
def test_tx_relay_after_segwit_activation(self):
12221224
"""Test transaction relay after segwit activation.
12231225
@@ -1309,7 +1311,7 @@ def test_tx_relay_after_segwit_activation(self):
13091311
self.utxo.pop(0)
13101312
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
13111313

1312-
@subtest # type: ignore
1314+
@subtest
13131315
def test_segwit_versions(self):
13141316
"""Test validity of future segwit version transactions.
13151317
@@ -1403,7 +1405,7 @@ def test_segwit_versions(self):
14031405
# Add utxo to our list
14041406
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
14051407

1406-
@subtest # type: ignore
1408+
@subtest
14071409
def test_premature_coinbase_witness_spend(self):
14081410

14091411
block = self.build_next_block()
@@ -1437,7 +1439,7 @@ def test_premature_coinbase_witness_spend(self):
14371439
test_witness_block(self.nodes[0], self.test_node, block2, accepted=True)
14381440
self.sync_blocks()
14391441

1440-
@subtest # type: ignore
1442+
@subtest
14411443
def test_uncompressed_pubkey(self):
14421444
"""Test uncompressed pubkey validity in segwit transactions.
14431445
@@ -1540,7 +1542,7 @@ def test_uncompressed_pubkey(self):
15401542
test_witness_block(self.nodes[0], self.test_node, block, accepted=True)
15411543
self.utxo.append(UTXO(tx5.sha256, 0, tx5.vout[0].nValue))
15421544

1543-
@subtest # type: ignore
1545+
@subtest
15441546
def test_signature_version_1(self):
15451547

15461548
key = ECKey()
@@ -1726,7 +1728,7 @@ def test_signature_version_1(self):
17261728
for i in range(len(tx.vout)):
17271729
self.utxo.append(UTXO(tx.sha256, i, tx.vout[i].nValue))
17281730

1729-
@subtest # type: ignore
1731+
@subtest
17301732
def test_non_standard_witness_blinding(self):
17311733
"""Test behavior of unnecessary witnesses in transactions does not blind the node for the transaction"""
17321734

@@ -1779,7 +1781,7 @@ def test_non_standard_witness_blinding(self):
17791781
self.utxo.pop(0)
17801782
self.utxo.append(UTXO(tx3.sha256, 0, tx3.vout[0].nValue))
17811783

1782-
@subtest # type: ignore
1784+
@subtest
17831785
def test_non_standard_witness(self):
17841786
"""Test detection of non-standard P2WSH witness"""
17851787
pad = chr(1).encode('latin-1')
@@ -1878,7 +1880,7 @@ def test_non_standard_witness(self):
18781880

18791881
self.utxo.pop(0)
18801882

1881-
@subtest # type: ignore
1883+
@subtest
18821884
def test_witness_sigops(self):
18831885
"""Test sigop counting is correct inside witnesses."""
18841886

@@ -1980,7 +1982,7 @@ def test_witness_sigops(self):
19801982
self.utxo.pop(0)
19811983
self.utxo.append(UTXO(tx2.sha256, 0, tx2.vout[0].nValue))
19821984

1983-
@subtest # type: ignore
1985+
@subtest
19841986
def test_superfluous_witness(self):
19851987
# Serialization of tx that puts witness flag to 3 always
19861988
def serialize_with_bogus_witness(tx):
@@ -2024,7 +2026,7 @@ def serialize(self):
20242026
with self.nodes[0].assert_debug_log(['Unknown transaction optional data']):
20252027
self.test_node.send_and_ping(msg_bogus_tx(tx))
20262028

2027-
@subtest # type: ignore
2029+
@subtest
20282030
def test_wtxid_relay(self):
20292031
# Use brand new nodes to avoid contamination from earlier tests
20302032
self.wtx_node = self.nodes[0].add_p2p_connection(TestP2PConn(wtxidrelay=True), services=P2P_SERVICES)

0 commit comments

Comments
 (0)