lint: Convert Python linter to Python #24794

pull fjahr wants to merge 1 commits into bitcoin:master from fjahr:202204-lint-py-py changing 3 files +134 −114
  1. fjahr commented at 11:56 pm on April 6, 2022: member

    The outputs provided by the Python version should be exactly the same as the ones from the shell version.

    There is small improvement here: Previously only the dependency of flake9 was checked, now all dependencies are checked before running.

    I also tried to mostly follow the recommendations here but happy to make more changes if there is still room for improvement.

  2. DrahtBot added the label Docs on Apr 7, 2022
  3. DrahtBot added the label Tests on Apr 7, 2022
  4. MarcoFalke removed the label Docs on Apr 7, 2022
  5. jonatack commented at 7:20 am on April 7, 2022: member
    Concept/approach ACK
  6. in test/lint/lint-python.py:94 in 65e36e1263 outdated
    89+    'W606'   # 'async' and 'await' are reserved keywords starting with Python 3.7
    90+)
    91+
    92+
    93+def check_dependencies():
    94+    list_out = subprocess.check_output(['pip3', 'list'], stderr=subprocess.DEVNULL).decode('utf-8')
    


    laanwj commented at 8:10 am on April 7, 2022:

    No need to call out to pip here, Python provides introspection for this, but I think even this would work:

    0for dep in DEPS:
    1    try:
    2        __import__(dep)
    3    except ImportError:
    4        print(f"Skipping Python linting since {dep} is not installed.")
    5        exit(0)
    

    fjahr commented at 11:51 am on April 17, 2022:
    Yepp, I found that the canonical way seems to be using pkg_resources. I implemented it using that.
  7. laanwj commented at 8:13 am on April 7, 2022: member
    I think it’s slightly weird that pyzmq is required for linting. I understand why this is though, avoiding that would require a stub library and isn’t in scope here.
  8. in test/lint/lint-python.py:17 in 65e36e1263 outdated
    11+import os
    12+import subprocess
    13+import sys
    14+
    15+DEPS = ['flake8', 'mypy', 'pyzmq']
    16+MYPY_CACHE_DIR = f"{os.getenv('BASE_ROOT_DIR', '')}/test/.mypy_cache"
    


    MarcoFalke commented at 8:14 am on April 7, 2022:
    Unrelated: If run outside the CI runner, this will likely result in /test/.mypy_cache, which may not be wanted?

    fjahr commented at 11:51 am on April 17, 2022:
    Yes, I noticed this as well but it is the existing behavior. I used getenv with the empty fallback because I wanted to potentially change it to a better one but forgot to think about it. What would you suggest as a better fallback?
  9. in test/lint/lint-python.py:89 in 65e36e1263 outdated
    84+    'W601,'  # .has_key() is deprecated, use "in"
    85+    'W602,'  # deprecated form of raising exception
    86+    'W603,'  # "<>" is deprecated, use "!="
    87+    'W604,'  # backticks are deprecated, use "repr()"
    88+    'W605,'  # invalid escape sequence "x"
    89+    'W606'   # 'async' and 'await' are reserved keywords starting with Python 3.7
    


    MarcoFalke commented at 8:14 am on April 7, 2022:
    0    'W606',   # 'async' and 'await' are reserved keywords starting with Python 3.7
    

    #24766 (review)


    fjahr commented at 11:51 am on April 17, 2022:
    Done
  10. in test/lint/lint-python.py:127 in 65e36e1263 outdated
    125+
    126+    try:
    127+        mypy_out = subprocess.check_output(mypy_args, stderr=subprocess.STDOUT)
    128+    except subprocess.CalledProcessError as e:
    129+        print(e.output.decode("utf-8"), end="")
    130+        exit(1)
    


    MarcoFalke commented at 8:17 am on April 7, 2022:
    0        subprocess.check_call(mypy_args)
    1    except subprocess.CalledProcessError:
    2        exit(1)
    

    How is this different from the shorter alternative?

    See:


    fjahr commented at 11:51 am on April 17, 2022:
    Hmm, I tried this initially with check_call but in my testing I didn’t seem to get the errors to my console. But I guess I did something wrong because with your code it seems to work now. Changed.
  11. MarcoFalke approved
  12. MarcoFalke commented at 8:18 am on April 7, 2022: member
    LGTM
  13. fanquake commented at 11:58 am on April 7, 2022: member
    I was half hoping we could just call the flake8 api directly, and check our files, rather than just moving to wrapping subprocess invocations of flake8 in python instead of bash. Maybe something for the future.
  14. laanwj commented at 11:45 am on April 8, 2022: member

    I was half hoping we could just call the flake8 api directly, and check our files, rather than just moving to wrapping subprocess invocations of flake8 in python instead of bash. Maybe something for the future.

    This sounds neat for a follow-up, but personally at least I think as a first step calling out to tools like flake8 is fine. The biggest issue with subprocess is shell ambiguity (so avoid shell=True), and basic system commands that may differ.

  15. fjahr force-pushed on Apr 17, 2022
  16. fjahr commented at 11:55 am on April 17, 2022: member

    I think it’s slightly weird that pyzmq is required for linting. I understand why this is though, avoiding that would require a stub library and isn’t in scope here.

    I am slightly unsure if you would like me to make a change or not, but I think not. Otherwise, let me know :)

    I was thinking about this quite a bit when initially putting it on the list but after seeing the errors produced when pyzmq is missing I thought it would be better to make this explicit because otherwise it might lead to people wasting time debugging in the wrong direction.

  17. fjahr commented at 11:55 am on April 17, 2022: member
    Addressed review comments.
  18. in test/lint/lint-python.py:122 in 28e7af46b1 outdated
    117+        flake8_out = subprocess.run(flake8_args, stdout=subprocess.PIPE, env=flake8_env)
    118+    except subprocess.CalledProcessError as e:
    119+        print(e.output.decode("utf-8"), end="")
    120+        exit(1)
    121+
    122+    print(flake8_out.stdout.decode("utf-8"), end="")
    


    MarcoFalke commented at 12:48 pm on April 17, 2022:

    how is this different from

    0    try:
    1        subprocess.check_call(flake8_args, env=flake8_env)
    2    except subprocess.CalledProcessError:
    3        exit(1)
    

    #24794 (review)


    fjahr commented at 10:56 pm on April 17, 2022:
    Looking at the docs I got the impression that only run allowed to pass in a env argument but it seems it works and is just not explicitly named for check_call. So I changed this.
  19. lint: Convert Python linter to Python 47b66ac4ac
  20. fjahr force-pushed on Apr 17, 2022
  21. laanwj commented at 4:50 pm on April 18, 2022: member
    Tested ACK 47b66ac4acd148da9fd0d894574f8bb3b3b33368
  22. laanwj merged this on Apr 18, 2022
  23. laanwj closed this on Apr 18, 2022

  24. MarcoFalke referenced this in commit 013daed9ac on Apr 19, 2022
  25. sidhujag referenced this in commit 04f1c86a88 on Apr 19, 2022
  26. DrahtBot locked this on Apr 18, 2023

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-17 06:12 UTC

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