Skip to content

Remove Default implementation for unit types#1145

Merged
tsmbland merged 3 commits intomainfrom
unit_types_default
Feb 26, 2026
Merged

Remove Default implementation for unit types#1145
tsmbland merged 3 commits intomainfrom
unit_types_default

Conversation

@tsmbland
Copy link
Collaborator

@tsmbland tsmbland commented Feb 25, 2026

I think this is cleaner/safer

Fixes #1143

@tsmbland tsmbland marked this pull request as ready for review February 25, 2026 14:18
Copilot AI review requested due to automatic review settings February 25, 2026 14:19
/// Note: this should be similar to the "shadow price" strategy, which is also based on marginal
/// costs of the most expensive producer, but may be more successful in cases where there are
// multiple SED/SVD outputs per asset.
/// multiple SED/SVD outputs per asset.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Surprised this sort of thing doesn't get flagged by any of the checks, to he honest

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah... it seems in general there aren't any tools that check doc comments, which is a bit annoying.

@tsmbland
Copy link
Collaborator Author

Link checker failing with <file:///home/runner/work/MUSE2/MUSE2/book/model/README.html#framework-overview> | Cannot find file: File not found. Check if file exists and path is correct - presumably nothing to do with this PR

Copy link
Contributor

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 removes Default from the project’s UnitType abstraction and from the generated unit wrapper types, to prevent silently defaulting domain-specific quantities (e.g., prices) to zero and to force explicit fallbacks where needed.

Changes:

  • Remove Default from the UnitType trait bound and from the base_unit_struct! derive set.
  • Replace or_default()/unwrap_or_default() usages that relied on unit defaults with explicit insertion/modification or explicit fallback values.
  • Update investment appraisal test scaffolding to construct ObjectiveCoefficients explicitly (instead of deriving Default in tests).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/units.rs Drops Default from the UnitType contract and base unit derives.
src/simulation/prices.rs Replaces or_default() accumulation with and_modify/or_insert; fixes a doc-comment line.
src/simulation/optimisation.rs Removes unwrap_or_default() on optional input prices and makes the fallback explicit via branching.
src/simulation/investment/appraisal/coefficients.rs Removes test-only Default derive for ObjectiveCoefficients.
src/simulation/investment/appraisal.rs Updates tests to build ObjectiveCoefficients explicitly.
src/input/asset.rs Removes an unused Default derive on an input CSV raw struct.
src/asset.rs Replaces unwrap_or_default() on missing prices with an explicit MoneyPerFlow(0.0) fallback.

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

Comment on lines 650 to +653
flow.coeff
* prices
.get(&flow.commodity.id, &self.region_id, time_slice)
.unwrap_or_default()
.unwrap_or(MoneyPerFlow(0.0))
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

This continues to treat a missing commodity price as zero via unwrap_or(MoneyPerFlow(0.0)). Given the motivation for removing Default on unit types (to avoid silently inferring missing prices as free), consider changing this call path to propagate absence (e.g., return Option/Result or require callers to provide a fallback explicitly at the call site where it’s truly intended).

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is beyond the scope of this PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you remember why we were treating missing prices as zero here? Are there cases where we might not have prices yet?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Some commodities genuinely don't have a price (all OTH commodities are unpriced), so it's fair to treat them as free in this case. Other (non-OTH) commodities may have a missing price because there are no producers of that commodity and no potential producers of that commodity in the following year. In this case it's probably NOT a good idea to treat the commodities as free, and this may be responsible for some of the weird prices Adam is describing in #1130

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 25, 2026 14:32
@codecov
Copy link

codecov bot commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.54%. Comparing base (e4b1990) to head (aba97e1).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1145      +/-   ##
==========================================
+ Coverage   87.52%   87.54%   +0.01%     
==========================================
  Files          55       55              
  Lines        7626     7634       +8     
  Branches     7626     7634       +8     
==========================================
+ Hits         6675     6683       +8     
  Misses        649      649              
  Partials      302      302              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

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

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.


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

Copy link
Collaborator

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

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

LGTM! Probably safer to remove this potential footgun.

Comment on lines 650 to +653
flow.coeff
* prices
.get(&flow.commodity.id, &self.region_id, time_slice)
.unwrap_or_default()
.unwrap_or(MoneyPerFlow(0.0))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you remember why we were treating missing prices as zero here? Are there cases where we might not have prices yet?

@tsmbland tsmbland merged commit e996669 into main Feb 26, 2026
8 checks passed
@tsmbland tsmbland deleted the unit_types_default branch February 26, 2026 14:35
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.

Remove Default implementation for unit types

3 participants