fix(utils): protobuf-compatible repeated field detection#1141
Conversation
Add _is_field_repeated helper that prefers field.is_repeated and falls back to field.label on older protobuf runtimes. Fixes crashes when newer protobuf builds remove label while the SDK still relied on it. Fixes a2aproject#1011
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/utils/proto_utils.py | 90.87% | 91.08% | 🟢 +0.21% |
| Total | 92.97% | 92.98% | ⚪️ 0.00% |
Generated by coverage-comment.yml
There was a problem hiding this comment.
Code Review
This pull request introduces a helper function _is_field_repeated to safely check if a protobuf field is repeated across different protobuf versions, replacing deprecated field.label checks. It also adds unit tests for this helper. The review feedback points out that monkeypatching FieldDescriptor in test_fallback_when_is_repeated_missing will fail in environments using the fast C++ implementation of Protobuf because C-extension types are read-only. A code suggestion is provided to use a simple mock object instead.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| def test_fallback_when_is_repeated_missing(self, monkeypatch): | ||
| field = AgentSkill.DESCRIPTOR.fields_by_name['tags'] | ||
|
|
||
| def raise_attribute_error(_self): | ||
| raise AttributeError('is_repeated') | ||
|
|
||
| monkeypatch.setattr( | ||
| type(field), | ||
| 'is_repeated', | ||
| property(raise_attribute_error), | ||
| ) | ||
| assert proto_utils._is_field_repeated(field) is True |
There was a problem hiding this comment.
Modifying the attributes or properties of FieldDescriptor (or its type) using monkeypatch will fail with a TypeError in environments where the fast C++ implementation of Protobuf is used (which is the default in many environments, as FieldDescriptor is a C-extension type and its attributes/type are read-only).
Instead of monkeypatching the built-in FieldDescriptor class, you can use a simple mock object or a custom class that lacks the is_repeated attribute to safely test the fallback path.
| def test_fallback_when_is_repeated_missing(self, monkeypatch): | |
| field = AgentSkill.DESCRIPTOR.fields_by_name['tags'] | |
| def raise_attribute_error(_self): | |
| raise AttributeError('is_repeated') | |
| monkeypatch.setattr( | |
| type(field), | |
| 'is_repeated', | |
| property(raise_attribute_error), | |
| ) | |
| assert proto_utils._is_field_repeated(field) is True | |
| def test_fallback_when_is_repeated_missing(self): | |
| class MockField: | |
| label = proto_utils.FieldDescriptor.LABEL_REPEATED | |
| field = MockField() | |
| assert proto_utils._is_field_repeated(field) is True |
References
- When checking for repeated fields in Protobuf, access descriptor constants via the
FieldDescriptorclass (e.g.,FieldDescriptor.LABEL_REPEATED) rather than directly from the field instance (e.g.,field.is_repeated).
Summary
_is_field_repeated()inproto_utilsthat prefersfield.is_repeatedand falls back tofield.label == FieldDescriptor.LABEL_REPEATEDwhen the newer API is unavailable.Problem
proto_utilsused the deprecatedFieldDescriptor.labelAPI. Protobuf 7 removeslabel, which breaks validation on newer runtimes even though the SDK dependency range can resolve protobuf 7.x in some environments.Test plan
uv run pytest tests/utils/test_proto_utils.py -q_is_field_repeatedagainst protobuf 6.x locally (is_repeated + label both present)Fixes #1011