Skip to content

Typing improvements#274

Open
shsms wants to merge 6 commits intofrequenz-floss:v0.x.xfrom
shsms:typing-improvements
Open

Typing improvements#274
shsms wants to merge 6 commits intofrequenz-floss:v0.x.xfrom
shsms:typing-improvements

Conversation

@shsms
Copy link
Contributor

@shsms shsms commented Mar 1, 2026

Earlier there were a number of getattr that was hiding the actual types of values going into MetricSamples. This PR replaces all such instances, so that only strongly typed values go into MetricSamples.

Copilot AI review requested due to automatic review settings March 1, 2026 12:57
@shsms
Copy link
Contributor Author

shsms commented Mar 1, 2026

This leaks PB types to the users, but this PR is only improving the typing of what users receive, it doesn't change any behaviour. But now that we have types, we can talk about improving the design.

@github-actions github-actions bot added the part:docs Affects the documentation label Mar 1, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the type-safety of samples produced by the Reporting API client by replacing dynamic getattr-based access patterns with strongly-typed structures, so MetricSample instances reflect the real value types coming from protobuf telemetry/state data.

Changes:

  • Expand MetricSample typing (component ID and value union types) to reflect actual protobuf-derived data.
  • Refactor GenericDataBatch to use typed Protocols + generics, replacing attribute-name strings with typed fetcher callables.
  • Replace multiple getattr(...) usages in telemetry/state iteration with direct, typed attribute access.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +150 to +154
items = self.items_fetcher(self._data_pb)

for item in items:
cid = getattr(item, self.id_attr)
for sample in getattr(item, "metric_samples", []):
cid = self.id_fetcher(item)
for sample in item.metric_samples:
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenericDataBatch.__iter__() now relies on direct attribute access (item.metric_samples, item.state_snapshots, etc.). There are no unit tests covering iteration/unrolling behavior (only is_empty() is tested), so regressions here would be easy to miss. Consider adding a test that constructs a minimal batch (mock/proto) and asserts the yielded MetricSamples for both metric samples and state snapshots.

Copilot uses AI. Check for mistakes.
@shsms shsms force-pushed the typing-improvements branch from 14a95e7 to 19169dd Compare March 1, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants