From 59355235f29e02eb4d88aa2d0c0d68aca901c929 Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Mon, 6 Jul 2026 20:40:19 -0700 Subject: [PATCH] perf jevents metric: Add python type annotations Make mypy clean. Signed-off-by: Ian Rogers Signed-off-by: Namhyung Kim --- 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())