Skip to content

Migrate echo from Pydantic V1 to attrs - #129

Merged
mikejturner merged 63 commits into
mainfrom
switch-to-attrs
Oct 15, 2026
Merged

mikejturner merged 63 commits into
mainfrom
switch-to-attrs

Conversation

@mikejturner

Copy link
Copy Markdown
Contributor

The is a major refactor removing all Pydantic code and replacing it with attrs.

See 'attrs-migration.md' for the full details.

discharging_power_limit: float = field(kw_only=True)
enable_trip_slack: bool = False
max_capacity: float
max_capacity: float = field(kw_only=True)

@explodingdinosaurs explodingdinosaurs Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

max_capacity should have units of energy (kWh) not power (kW).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See above.

interval_duration: int = field(kw_only=True)
# Battery attributes
max_capacity: float
max_capacity: float = field(kw_only=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

max_capacity should have units of energy (kWh) not power (kW).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See above

startup_cop: float | None

# factor for converting a temperature difference to kW required to achieve that delta T
deg_to_kw: float = field(kw_only=True)

@explodingdinosaurs explodingdinosaurs Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not an issue for this PR, but I feel deg_to_kw should have units of kWh, not kW, as it takes a certain amount of energy to raise the temperature of a substance, not power. But I will double check this.

Created an issue: #130

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

see above

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this might still be an issue, in a way that's not related to kw_only so I'll keep the issue and investigate it later

None # Maximum energy flow out of the storage at each interval, in energy_flow_units
)
max_temp: float = field(
kw_only=True, validator=temperature_range_validator

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's have a chat about this kw_only attribute

Comment thread src/echo/objectives/tariff.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
Comment thread tests/integration/test_evs.py Resolved
# Create edge objects and add to graph
system.connect_ports_and_create_edge(grid.ports["grid_to_cp"], connection_point.ports["cp_to_grid"])

with pytest.raises(Exception):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder why I put this line here originally?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It didn't make sense to be so I removed it.


# Rebuild all the edges before optimisation
system.rebuild_all_edges()
# system.rebuild_all_edges()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, that is very cool :)

Comment thread tests/unit/models/base/test_port.py Resolved
Comment thread tests/unit/models/base/test_port.py Resolved

@explodingdinosaurs explodingdinosaurs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

attrs is so much more elegant than pydantic for our purposes.

Most changes are detailed in comments in relevant locations. We'll need to have a chat about:

  • usage of kw_only attribute and what this means for variables that don't have units of power
  • How does one of the @reset_periods.validator decorator in src/echo/objectives/tariff.py function?

Other suggested changes:

@mikejturner

Copy link
Copy Markdown
Contributor Author

✅ Code coverage on switch-to-attrs is at 84% (compared to main being at 84%)

File Reductions

File Details
src/echo/models/agnostic/storage.py 🔻 1% (83% → 82%)
src/echo/models/base/edge.py 🔻 1% (69% → 68%)
src/echo/models/base/node.py 🔻 2% (98% → 96%)
src/echo/models/base/optimisation_graph.py 🔻 4% (76% → 72%)
src/echo/models/gas/boiler_tempcontrolled.py 🔻 2% (74% → 72%)
src/echo/models/thermal/storage.py 🔻 1% (82% → 81%)
src/echo/optimiser.py 🔻 1% (73% → 72%)
src/echo/validators.py 🔻 1% (95% → 94%)

Full Coverage Report

File Details
src/echo/__init__.py ✅ 100% → 100%
src/echo/configuration.py ✅ 100% → 100%
src/echo/constants.py ✅ 100% → 100%
src/echo/exceptions.py ✅ 100% → 100%
src/echo/functional.py ✅ 100% → 100%
src/echo/models/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/aggregation.py ✅ 95% → 95%
src/echo/models/agnostic/base.py ✅ 94% → 95%
src/echo/models/agnostic/bounded.py ✅ 100% → 100%
src/echo/models/agnostic/controlled.py ✅ 100% → 100%
src/echo/models/agnostic/flex.py ✅ 100% → 100%
src/echo/models/agnostic/input_output.py ✅ 100% → 100%
src/echo/models/agnostic/storage.py 🔻 1% (83% → 82%)
src/echo/models/agnostic/tellegen/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/tellegen/base.py ✅ 100% → 100%
src/echo/models/agnostic/tellegen/multi_commodity.py ✅ 38% → 43%
src/echo/models/agnostic/tellegen/partitioned_multi_commodity.py ✅ 96% → 96%
src/echo/models/agnostic/tellegen/three_way_valve.py ✅ 90% → 90%
src/echo/models/agnostic/time_delay.py ✅ 100% → 100%
src/echo/models/agnostic/time_varying.py ✅ 76% → 77%
src/echo/models/base/__init__.py ✅ 100% → 100%
src/echo/models/base/edge.py 🔻 1% (69% → 68%)
src/echo/models/base/node.py 🔻 2% (98% → 96%)
src/echo/models/base/optimisation_graph.py 🔻 4% (76% → 72%)
src/echo/models/base/path.py ✅ 92% → 92%
src/echo/models/base/port.py ✅ 98% → 98%
src/echo/models/base/transform.py ✅ 90% → 90%
src/echo/models/base/types.py ✅ 100% → 100%
src/echo/models/carbon/__init__.py ✅ 100% → 100%
src/echo/models/carbon/aggregation.py ✅ 100% → 100%
src/echo/models/carbon/base.py ✅ 100% → 100%
src/echo/models/electrical/__init__.py ✅ 100% → 100%
src/echo/models/electrical/base.py ✅ 100% → 100%
src/echo/models/electrical/ev/__init__.py ✅ 100% → 100%
src/echo/models/electrical/ev/base.py ✅ 94% → 94%
src/echo/models/electrical/ev/deprecated.py ✅ 33% → 34%
src/echo/models/electrical/ev/ev_with_profile.py ✅ 73% → 73%
src/echo/models/electrical/ev/v0g.py ✅ 100% → 100%
src/echo/models/electrical/ev/v1g.py ✅ 100% → 100%
src/echo/models/electrical/ev/v2g.py ✅ 100% → 100%
src/echo/models/electrical/generation.py ✅ 94% → 95%
src/echo/models/electrical/inverter.py ✅ 92% → 92%
src/echo/models/gas/__init__.py ✅ 100% → 100%
src/echo/models/gas/base.py ✅ 100% → 100%
src/echo/models/gas/boiler_fixedcop.py ✅ 86% → 86%
src/echo/models/gas/boiler_tempcontrolled.py 🔻 2% (74% → 72%)
src/echo/models/prebuilt/__init__.py ✅ 100% → 100%
src/echo/models/prebuilt/battery.py ✅ 71% → 75%
src/echo/models/prebuilt/diesel_generator.py ✅ 58% → 59%
src/echo/models/prebuilt/flex.py ✅ 79% → 79%
src/echo/models/prebuilt/load.py ✅ 91% → 91%
src/echo/models/prebuilt/scaled_solar.py ✅ 67% → 67%
src/echo/models/prebuilt/solar.py ✅ 50% → 50%
src/echo/models/scenario.py ✅ 100% → 100%
src/echo/models/thermal/__init__.py ✅ 100% → 100%
src/echo/models/thermal/base.py ✅ 49% → 49%
src/echo/models/thermal/parameterised_chiller.py ✅ 85% → 85%
src/echo/models/thermal/parameterised_heatpump.py ✅ 88% → 88%
src/echo/models/thermal/simple_chiller.py ✅ 88% → 88%
src/echo/models/thermal/simple_heatpump.py ✅ 92% → 92%
src/echo/models/thermal/simple_heatpump_dualoutput.py ✅ 92% → 92%
src/echo/models/thermal/storage.py 🔻 1% (82% → 81%)
src/echo/objectives/__init__.py ✅ 100% → 100%
src/echo/objectives/base.py ✅ 98% → 98%
src/echo/objectives/contingency.py ✅ 93% → 94%
src/echo/objectives/power.py ✅ 86% → 87%
src/echo/objectives/tariff.py ✅ 85% → 85%
src/echo/optimiser.py 🔻 1% (73% → 72%)
src/echo/tracker.py ✅ 43% → 43%
src/echo/utils.py ✅ 61% → 61%
src/echo/validators.py 🔻 1% (95% → 94%)
src/echo/visualization.py ✅ 91% → 91%
TOTAL ✅ 84% → 84%

@mikejturner

Copy link
Copy Markdown
Contributor Author

✅ Code coverage on switch-to-attrs is at 84% (compared to main being at 84%)

File Reductions

File Details
src/echo/models/agnostic/storage.py 🔻 1% (83% → 82%)
src/echo/models/base/edge.py 🔻 1% (69% → 68%)
src/echo/models/base/node.py 🔻 2% (98% → 96%)
src/echo/models/base/optimisation_graph.py 🔻 4% (76% → 72%)
src/echo/models/gas/boiler_tempcontrolled.py 🔻 2% (74% → 72%)
src/echo/models/thermal/storage.py 🔻 1% (82% → 81%)
src/echo/optimiser.py 🔻 1% (73% → 72%)
src/echo/validators.py 🔻 1% (95% → 94%)

Full Coverage Report

File Details
src/echo/__init__.py ✅ 100% → 100%
src/echo/configuration.py ✅ 100% → 100%
src/echo/constants.py ✅ 100% → 100%
src/echo/exceptions.py ✅ 100% → 100%
src/echo/functional.py ✅ 100% → 100%
src/echo/models/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/aggregation.py ✅ 95% → 95%
src/echo/models/agnostic/base.py ✅ 94% → 95%
src/echo/models/agnostic/bounded.py ✅ 100% → 100%
src/echo/models/agnostic/controlled.py ✅ 100% → 100%
src/echo/models/agnostic/flex.py ✅ 100% → 100%
src/echo/models/agnostic/input_output.py ✅ 100% → 100%
src/echo/models/agnostic/storage.py 🔻 1% (83% → 82%)
src/echo/models/agnostic/tellegen/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/tellegen/base.py ✅ 100% → 100%
src/echo/models/agnostic/tellegen/multi_commodity.py ✅ 38% → 43%
src/echo/models/agnostic/tellegen/partitioned_multi_commodity.py ✅ 96% → 96%
src/echo/models/agnostic/tellegen/three_way_valve.py ✅ 90% → 90%
src/echo/models/agnostic/time_delay.py ✅ 100% → 100%
src/echo/models/agnostic/time_varying.py ✅ 76% → 77%
src/echo/models/base/__init__.py ✅ 100% → 100%
src/echo/models/base/edge.py 🔻 1% (69% → 68%)
src/echo/models/base/node.py 🔻 2% (98% → 96%)
src/echo/models/base/optimisation_graph.py 🔻 4% (76% → 72%)
src/echo/models/base/path.py ✅ 92% → 92%
src/echo/models/base/port.py ✅ 98% → 98%
src/echo/models/base/transform.py ✅ 90% → 90%
src/echo/models/base/types.py ✅ 100% → 100%
src/echo/models/carbon/__init__.py ✅ 100% → 100%
src/echo/models/carbon/aggregation.py ✅ 100% → 100%
src/echo/models/carbon/base.py ✅ 100% → 100%
src/echo/models/electrical/__init__.py ✅ 100% → 100%
src/echo/models/electrical/base.py ✅ 100% → 100%
src/echo/models/electrical/ev/__init__.py ✅ 100% → 100%
src/echo/models/electrical/ev/base.py ✅ 94% → 94%
src/echo/models/electrical/ev/deprecated.py ✅ 33% → 34%
src/echo/models/electrical/ev/ev_with_profile.py ✅ 73% → 73%
src/echo/models/electrical/ev/v0g.py ✅ 100% → 100%
src/echo/models/electrical/ev/v1g.py ✅ 100% → 100%
src/echo/models/electrical/ev/v2g.py ✅ 100% → 100%
src/echo/models/electrical/generation.py ✅ 94% → 95%
src/echo/models/electrical/inverter.py ✅ 92% → 92%
src/echo/models/gas/__init__.py ✅ 100% → 100%
src/echo/models/gas/base.py ✅ 100% → 100%
src/echo/models/gas/boiler_fixedcop.py ✅ 86% → 86%
src/echo/models/gas/boiler_tempcontrolled.py 🔻 2% (74% → 72%)
src/echo/models/prebuilt/__init__.py ✅ 100% → 100%
src/echo/models/prebuilt/battery.py ✅ 71% → 75%
src/echo/models/prebuilt/diesel_generator.py ✅ 58% → 59%
src/echo/models/prebuilt/flex.py ✅ 79% → 79%
src/echo/models/prebuilt/load.py ✅ 91% → 91%
src/echo/models/prebuilt/scaled_solar.py ✅ 67% → 67%
src/echo/models/prebuilt/solar.py ✅ 50% → 50%
src/echo/models/scenario.py ✅ 100% → 100%
src/echo/models/thermal/__init__.py ✅ 100% → 100%
src/echo/models/thermal/base.py ✅ 49% → 49%
src/echo/models/thermal/parameterised_chiller.py ✅ 85% → 85%
src/echo/models/thermal/parameterised_heatpump.py ✅ 88% → 88%
src/echo/models/thermal/simple_chiller.py ✅ 88% → 88%
src/echo/models/thermal/simple_heatpump.py ✅ 92% → 92%
src/echo/models/thermal/simple_heatpump_dualoutput.py ✅ 92% → 92%
src/echo/models/thermal/storage.py 🔻 1% (82% → 81%)
src/echo/objectives/__init__.py ✅ 100% → 100%
src/echo/objectives/base.py ✅ 98% → 98%
src/echo/objectives/contingency.py ✅ 93% → 94%
src/echo/objectives/power.py ✅ 86% → 87%
src/echo/objectives/tariff.py ✅ 85% → 85%
src/echo/optimiser.py 🔻 1% (73% → 72%)
src/echo/tracker.py ✅ 43% → 43%
src/echo/utils.py ✅ 61% → 61%
src/echo/validators.py 🔻 1% (95% → 94%)
src/echo/visualization.py ✅ 91% → 91%
TOTAL ✅ 84% → 84%

1 similar comment
@mikejturner

Copy link
Copy Markdown
Contributor Author

✅ Code coverage on switch-to-attrs is at 84% (compared to main being at 84%)

File Reductions

File Details
src/echo/models/agnostic/storage.py 🔻 1% (83% → 82%)
src/echo/models/base/edge.py 🔻 1% (69% → 68%)
src/echo/models/base/node.py 🔻 2% (98% → 96%)
src/echo/models/base/optimisation_graph.py 🔻 4% (76% → 72%)
src/echo/models/gas/boiler_tempcontrolled.py 🔻 2% (74% → 72%)
src/echo/models/thermal/storage.py 🔻 1% (82% → 81%)
src/echo/optimiser.py 🔻 1% (73% → 72%)
src/echo/validators.py 🔻 1% (95% → 94%)

Full Coverage Report

File Details
src/echo/__init__.py ✅ 100% → 100%
src/echo/configuration.py ✅ 100% → 100%
src/echo/constants.py ✅ 100% → 100%
src/echo/exceptions.py ✅ 100% → 100%
src/echo/functional.py ✅ 100% → 100%
src/echo/models/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/aggregation.py ✅ 95% → 95%
src/echo/models/agnostic/base.py ✅ 94% → 95%
src/echo/models/agnostic/bounded.py ✅ 100% → 100%
src/echo/models/agnostic/controlled.py ✅ 100% → 100%
src/echo/models/agnostic/flex.py ✅ 100% → 100%
src/echo/models/agnostic/input_output.py ✅ 100% → 100%
src/echo/models/agnostic/storage.py 🔻 1% (83% → 82%)
src/echo/models/agnostic/tellegen/__init__.py ✅ 100% → 100%
src/echo/models/agnostic/tellegen/base.py ✅ 100% → 100%
src/echo/models/agnostic/tellegen/multi_commodity.py ✅ 38% → 43%
src/echo/models/agnostic/tellegen/partitioned_multi_commodity.py ✅ 96% → 96%
src/echo/models/agnostic/tellegen/three_way_valve.py ✅ 90% → 90%
src/echo/models/agnostic/time_delay.py ✅ 100% → 100%
src/echo/models/agnostic/time_varying.py ✅ 76% → 77%
src/echo/models/base/__init__.py ✅ 100% → 100%
src/echo/models/base/edge.py 🔻 1% (69% → 68%)
src/echo/models/base/node.py 🔻 2% (98% → 96%)
src/echo/models/base/optimisation_graph.py 🔻 4% (76% → 72%)
src/echo/models/base/path.py ✅ 92% → 92%
src/echo/models/base/port.py ✅ 98% → 98%
src/echo/models/base/transform.py ✅ 90% → 90%
src/echo/models/base/types.py ✅ 100% → 100%
src/echo/models/carbon/__init__.py ✅ 100% → 100%
src/echo/models/carbon/aggregation.py ✅ 100% → 100%
src/echo/models/carbon/base.py ✅ 100% → 100%
src/echo/models/electrical/__init__.py ✅ 100% → 100%
src/echo/models/electrical/base.py ✅ 100% → 100%
src/echo/models/electrical/ev/__init__.py ✅ 100% → 100%
src/echo/models/electrical/ev/base.py ✅ 94% → 94%
src/echo/models/electrical/ev/deprecated.py ✅ 33% → 34%
src/echo/models/electrical/ev/ev_with_profile.py ✅ 73% → 73%
src/echo/models/electrical/ev/v0g.py ✅ 100% → 100%
src/echo/models/electrical/ev/v1g.py ✅ 100% → 100%
src/echo/models/electrical/ev/v2g.py ✅ 100% → 100%
src/echo/models/electrical/generation.py ✅ 94% → 95%
src/echo/models/electrical/inverter.py ✅ 92% → 92%
src/echo/models/gas/__init__.py ✅ 100% → 100%
src/echo/models/gas/base.py ✅ 100% → 100%
src/echo/models/gas/boiler_fixedcop.py ✅ 86% → 86%
src/echo/models/gas/boiler_tempcontrolled.py 🔻 2% (74% → 72%)
src/echo/models/prebuilt/__init__.py ✅ 100% → 100%
src/echo/models/prebuilt/battery.py ✅ 71% → 75%
src/echo/models/prebuilt/diesel_generator.py ✅ 58% → 59%
src/echo/models/prebuilt/flex.py ✅ 79% → 79%
src/echo/models/prebuilt/load.py ✅ 91% → 91%
src/echo/models/prebuilt/scaled_solar.py ✅ 67% → 67%
src/echo/models/prebuilt/solar.py ✅ 50% → 50%
src/echo/models/scenario.py ✅ 100% → 100%
src/echo/models/thermal/__init__.py ✅ 100% → 100%
src/echo/models/thermal/base.py ✅ 49% → 49%
src/echo/models/thermal/parameterised_chiller.py ✅ 85% → 85%
src/echo/models/thermal/parameterised_heatpump.py ✅ 88% → 88%
src/echo/models/thermal/simple_chiller.py ✅ 88% → 88%
src/echo/models/thermal/simple_heatpump.py ✅ 92% → 92%
src/echo/models/thermal/simple_heatpump_dualoutput.py ✅ 92% → 92%
src/echo/models/thermal/storage.py 🔻 1% (82% → 81%)
src/echo/objectives/__init__.py ✅ 100% → 100%
src/echo/objectives/base.py ✅ 98% → 98%
src/echo/objectives/contingency.py ✅ 93% → 94%
src/echo/objectives/power.py ✅ 86% → 87%
src/echo/objectives/tariff.py ✅ 85% → 85%
src/echo/optimiser.py 🔻 1% (73% → 72%)
src/echo/tracker.py ✅ 43% → 43%
src/echo/utils.py ✅ 61% → 61%
src/echo/validators.py 🔻 1% (95% → 94%)
src/echo/visualization.py ✅ 91% → 91%
TOTAL ✅ 84% → 84%

@mikejturner
mikejturner merged commit 90a9e19 into main Oct 15, 2026
11 checks passed
@mikejturner
mikejturner deleted the switch-to-attrs branch October 15, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants