test: Correct MyPy typing for subtest decorator #23420

pull fspv wants to merge 1 commits into bitcoin:master from fspv:master changing 1 files +43 −41
  1. fspv commented at 1:23 am on November 3, 2021: 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).

     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
    
  2. DrahtBot added the label Tests on Nov 3, 2021
  3. MarcoFalke renamed this:
    [MyPy] Correct typing for @subtest decorator
    test: Correct MyPy typing for @subtest decorator
    on Nov 3, 2021
  4. fspv commented at 12:55 pm on November 3, 2021: contributor
    @MarcoFalke Do you still think this should be merged, despite the parent issue has been closed?
  5. fanquake commented at 4:42 am on November 6, 2021: member
    @prius yes this should be left open.
  6. laanwj commented at 6:26 pm on November 10, 2021: 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.

  7. fspv renamed this:
    test: Correct MyPy typing for @subtest decorator
    test: Correct MyPy typing for subtest decorator
    on Nov 10, 2021
  8. fspv commented at 7:16 pm on November 10, 2021: contributor
    @laanwj Good catch! Removed @ from everywhere.
  9. fanquake commented at 2:06 am on November 11, 2021: member

    Removed @ from everywhere.

    It’s still in the commit message.

  10. test: Correct MyPy typing for subtest decorator 467fe5779c
  11. fspv commented at 11:36 am on November 11, 2021: contributor
    @fanquake Commit message changed to be the same as PR title.
  12. fanquake approved
  13. fanquake commented at 12:32 pm on November 11, 2021: member
    ACK 467fe5779ca85bd8d58fde65948ffc8c35078e60
  14. fanquake merged this on Nov 11, 2021
  15. fanquake closed this on Nov 11, 2021

  16. sidhujag referenced this in commit e6b0f71dc7 on Nov 11, 2021
  17. DrahtBot locked this on Nov 11, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-25 06:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me