Skip to content

test: Correct MyPy typing for subtest decorator - #23420

Merged
fanquake merged 1 commit into
bitcoin:masterfrom
fspv:master
Nov 11, 2021
Merged

fanquake merged 1 commit into
bitcoin:masterfrom
fspv:master

Conversation

@fspv

@fspv fspv commented Nov 3, 2021 •

Copy link
Copy Markdown
Contributor

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).

(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

@DrahtBot DrahtBot added the Tests label Nov 3, 2021
@fspv fspv mentioned this pull request Nov 3, 2021
3 tasks
@maflcko maflcko changed the title [MyPy] Correct typing for @subtest decorator test: Correct MyPy typing for @subtest decorator Nov 3, 2021
@fspv

fspv commented Nov 3, 2021

Copy link
Copy Markdown
Contributor Author

@MarcoFalke Do you still think this should be merged, despite the parent issue has been closed?

@fanquake

fanquake commented Nov 6, 2021

Copy link
Copy Markdown
Member

@prius yes this should be left open.

@laanwj

laanwj commented Nov 10, 2021 •

Copy link
Copy Markdown
Member

Code review ACK 83b32fe599c91fc52d89f4172c0b064d643620d8

I would prefer for the @ to be removed from the commit message and PR title, because otherwise it it will notify the github user subtest every time something is done with the commit.

@fspv fspv changed the title test: Correct MyPy typing for @subtest decorator test: Correct MyPy typing for subtest decorator Nov 10, 2021
@fspv

fspv commented Nov 10, 2021

Copy link
Copy Markdown
Contributor Author

@laanwj Good catch! Removed @ from everywhere.

@fanquake

Copy link
Copy Markdown
Member

Removed @ from everywhere.

It's still in the commit message.

@fspv

fspv commented Nov 11, 2021

Copy link
Copy Markdown
Contributor Author

@fanquake Commit message changed to be the same as PR title.

@fanquake fanquake left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 467fe57

@fanquake
fanquake merged commit d217ee2 into bitcoin:master Nov 11, 2021
@bitcoin bitcoin locked and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants