Add Travis check for unused Python imports #11835

pull practicalswift wants to merge 2 commits into bitcoin:master from practicalswift:lint-python changing 5 files +13 −4
  1. practicalswift commented at 8:27 PM on December 5, 2017: contributor

    Add Travis check for unused Python imports.

    $ contrib/devtools/lint-python.sh
    ./test/functional/example_test.py:18:1: F401 'test_framework.mininode.NODE_NETWORK' imported but unused
    ./test/functional/test_framework/messages.py:27:1: F401 'test_framework.util.wait_until' imported but unused
    ./test/functional/test_framework/test_framework.py:16:1: F401 'traceback' imported but unused
    
  2. MarcoFalke commented at 9:02 PM on December 5, 2017: member

    Cool. I hope this will bring down nit comments on functional test pulls.

    utACK 998cdc958f4e50efceb5f5c01f0002506a9d863c

  3. MarcoFalke added the label Tests on Dec 5, 2017
  4. in .travis.yml:24 in 998cdc958f outdated
      20 | @@ -21,7 +21,7 @@ env:
      21 |      - WINEDEBUG=fixme-all
      22 |    matrix:
      23 |  # ARM
      24 | -    - HOST=arm-linux-gnueabihf PACKAGES="g++-arm-linux-gnueabihf" DEP_OPTS="NO_QT=1" CHECK_DOC=1 GOAL="install" BITCOIN_CONFIG="--enable-glibc-back-compat --enable-reduce-exports"
      25 | +    - HOST=arm-linux-gnueabihf PACKAGES="g++-arm-linux-gnueabihf python-flake8" DEP_OPTS="NO_QT=1" CHECK_DOC=1 GOAL="install" BITCOIN_CONFIG="--enable-glibc-back-compat --enable-reduce-exports"
    


    MarcoFalke commented at 9:17 PM on December 5, 2017:

    Possibly this version is too old on travis to actually catch those?

    Could instead install python3-pip and then a few lines below, pip3 install flake8 --user

  5. practicalswift force-pushed on Dec 5, 2017
  6. practicalswift commented at 9:59 PM on December 5, 2017: contributor

    Travis now fails as expected:

    $ if [ "$CHECK_DOC" = 1 -a "$TRAVIS_EVENT_TYPE" = "pull_request" ]; then contrib/devtools/lint-all.sh; fi
    ./test/functional/example_test.py:18:1: F401 'test_framework.mininode.NODE_NETWORK' imported but unused
    ./test/functional/test_framework/test_framework.py:16:1: F401 'traceback' imported but unused
    ./test/functional/test_framework/messages.py:27:1: F401 'test_framework.util.wait_until' imported but unused
    
    ^---- failure generated from contrib/devtools/lint-python.sh
    
    The command "if [ "$CHECK_DOC" = 1 -a "$TRAVIS_EVENT_TYPE" = "pull_request" ]; then contrib/devtools/lint-all.sh; fi" failed and exited with 1 during .
    
    Your build has been stopped.
    

    I will now add a commit fixing those three unused imports to verify that Travis passes.

  7. practicalswift commented at 7:12 AM on December 6, 2017: contributor

    Now passing Travis and thus working as expected! :-)

  8. meshcollider commented at 11:37 PM on December 7, 2017: contributor

    Concept ACK, sounds useful

  9. MarcoFalke requested review from jnewbery on Dec 8, 2017
  10. in contrib/devtools/lint-python.sh:1 in a40ece02ba outdated
       0 | @@ -0,0 +1,4 @@
       1 | +#!/bin/sh
    


    jnewbery commented at 7:02 PM on December 8, 2017:

    Can you add a copyright notice and comment please? Just copy-paste from lint-whitespace.sh and change the comment to be something like:

    #
    # Copyright (c) 2017 The Bitcoin Core developers
    # Distributed under the MIT software license, see the accompanying
    # file COPYING or http://www.opensource.org/licenses/mit-license.php.
    #
    # Check for specified flake8 warnings in python files.
    
  11. jnewbery commented at 7:16 PM on December 8, 2017: member

    Thanks for tagging me @MarcoFalke . You know how much I love python linting :)

    This looks good to me. Example of this failing is here: https://travis-ci.org/bitcoin/bitcoin/jobs/312082939.

    I have one comment inline, and one general comment that it makes more sense to me to reverse the order of the commits (fix the warnings first and then add the checker).

    The only potential problem is if there are open pull requests which would silently conflict with this (ie PRs which introduce unused imports or change python modules without removing no-longer-required imports). @ajtowns was adding some checking in a different PR here: https://github.com/jnewbery/bitcoin/pull/4 and used a magic script to check open PRs for conflicts. Perhaps he could share that and we could do the same here?

    Longer term, I'd like to add more flake8 warnings to this, as long as we can all agree on which ones. Good ones could be:

    • W291 trailing whitespace
    • W191 indentation contains tabs
    • F821 undefined name
  12. MarcoFalke commented at 7:38 PM on December 8, 2017: member

    The only potential problem is if there are open pull requests which would silently conflict with this

    Good catch. However, instead of adding temporary code to allow for leeway, I'd rather just reset all arm_doc_jobs on travis. Otherwise we'd have to drag along the leeway code for an unspecific amount of time.

    • W291 trailing whitespace
    • W191 indentation contains tabs
    • F821 undefined name

    We already check for trailing whitespace and tab charactes in https://github.com/bitcoin/bitcoin/blob/4ef4dfebbc07d93d72899f60e01ca77a280c9122/contrib/devtools/lint-whitespace.sh#L36 https://github.com/bitcoin/bitcoin/blob/4ef4dfebbc07d93d72899f60e01ca77a280c9122/contrib/devtools/lint-whitespace.sh#L63

    Though, the check is only done on the diff of the pull request (ignoring preexisting lint). However, I don't see an issue fixing existing lint errors in python code and enabling those checks in this pull.

  13. jnewbery commented at 7:43 PM on December 8, 2017: member

    instead of adding temporary code to allow for leeway

    Sorry - I wasn't suggesting going that far - just running a check and then adding a note to those PRs and manually restarting the travis jobs.

    I don't see an issue fixing existing lint errors in python code and enabling those checks in this pull.

    I think it's better to keep this PR focused on adding the lint-python.sh infrastructure, since it's small and easy to review. Future PRs can add additional flake8 rules.

  14. MarcoFalke commented at 8:45 PM on December 8, 2017: member

    Agree, makes sense.

  15. Remove unused Python imports c7399e7082
  16. Add Travis check for unused Python imports d60b320740
  17. practicalswift force-pushed on Dec 10, 2017
  18. practicalswift commented at 10:51 AM on December 10, 2017: contributor

    @jnewbery Thanks for reviewing! Added a copyright header and changed the commit ordering. Looks good? :-)

  19. in contrib/devtools/lint-python.sh:10 in d60b320740
       5 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php.
       6 | +#
       7 | +# Check for specified flake8 warnings in python files.
       8 | +
       9 | +# F401: module imported but unused
      10 | +flake8 --ignore=B,C,E,F,I,N,W --select=F401 .
    


    jnewbery commented at 1:17 PM on December 10, 2017:

    Where did you get this list of error classes to ignore? I believe flake8 only uses:

    Where did the B, I and N classes come from?


    practicalswift commented at 1:28 PM on December 10, 2017:

    jnewbery commented at 2:05 PM on December 10, 2017:

    Thanks @practicalswift. I'll add those to my tool belt!

  20. jnewbery commented at 2:12 PM on December 10, 2017: member

    utACK d60b32074098d50b04e408c1304dd6f6120654ed

  21. ajtowns commented at 10:25 PM on December 10, 2017: member

    utACK d60b32074098d50b04e408c1304dd6f6120654ed (I've run the new lint script locally, but haven't verified the travis changes work)

    The PR checking I did for the naming convention patches mentioned above was just on filenames ("git diff --name-status $(git merge-base $PR master)..$PR"), checking to see what PRs would break this checks is a bit harder. The following looks okay:

    total=$(echo $PULLS | wc -w)
    sofar=0
    for a in $PULLS; do
       sofar=$((sofar + 1))
       echo >&2 "$a ($sofar/$total)"
       (
         (git checkout -q pull/origin/$a && flake8 --ignore=B,C,E,F,I,N,W --select=F401 --format '%(path)s:-:%(col)d: %(code)s %(text)s') | sed 'p'
         (git checkout -q $(git merge-base origin/master pull/origin/$a) && flake8 --ignore=B,C,E,F,I,N,W --select=F401 --format '%(path)s:-:%(col)d: %(code)s %(text)s')
       ) | sed "s/^/$a: /" | sort | uniq -c | sed -n 's/^ *2 //p'
    done | tee ../LOG

    (To fetch pull requests, you need a entry in .git/config like:

    [remote "origin"]
    url = https://github.com/bitcoin/bitcoin.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    fetch = +refs/pull/*/head:refs/pull/origin/*

    )

    Reformatting the output above suggests the following PRs introduce unreferenced imports:

    #11796: ./test/functional/test_framework/blocktools.py:

    F401 '.script.OP_EQUAL' imported but unused
    F401 '.script.OP_HASH160' imported but unused

    #11774: ./test/functional/test_framework/blocktools.py

    F401 '.script.OP_EQUAL' imported but unused
    F401 '.script.OP_HASH160' imported but unused

    #11771: ./test/functional/test_framework/mininode.py

    F401 'test_framework.util.assert_equal' imported but unused

    #11708: ./test/functional/signrawtransactions.py

    F401 'test_framework.script.OP_0' imported but unused

    #11666: ./test/functional/signinput.py

    F401 'time' imported but unused

    #11653: ./test/functional/getsignaturehash.py

    F401 'time' imported but unused

    #11647: ./test/functional/minchainwork.py

    F401 'test_framework.util.sync_blocks' imported but unused

    #10947: ./test/functional/segwit2.py

    F401 'io.BytesIO' imported but unused
    F401 'test_framework.address.key_to_p2pkh' imported but unused
    F401 'test_framework.address.script_to_p2sh' imported but unused
    F401 'test_framework.mininode.COIN' imported but unused
    F401 'test_framework.mininode.COutPoint' imported but unused
    F401 'test_framework.mininode.CTransaction' imported but unused
    F401 'test_framework.mininode.CTxIn' imported but unused
    F401 'test_framework.mininode.CTxOut' imported but unused
    F401 'test_framework.mininode.FromHex' imported but unused
    F401 'test_framework.mininode.sha256' imported but unused
    F401 'test_framework.mininode.ToHex' imported but unused
    F401 'test_framework.script.CScript' imported but unused
    F401 'test_framework.script.hash160' imported but unused
    F401 'test_framework.script.OP_0' imported but unused
    F401 'test_framework.script.OP_1' imported but unused
    F401 'test_framework.script.OP_2' imported but unused
    F401 'test_framework.script.OP_CHECKMULTISIG' imported but unused
    F401 'test_framework.script.OP_CHECKSIG' imported but unused
    F401 'test_framework.script.OP_DUP' imported but unused
    F401 'test_framework.script.OP_EQUAL' imported but unused
    F401 'test_framework.script.OP_EQUALVERIFY' imported but unused
    F401 'test_framework.script.OP_HASH160' imported but unused
    F401 'test_framework.script.OP_TRUE' imported but unused

    #10554: ./test/functional/zmq_test.py

    F401 'test_framework.util.assert_not_equal' imported but unused

    #10470: ./test/functional/listsinceblock.py

    F401 'test_framework.util.start_nodes' imported but unused
    F401 'test_framework.util.sync_mempools' imported but unused

    #10437:

    ./test/functional/bip135-grace.py
      F401 'tempfile' imported but unused
      F401 'test_framework.script.CScript' imported but unused
      F401 'test_framework.script.OP_1NEGATE' imported but unused
      F401 'test_framework.script.OP_CHECKSEQUENCEVERIFY' imported but unused
      F401 'test_framework.script.OP_DROP' imported but unused
      F401 'test_framework.util.sync_blocks' imported but unused
    ./test/functional/bip135-threshold.py
      F401 'tempfile' imported but unused
      F401 'test_framework.script.CScript' imported but unused
      F401 'test_framework.script.OP_1NEGATE' imported but unused
      F401 'test_framework.script.OP_CHECKSEQUENCEVERIFY' imported but unused
      F401 'test_framework.script.OP_DROP' imported but unused
      F401 'test_framework.util.sync_blocks' imported but unused

    #10366:

    ./test/functional/bip9-softforks.py
      F401 'test_framework.util.stop_nodes' imported but unused
    ./test/functional/mempool_persist.py
      F401 'test_framework.util.connect_nodes_bi' imported but unused
    ./test/functional/merkle_blocks.py
      F401 'test_framework.util.assert_raises' imported but unused
      F401 'test_framework.util.JSONRPCException' imported but unused
    ./test/functional/mining.py
      F401 'hashlib.sha256' imported but unused
      F401 'struct.pack' imported but unused
    ./test/functional/smartfees.py
      F401 'test_framework.util.assert_equal' imported but unused

    #9483: ./qa/rpc-tests/spv.py

    F401 'pprint.pprint' imported but unused

    #9152: ./test/functional/sweepprivkeys.py

    F401 'decimal' imported but unused
    F401 'math' imported but unused
    F401 'json' imported but unused

    #8755: ./qa/rpc-tests/sighashlimit.py

    F401 'test_framework.script.OP_1' imported but unused
  22. MarcoFalke merged this on Dec 10, 2017
  23. MarcoFalke closed this on Dec 10, 2017

  24. MarcoFalke referenced this in commit f60b4ad579 on Dec 10, 2017
  25. MarcoFalke commented at 11:36 PM on December 10, 2017: member

    Thanks for writing and running the script @ajtowns

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512
    
    utACK d60b32074098d50b04e408c1304dd6f6120654ed
    -----BEGIN PGP SIGNATURE-----
    
    iQIcBAEBCgAGBQJaLcS6AAoJENLqSFDnUoslH9gP/0PiY6125k0oO/yI2I8C8LmX
    VuQJY5JgdH0apA3mjWYoVLY0mC3vIa1HeOPPBUwXibEDBrO3rBd+OnzW2ZY6FvJ9
    KwViKukjtpK51Kt1AxJN2BBmr/W4M9nCUJ3EaQHdhmKPnrIPcKIqlkufqbpRYDFq
    QaA57ly/FHgQfuYEh4cvGZeO6k8RvQcGwnWx4jyQbj/bvYfZwbuHQZp78OChf3Eb
    VnspOmy7PGn9vTpa/4qqs1SM6s5/cZJ6JRF1UP2abS49/EP8fbAWFqpdFWCrBvh0
    qoLfhfGeSeQG2t+9LDUjoIMUhbt7rqzd5oPIwVNL8fxU5Wpa8aY/SHix0Gj3gEZA
    xt1OcsCMWLHhTPygxF6s7UgSqvoh2oGoic4PZyY7ffWA+SciBue7tsS4PlV7KLfN
    4lwDNw8rDmykmteDb9Q0g852WYahXRpRWBTswtRy1C+xe5KwusZZeSWpVF4RtkfK
    ao1MYY/1W/fO71y8fMtJFmNYnvwrwp680ZQ5TMCvtjP6VrSq3bGdEvvijR1j+UgA
    z51/mSAVupZsZX/MYKzn7hcwVHV/96ACZeiXL+r1CSDGcOPSZeUfwzHz3JKA7xu5
    bFsv8PnQseBdjpIxbxRLZKx4niWGOjk/lUKArbv45uEXzVeQ9TJKDJ5Pnkal92IB
    85anKK9+nHkEs0DVdfn+
    =Br1P
    -----END PGP SIGNATURE-----
    
  26. jnewbery commented at 12:48 AM on December 11, 2017: member

    Awesome. Thanks @ajtowns . @MarcoFalke - do you plan to comment on those PRs or would you like me to do it?

  27. MarcoFalke commented at 1:08 AM on December 11, 2017: member

    I checked that there are comments on all that are mergeable to master. The three ones by you and @ajtowns don't need a comment, I presume. (#11796, #11774, #11771)

  28. PastaPastaPasta referenced this in commit 6314e46af7 on Jun 13, 2020
  29. PastaPastaPasta referenced this in commit afcc8c0a55 on Jun 13, 2020
  30. PastaPastaPasta referenced this in commit a60ad08e38 on Jun 14, 2020
  31. PastaPastaPasta referenced this in commit 96a00f145a on Jun 17, 2020
  32. PastaPastaPasta referenced this in commit ded5132ac3 on Jun 17, 2020
  33. PastaPastaPasta referenced this in commit d76e35b6ae on Jun 17, 2020
  34. PastaPastaPasta referenced this in commit cd1fdb5b01 on Jun 17, 2020
  35. practicalswift deleted the branch on Apr 10, 2021
  36. gades referenced this in commit 5b8810639f on Mar 8, 2022
  37. DrahtBot locked this on Aug 18, 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: 2026-04-16 15:15 UTC

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