Context
The v0.4.x cycle landed the delivery-area invariants work as a backwards-compatible set of soft shims so v0.4.0 callers keep working unchanged. Specifically:
DeliveryArea.code stayed str | None and DeliveryArea.__post_init__ only emits a DeprecationWarning (rather than raising ValueError) when the 2-character-code / non-UNSPECIFIED-code_type invariants are violated.
delivery_area_from_proto() was kept and marked @deprecated, redirecting to the new delivery_area_from_proto2().
delivery_area_from_proto() internally wraps its DeliveryArea(...) construction in warnings.catch_warnings() so callers of the deprecated entry point see exactly one DeprecationWarning (the outer one, not the inner __post_init__ one).
This issue tracks the v0.5.0 clean-up: remove the shims and land the strict shape originally planned.
Scope: what to remove in v0.5.0
1. Delete the deprecated proto converter
- File:
src/frequenz/client/common/grid/proto/v1alpha8/_delivery_area.py
- Delete the
@deprecated delivery_area_from_proto() function entirely.
- Drop the
typing_extensions.deprecated import if no other symbol in the file uses it.
- Drop the
import logging, _logger = ... and import warnings if they become unused after the deletion.
2. Rename delivery_area_from_proto2 → delivery_area_from_proto
-
Same file: src/frequenz/client/common/grid/proto/v1alpha8/_delivery_area.py
-
Rename the function to reclaim the natural name.
-
Optional cleanup: because DeliveryArea will raise ValueError in step 4, the new implementation can drop the manual len(code) != 2 or raw_code_type == 0 pre-check and just use the try/except ValueError pattern originally planned, e.g.:
try:
return DeliveryArea(code=message.code, code_type=code_type)
except ValueError:
return InvalidDeliveryArea(code=message.code, code_type=code_type)
-
Update src/frequenz/client/common/grid/proto/v1alpha8/__init__.py:
remove delivery_area_from_proto2 from imports / __all__, keep
delivery_area_from_proto.
-
Update src/frequenz/client/common/microgrid/proto/v1alpha8/_microgrid.py
to call delivery_area_from_proto again (no 2 suffix).
3. Tighten DeliveryArea.code: str | None → str
- File:
src/frequenz/client/common/grid/_delivery_area.py
BaseDeliveryArea.code becomes str (drop the | None). Both concrete leaves inherit the tightened type.
- Drop the
self.code is None or ... branch inside DeliveryArea.__post_init__ — len(self.code) != 2 alone covers empty
and short/long codes.
- Update
BaseDeliveryArea.__str__ if the self.code or "<NO CODE>" fallback is now purely for the empty-string case (still true; keep it).
4. Convert DeliveryArea.__post_init__ from warn → raise
- File:
src/frequenz/client/common/grid/_delivery_area.py
- Replace the
warnings.warn(..., DeprecationWarning, stacklevel=3) call with raise ValueError("DeliveryArea invariants violated: " + "; ".join(issues)).
- Update the docstring: remove the "currently emits a
DeprecationWarning; will raise in a future release" language and
document the hard ValueError.
5. Simplify inner suppression sites
- The
warnings.catch_warnings() block around DeliveryArea(code=..., code_type=...) inside the deleted delivery_area_from_proto() goes away with step 1 (nothing else needs
it).
- Audit
microgrid_from_proto and any other internal call site that hand-constructs a DeliveryArea from wire data — they should now go through delivery_area_from_proto (post-rename) and catch its ValueError if they need to distinguish invalid-vs-valid outside the return type.
6. Tests
-
Delete test_from_proto and test_get_code_type_from_proto_unspecified_raises and test_from_proto_emits_deprecation_warning in tests/grid/proto/v1alpha8/test_delivery_area.py (they exercise the
deleted deprecated function).
-
Rename the test_from_proto2* tests to test_from_proto* (drop the 2) and drop pytest.deprecated_call(...) wrappers.
-
In tests/grid/test_delivery_area.py:
- Rename
test_creation_invalid_emits_deprecation_warning (and its _member sibling) to test_creation_invalid_raises and switch from pytest.warns(DeprecationWarning, ...) to pytest.raises(ValueError, ...).
- Rewrite the parametrized
_DeliveryAreaTestCase.code type as str.
7. Release notes
- Move the two "Upgrading" bullets for
delivery_area_from_proto and DeliveryArea.__post_init__ from the v0.4.x notes into the v0.5.0 notes with the tense adjusted ("is now removed" / "now raises ValueError").
- Note the tightened
code: str type in the "Upgrading" section explicitly if the semver-0.x.x guide requires it.
Verification checklist
References
The shape landed in the v0.4.x cycle in #241
Context
The v0.4.x cycle landed the delivery-area invariants work as a backwards-compatible set of soft shims so v0.4.0 callers keep working unchanged. Specifically:
DeliveryArea.codestayedstr | NoneandDeliveryArea.__post_init__only emits aDeprecationWarning(rather than raisingValueError) when the 2-character-code / non-UNSPECIFIED-code_typeinvariants are violated.delivery_area_from_proto()was kept and marked@deprecated, redirecting to the newdelivery_area_from_proto2().delivery_area_from_proto()internally wraps itsDeliveryArea(...)construction inwarnings.catch_warnings()so callers of the deprecated entry point see exactly oneDeprecationWarning(the outer one, not the inner__post_init__one).This issue tracks the v0.5.0 clean-up: remove the shims and land the strict shape originally planned.
Scope: what to remove in v0.5.0
1. Delete the deprecated proto converter
src/frequenz/client/common/grid/proto/v1alpha8/_delivery_area.py@deprecateddelivery_area_from_proto()function entirely.typing_extensions.deprecatedimport if no other symbol in the file uses it.import logging,_logger = ...andimport warningsif they become unused after the deletion.2. Rename
delivery_area_from_proto2→delivery_area_from_protoSame file:
src/frequenz/client/common/grid/proto/v1alpha8/_delivery_area.pyRename the function to reclaim the natural name.
Optional cleanup: because
DeliveryAreawill raiseValueErrorin step 4, the new implementation can drop the manuallen(code) != 2 or raw_code_type == 0pre-check and just use thetry/except ValueErrorpattern originally planned, e.g.:Update
src/frequenz/client/common/grid/proto/v1alpha8/__init__.py:remove
delivery_area_from_proto2from imports /__all__, keepdelivery_area_from_proto.Update
src/frequenz/client/common/microgrid/proto/v1alpha8/_microgrid.pyto call
delivery_area_from_protoagain (no2suffix).3. Tighten
DeliveryArea.code: str | None→strsrc/frequenz/client/common/grid/_delivery_area.pyBaseDeliveryArea.codebecomesstr(drop the| None). Both concrete leaves inherit the tightened type.self.code is None or ...branch insideDeliveryArea.__post_init__—len(self.code) != 2alone covers emptyand short/long codes.
BaseDeliveryArea.__str__if theself.code or "<NO CODE>"fallback is now purely for the empty-string case (still true; keep it).4. Convert
DeliveryArea.__post_init__from warn → raisesrc/frequenz/client/common/grid/_delivery_area.pywarnings.warn(..., DeprecationWarning, stacklevel=3)call withraise ValueError("DeliveryArea invariants violated: " + "; ".join(issues)).DeprecationWarning; will raise in a future release" language anddocument the hard
ValueError.5. Simplify inner suppression sites
warnings.catch_warnings()block aroundDeliveryArea(code=..., code_type=...)inside the deleteddelivery_area_from_proto()goes away with step 1 (nothing else needsit).
microgrid_from_protoand any other internal call site that hand-constructs aDeliveryAreafrom wire data — they should now go throughdelivery_area_from_proto(post-rename) and catch itsValueErrorif they need to distinguish invalid-vs-valid outside the return type.6. Tests
Delete
test_from_protoandtest_get_code_type_from_proto_unspecified_raisesandtest_from_proto_emits_deprecation_warningintests/grid/proto/v1alpha8/test_delivery_area.py(they exercise thedeleted deprecated function).
Rename the
test_from_proto2*tests totest_from_proto*(drop the2) and droppytest.deprecated_call(...)wrappers.In
tests/grid/test_delivery_area.py:test_creation_invalid_emits_deprecation_warning(and its_membersibling) totest_creation_invalid_raisesand switch frompytest.warns(DeprecationWarning, ...)topytest.raises(ValueError, ...)._DeliveryAreaTestCase.codetype asstr.7. Release notes
delivery_area_from_protoandDeliveryArea.__post_init__from the v0.4.x notes into the v0.5.0 notes with the tense adjusted ("is now removed" / "now raisesValueError").code: strtype in the "Upgrading" section explicitly if the semver-0.x.x guide requires it.Verification checklist
pytest— full suite greenmypy— cleanpylint src/frequenz/client/common tests— 10.00/10flake8 src tests— silentRELEASE_NOTES.mdmentions the removal under Upgrading and anycorresponding New Features entries retired.
References
The shape landed in the v0.4.x cycle in #241