bazel-contrib / bazel-contrib/rules_mypy

Mypy doesn't find modules in external non-Pip repositories (e.g. Abseil)

Open
#112 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Starlark
Stars
12
Forks
13
PR merge metrics
No merged PRs in 30d

Description

Example workspace setup:

MODULE.bazel:

```starlark
bazel_dep(name = "rules_python", version = "1.5.3")
bazel_dep(name = "rules_mypy", version = "0.40.0")
bazel_dep(name = "abseil-py", version = "2.1.0")
```

BUILD.bazel:

```starlark
load("@rules_python//python:py_binary.bzl", "py_binary")

py_binary(
name = "bin",
srcs = ["bin.py"],
deps = ["@abseil-py//absl:app"],
)
```

mypy.bzl:

```starlark
load("@rules_mypy//mypy:mypy.bzl", _mypy = "mypy")

mypy = _mypy()
```

bin.py:

```python
from absl import app
def main(argv):
print('hello world')
if __name__ == '__main__':
app.run(main)
```

The Mypy aspect fails:

```
$ bazel build --aspects=//:mypy.bzl%mypy --output_groups=+mypy --verbose_failures //:bin
[...]
ERROR: [...]/BUILD.bazel:3:10: mypy //:bin failed: (Exit 1): mypy failed: error executing mypy command (from target //:bin)
(cd [...]/execroot/_main && \
exec env - \
MYPYPATH=.:external/abseil-py+/site-packages \
MYPY_FORCE_COLOR=1 \
PATH=/bin:/usr/bin:/usr/local/bin \
PYTHONNOUSERSITE=true \
TERM=xterm-256color \
bazel-out/darwin_arm64-opt-exec-ST-47de9cc097ad/bin/external/rules_mypy+/mypy/private/mypy --output bazel-out/darwin_arm64-fastbuild/bin/bin.mypy_stdout --cache-dir bazel-out/darwin_arm64-fastbuild/bin/bin.mypy_cache bin.py --mypy-ini external/rules_mypy+/mypy/private/default_mypy.ini)
[...]
bin.py:1: error: Cannot find implementation or library stub for module named
"absl" [import-not-found]
from absl import app
^
bin.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
[...]
ERROR: Build did NOT complete successfully
```

Note that MYPYPATH contains external/abseil-py+/site-packages, but the sources (within the execroot) are in external/abseil-py+ (without site-packages). I guess the site-packages directory is only correct for Pip dependencies, and the logic in https://github.com/theoremlp/rules_mypy/blob/v0.40.0/mypy/private/mypy.bzl#L137 should also add the external repository root directory itself to the search path.

Contributor guide

Open the contributing guide

Research direction

Start in mypy/private/mypy.bzl around line 137 and compare how MYPYPATH is assembled for Pip and non-Pip external repositories. Reproduce the example with MODULE.bazel, BUILD.bazel, mypy.bzl, and bin.py using the shown Bazel command. Done means the external absl import is found and the mypy aspect completes successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.