Skip to content

Remove DeliveryArea backward-compat #240

Description

@llucax

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_proto2delivery_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 | Nonestr

  • 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

  • pytest — full suite green
  • mypy — clean
  • pylint src/frequenz/client/common tests — 10.00/10
  • flake8 src tests — silent
  • RELEASE_NOTES.md mentions the removal under Upgrading and any
    corresponding New Features entries retired.

References

The shape landed in the v0.4.x cycle in #241

Metadata

Metadata

Assignees

Labels

part:gridAffects the grid protobuf definitionsscope:breaking-changeBreaking change, users will need to update their code

Fields

Priority

None yet

Effort

None yet

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions