This is the part of the effort to make python typing correct #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
).
0(venv) vagrant@ubuntu-focal:/vagrant/test/functional$ mypy p2p_segwit.py
1p2p_segwit.py:298: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
2p2p_segwit.py:327: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
3p2p_segwit.py:358: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
4p2p_segwit.py:447: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
5p2p_segwit.py:519: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
6p2p_segwit.py:561: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
7p2p_segwit.py:659: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
8p2p_segwit.py:670: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
9p2p_segwit.py:737: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
10p2p_segwit.py:826: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
11p2p_segwit.py:866: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
12p2p_segwit.py:941: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
13p2p_segwit.py:977: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
14p2p_segwit.py:1052: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
15p2p_segwit.py:1089: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
16p2p_segwit.py:1136: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
17p2p_segwit.py:1220: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
18p2p_segwit.py:1312: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
19p2p_segwit.py:1406: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
20p2p_segwit.py:1440: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
21p2p_segwit.py:1543: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
22p2p_segwit.py:1729: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
23p2p_segwit.py:1782: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
24p2p_segwit.py:1881: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
25p2p_segwit.py:1983: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
26p2p_segwit.py:2027: error: Argument 1 to "subtest" has incompatible type "Callable[[SegWitTest], Any]"; expected "SegWitTest"
27Found 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.
0N805 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:
0(venv) vagrant@ubuntu-focal:/vagrant/test/functional$ mypy p2p_segwit.py
1Success: no issues found in 1 source file
0(venv) vagrant@ubuntu-focal:/vagrant/test/functional$ ./test_runner.py p2p_segwit
1Temporary test directory at /tmp/test_runner_₿_🏃_20211103_011449
2Running Unit Tests for Test Framework Modules
3..........
4----------------------------------------------------------------------
5Ran 10 tests in 0.546s
6
7OK
8Remaining jobs: [p2p_segwit.py]
91/1 - p2p_segwit.py passed, Duration: 81 s
10
11TEST | STATUS | DURATION
12
13p2p_segwit.py | ✓ Passed | 81 s
14
15ALL | ✓ Passed | 81 s (accumulated)
16Runtime: 81 s