Skip to content

Conversation

@josibake
Copy link
Member

This relates to #19389 by enabling mypy to run without --ignore-imports. This is the minimum amount of work needed to get mypy running in the linter, after which annotations can be added to scripts in contrib/ and test_framework/ to get the full benefit of running mypy in the linter.

special notes

This also involved updating PyZMQ per #19389 (comment) .

how to test

after making these changes, I ran ./test/lint/lint-python.sh and verified no errors.

To be sure mypy would actually catch type errors when they existed, I added the following lines to contrib/devtools/symbol-checker.py:

def check_imported_symbols(filename: str) -> bool:
    test = filename + 1
    ...

First, filename needed to be annotated as str, or else mypy treats it as Any and Any +1 is allowed. After annotating filename: str and adding an illegal operation, running ./test/lint/lint-python.sh` returned the following error:

contrib/devtools/symbol-check.py:170:5: F841 local variable 'test' is assigned to but never used
contrib/devtools/symbol-check.py:170: error: Unsupported operand types for + ("str" and "int")  [operator]

next steps

Where we do have mypy annotations, they are often incomplete. This causes mypy to evaluate arguments as Any (see example above with symbol-checker.py). I propose a follow-up PR adding annotations. At a minimum, annotations should be added for everything in test_framework/, which can be done fairly easily with monkeytype. The benefit of this is, now that we are running without --ignore-imports, mypy can detect errors in the functional tests even if they don't have annotations, so long as all of the files in test_framework/ are fully annotated.

@fanquake
Copy link
Member

Pretty sure need to install pyzmq into the container where the linter is running:

test/functional/test_framework/test_framework.py:805: error: Cannot find implementation or library stub for module named 'zmq'  [import]
test/functional/interface_zmq.py:32: error: Cannot find implementation or library stub for module named 'zmq'  [import]
test/functional/interface_zmq.py:32: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 2 errors in 2 files (checked 214 source files)
^---- failure generated from test/lint/lint-python.sh

@josibake
Copy link
Member Author

Pretty sure need to install pyzmq into the container where the linter is running:

ah yes, good catch!

mypy stubs were introduced in 21.0.1
install 22.2.1 (latest as of this commit)
ignore lief package
remove --ignore-imports flag from mympy run
make data/ importable
@josibake josibake force-pushed the josibake-fix-mypy-import-errors branch from 4926236 to 47995dd Compare August 31, 2021 13:12
@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 1, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #23148 (build: Fix guix linker-loader path and add check_ELF_interpreter by dongcarl)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@practicalswift
Copy link
Contributor

Concept ACK

@fanquake
Copy link
Member

Can you change the commit message for 065c64a to be something like: ci: install pyzmq into lint container. As you're not actually bumping the version, you're adding it to the container for the first time.

Can you also add a commit bumping mypy to the latest version. Looks like it's 0.910.

In either commit can you add / update the dependency here as well: https://github.com/bitcoin/bitcoin/tree/master/test#lint-tests.

@laanwj
Copy link
Member

laanwj commented Oct 1, 2021

Concept ACK, less exceptions in mypy allows for more thorough checking.

fi

if ! mypy --ignore-missing-imports --show-error-codes $(git ls-files "test/functional/*.py" "contrib/devtools/*.py"); then
if ! mypy --show-error-codes $(git ls-files "test/functional/*.py" "contrib/devtools/*.py"); then
Copy link
Member

Choose a reason for hiding this comment

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

Something I've wondered about in the past is whether checking these in the same mypy invocation is a good idea. I think it will consider all the files part of the same top-level module, which might cause inconsistencies of different directories have same-named modules, for example.
That said, it works.

@fanquake
Copy link
Member

fanquake commented Oct 7, 2021

I've fixed this up / taken it over in #23212.

@fanquake fanquake closed this Oct 7, 2021
maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Oct 17, 2021
a46f71b lint: enable mypy checking for missing imports (josibake)
22e6526 lint mypy 0.910 (fanquake)
6ae9c2e lint: install pyzmq (22.3.0) into linter environment (josibake)
b93e229 doc: remove pointlessly duplicated linter version / install info (fanquake)

Pull request description:

  This is bitcoin#22844 with issues addressed, and two additional commits. One to move to the latest mypy version in the CI, and another to remove what is just pointless duplication from the test README. There's no need to relist package versions and install instructions (meaning 2x the work when changing anything), when you can just link to `04_install.sh` which has the versions used and pip invocations to install them.

ACKs for top commit:
  practicalswift:
    cr ACK a46f71b

Tree-SHA512: 2900dea3901d03a846dc1ea912f217d152e803845516c7d941745ec1291d145590cd4bf2ddc497f7cf628119ba9905d7b1531836062aa85b384e39cf436f62c6
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 17, 2021
a46f71b lint: enable mypy checking for missing imports (josibake)
22e6526 lint mypy 0.910 (fanquake)
6ae9c2e lint: install pyzmq (22.3.0) into linter environment (josibake)
b93e229 doc: remove pointlessly duplicated linter version / install info (fanquake)

Pull request description:

  This is bitcoin#22844 with issues addressed, and two additional commits. One to move to the latest mypy version in the CI, and another to remove what is just pointless duplication from the test README. There's no need to relist package versions and install instructions (meaning 2x the work when changing anything), when you can just link to `04_install.sh` which has the versions used and pip invocations to install them.

ACKs for top commit:
  practicalswift:
    cr ACK a46f71b

Tree-SHA512: 2900dea3901d03a846dc1ea912f217d152e803845516c7d941745ec1291d145590cd4bf2ddc497f7cf628119ba9905d7b1531836062aa85b384e39cf436f62c6
@bitcoin bitcoin locked and limited conversation to collaborators Oct 30, 2022
@josibake josibake deleted the josibake-fix-mypy-import-errors branch January 26, 2024 10:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants