[PATCH v1 13/49] perf build: Make mypy build test opt-out (NO_MYPY=1)

From: Ian Rogers

Date: Sun Sep 20 2026 - 01:27:32 EST


Commit 168910d0f937 ("perf build: Add mypy build tests") added mypy as an
opt-in compile-time check (MYPY=1) because of existing mypy errors across
perf Python scripts.

Now that all Python scripts in tools/perf have been cleaned up and pass
mypy without errors, make the mypy build test opt-out (default on when
mypy is installed, disabled with NO_MYPY=1), matching NO_SHELLCHECK=1.
Set MYPYPATH to tools/perf/python so local imports resolve during type
checking, and clean *.mypy_log files in 'make clean'.

Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/Build | 2 +-
tools/perf/Makefile.perf | 20 +++++++++++++++++---
tools/perf/pmu-events/Build | 2 +-
tools/perf/tests/Build | 2 +-
tools/perf/util/Build | 2 +-
5 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/tools/perf/Build b/tools/perf/Build
index e18c80a5c1bc..37d603faea6d 100644
--- a/tools/perf/Build
+++ b/tools/perf/Build
@@ -88,7 +88,7 @@ endif

$(OUTPUT)%.mypy_log: %
$(call rule_mkdir)
- $(Q)$(call echo-cmd,test)mypy "$<" > $@ || (cat $@ && rm $@ && false)
+ $(Q)$(call echo-cmd,test)$(MYPY) "$<" > $@ || (cat $@ && rm $@ && false)

perf-y += $(MYPY_TEST_LOGS)

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 2438b40eaaec..f22eda3d2852 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -129,6 +129,8 @@ include ../scripts/utilities.mak
# Define GEN_VMLINUX_H to generate vmlinux.h from the BTF.
#
# Define NO_SHELLCHECK if you do not want to run shellcheck during build
+#
+# Define NO_MYPY if you do not want to run mypy during build

# As per kernel Makefile, avoid funny character set dependencies
unexport LC_ALL
@@ -263,8 +265,19 @@ ifneq ($(SHELLCHECK),)
endif

# Runs mypy on perf python files
-ifeq ($(MYPY),1)
- MYPY := $(shell which mypy 2> /dev/null)
+MAKEOVERRIDES := $(filter-out MYPY=%,$(MAKEOVERRIDES))
+ifeq ($(NO_MYPY),1)
+ override MYPY :=
+else
+ ifeq ($(MYPY),1)
+ override MYPY := $(shell which mypy 2> /dev/null)
+ else
+ MYPY ?= $(shell which mypy 2> /dev/null)
+ endif
+endif
+
+ifneq ($(MYPY),)
+ override MYPY := MYPYPATH=$(srctree)/tools/perf/python $(MYPY)
endif

# Runs pylint on perf python files
@@ -948,7 +961,8 @@ clean:: $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBSYMBOL)-clean $(
$(call QUIET_CLEAN, core-objs) $(RM) $(LIBPERF_A) $(OUTPUT)perf-archive \
$(OUTPUT)perf-iostat $(LANG_BINDINGS)
$(Q)find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '*.a' -delete -o \
- -name '\.*.cmd' -delete -o -name '\.*.d' -delete -o -name '*.shellcheck_log' -delete
+ -name '\.*.cmd' -delete -o -name '\.*.d' -delete -o -name '*.shellcheck_log' -delete -o \
+ -name '*.mypy_log' -delete
$(Q)$(RM) $(OUTPUT).config-detected
$(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso32 \
perf-read-vdsox32 $(OUTPUT)$(LIBJVMTI).so
diff --git a/tools/perf/pmu-events/Build b/tools/perf/pmu-events/Build
index 372773b998e4..c3542e5ae46f 100644
--- a/tools/perf/pmu-events/Build
+++ b/tools/perf/pmu-events/Build
@@ -187,7 +187,7 @@ JEVENTS_DEPS := $(OUT_JSON) $(GEN_JSON) $(JEVENTS_PY) $(METRIC_PY) $(EMPTY_PMU_E
ifdef MYPY
define MYPY_RULE
$(2): $(1)
- $$(Q)$$(call echo-cmd,test)mypy $(1) > $(2) || (cat $(2) && rm $(2) && false)
+ $$(Q)$$(call echo-cmd,test)$$(MYPY) $(1) > $(2) || (cat $(2) && rm $(2) && false)
endef
$(foreach src,$(wildcard pmu-events/*.py), \
$(eval dest := $(patsubst pmu-events/%,$(OUTPUT)pmu-events/%.mypy_log,$(src))) \
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 406e48eed1c8..98b05b68fc14 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -104,7 +104,7 @@ endif

$(OUTPUT)%.mypy_log: %
$(call rule_mkdir)
- $(Q)$(call echo-cmd,test)mypy "$<" > $@ || (cat $@ && rm $@ && false)
+ $(Q)$(call echo-cmd,test)$(MYPY) "$<" > $@ || (cat $@ && rm $@ && false)

perf-test-y += $(MYPY_TEST_LOGS)

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index d08f2af7d970..71d1f7289d03 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -426,7 +426,7 @@ endif

$(OUTPUT)%.mypy_log: %
$(call rule_mkdir)
- $(Q)$(call echo-cmd,test)mypy "$<" > $@ || (cat $@ && rm $@ && false)
+ $(Q)$(call echo-cmd,test)$(MYPY) "$<" > $@ || (cat $@ && rm $@ && false)

perf-util-y += $(MYPY_TEST_LOGS)

--
2.55.0.1082.g2b9226bbc0-goog