Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: theoremlp/rules_mypy
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.3.1
Choose a base ref
...
head repository: theoremlp/rules_mypy
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.4.0
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Aug 9, 2024

  1. Use mypy's hard exit for perf (#13)

    Use mypy's hard_exit function to exit the process without freeing objects as a perf optimization.
    mark-thm authored Aug 9, 2024
    Copy the full SHA
    f29040c View commit details
  2. Make suppression tags configurable (#14)

    In our use-case, we want to suppress running mypy using either the tag `no-mypy` or the tag `no-lint`. Before we open-sourced this ruleset, we used an "eligibility function", but almost always configured that using rule kind and tags, so we're opting here for the narrower (and simpler to perform) customization.
    mark-thm authored Aug 9, 2024
    Copy the full SHA
    4e2c620 View commit details
Showing with 15 additions and 5 deletions.
  1. +3 −0 .vscode/settings.json
  2. +8 −4 mypy/private/mypy.bzl
  3. +4 −1 mypy/private/mypy.py
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.analysis.typeCheckingMode": "basic"
}
12 changes: 8 additions & 4 deletions mypy/private/mypy.bzl
Original file line number Diff line number Diff line change
@@ -27,9 +27,10 @@ def _mypy_impl(target, ctx):
if ctx.rule.kind not in ["py_binary", "py_library", "py_test"]:
return []

# disable if a target is tagged "no-mypy"
if "no-mypy" in ctx.rule.attr.tags:
return []
# disable if a target is tagged with at least one suppression tag
for tag in ctx.attr._suppression_tags:
if tag in ctx.rule.attr.tags:
return []

# we need to help mypy map the location of external deps by setting
# MYPYPATH to include the site-packages directories.
@@ -120,7 +121,7 @@ def _mypy_impl(target, ctx):
OutputGroupInfo(mypy = depset([output_file])),
]

def mypy(mypy_cli = None, mypy_ini = None, types = None, cache = True):
def mypy(mypy_cli = None, mypy_ini = None, types = None, cache = True, suppression_tags = None):
"""
Create a mypy target inferring upstream caches from deps.
@@ -138,6 +139,8 @@ def mypy(mypy_cli = None, mypy_ini = None, types = None, cache = True):
Use the types extension to create this map for a requirements.in
or requirements.txt file.
cache: (optional, default True) propagate the mypy cache
suppression_tags: (optional, default ["no-mypy"]) tags that suppress running
mypy on a particular target.
Returns:
a mypy aspect.
@@ -166,6 +169,7 @@ def mypy(mypy_cli = None, mypy_ini = None, types = None, cache = True):
# this kind of attr to pass naturally
"_types_keys": attr.label_list(default = types.keys()),
"_types_values": attr.label_list(default = types.values()),
"_suppression_tags": attr.string_list(default = suppression_tags or ["no-mypy"]),
"cache": attr.bool(default = cache),
} | additional_attrs,
)
5 changes: 4 additions & 1 deletion mypy/private/mypy.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import click

import mypy.api
import mypy.util


def _merge_upstream_caches(cache_dir: str, upstream_caches: list[str]) -> None:
@@ -86,7 +87,9 @@ def main(
file.write(errors)
file.write(report)

sys.exit(status)
# use mypy's hard_exit to exit without freeing objects, it can be meaningfully
# faster than an orderly shutdown
mypy.util.hard_exit(status)


if __name__ == "__main__":