bazel-contrib / bazel-contrib/rules_mypy
Respect mypy.ini / pyproject.toml's `mypy.exclude` option
- Dominant language
- Starlark
- Stars
- 12
- Forks
- 13
- PR merge metrics
- No merged PRs in 30d
Description
mypy's [`--exclude` option does not apply when running against a single file or module](https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-exclude):
> If you pass a file or module explicitly it will still be checked. For instance, `mypy --exclude '/setup.py$' but_still_check/setup.py`
Target tags can be used to skip type checking, but those tags are much more cumbersome to maintain, especially when users have per-file py_library targets.
I propose adjusting the `mypy_runner.py` code to read the mypy config file and remove any `exclude`d items from `srcs`.
handywavy example:
```python
def run_mypy(
mypy_ini: Optional[str], cache_dir: str, srcs: list[str]
) -> tuple[str, str, int]:
maybe_config = ["--config-file", mypy_ini] if mypy_ini else []
# the new stuff
if mypy_ini:
excludes = extract_excludes_from_config(mypy_ini) # reads the ini/toml file.
srcs = remove_excludes_from_srcs(srcs) # handles the 'exclude' patterns regex matching
if len(srcs) == 0:
# Nothing to do
return "", "", 0
report, errors, status = mypy.api.run(
maybe_config
+ [
# do not check mtime in cache
"--skip-cache-mtime-checks",
# mypy defaults to incremental, but force it on anyway
"--incremental",
# use a known cache-dir
f"--cache-dir={cache_dir}",
# use current dir + MYPYPATH to resolve deps
"--explicit-package-bases",
# speedup
"--fast-module-lookup",
]
+ srcs
)
if status:
sys.stderr.write(errors)
sys.stderr.write(report)
return report, errors, status
```
This sort of thing will make migrating from standalone mypy to rules_mypy much easier.
Note: care must be taken when reading the config file and applying the regex, as [the toml value can take two forms](https://mypy.readthedocs.io/en/stable/config_file.html#confval-exclude). Ideally this parsing would be offloaded to mypy itself instead of trying to re-implement it here.
Contributor guide
Research direction
Start in mypy_runner.py and trace how the config file and srcs are passed to mypy.api.run. Check mypy's documented exclude formats for INI and TOML, then verify that matching sources are omitted, both config forms are handled, and an empty source list exits successfully without running mypy.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100