[PATCH v1 3/3] perf jevents metric: Add python type annotations

From: Ian Rogers

Date: Mon Jul 06 2026 - 23:42:27 EST


Make mypy clean.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/pmu-events/metric.py | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/tools/perf/pmu-events/metric.py b/tools/perf/pmu-events/metric.py
index 11c7162825f4..ce025675898c 100644
--- a/tools/perf/pmu-events/metric.py
+++ b/tools/perf/pmu-events/metric.py
@@ -276,7 +276,7 @@ class Operator(Expression):
lhs = self.lhs.Simplify()
rhs = self.rhs.Simplify()
if isinstance(lhs, Constant) and isinstance(rhs, Constant):
- return Constant(ast.literal_eval(lhs + self.operator + rhs))
+ return Constant(ast.literal_eval(lhs.value + self.operator + rhs.value))

if isinstance(self.lhs, Constant):
if self.operator in ('+', '|') and lhs.value == '0':
@@ -298,7 +298,7 @@ class Operator(Expression):
if self.operator == '*' and rhs.value == '0':
return Constant(0)

- if self.operator == '*' and self.rhs.value == '1':
+ if self.operator == '*' and rhs.value == '1':
return lhs

return Operator(self.operator, lhs, rhs)
@@ -316,9 +316,7 @@ class Operator(Expression):
if self.Equals(expression):
return Event(name)
lhs = self.lhs.Substitute(name, expression)
- rhs = None
- if self.rhs:
- rhs = self.rhs.Substitute(name, expression)
+ rhs = self.rhs.Substitute(name, expression)
return Operator(self.operator, lhs, rhs)


@@ -382,7 +380,9 @@ class Function(Expression):
rhs: Optional[Union[int, float, Expression]] = None):
self.fn = fn
self.lhs = _Constify(lhs)
- self.rhs = _Constify(rhs)
+ self.rhs = None
+ if rhs is not None:
+ self.rhs = _Constify(rhs)

def ToPerfJson(self):
if self.rhs:
@@ -407,7 +407,8 @@ class Function(Expression):
return Function(self.fn, lhs, rhs)

def HasExperimentalEvents(self) -> bool:
- return self.lhs.HasExperimentalEvents() or (self.rhs and self.rhs.HasExperimentalEvents())
+ return (self.lhs.HasExperimentalEvents() or
+ (self.rhs is not None and self.rhs.HasExperimentalEvents()))

def Equals(self, other: Expression) -> bool:
if isinstance(other, Function):
@@ -683,7 +684,7 @@ class MetricGroup:

def Flatten(self) -> Set[Metric]:
"""Returns a set of all leaf metrics."""
- result = set()
+ result: Set[Metric] = set()
for x in self.metric_list:
result = result.union(x.Flatten())

--
2.55.0.795.g602f6c329a-goog