Skip to content

Add dict-style component access to Model#226

Closed
FBumann wants to merge 1 commit intoBravos-Power:mainfrom
FBumann:feature/variable-names
Closed

Add dict-style component access to Model#226
FBumann wants to merge 1 commit intoBravos-Power:mainfrom
FBumann:feature/variable-names

Conversation

@FBumann
Copy link

@FBumann FBumann commented Feb 13, 2026

Summary

  • Add m["name"] = ... / m["name"] dict-style access for model components, enabling arbitrary string names (e.g. "Variable--2 xy", "con: budget ≤ 10")
  • Unified registry via _components dict so attribute-assigned components are also accessible via m["X"] and vice versa
  • Add __contains__ support ("X" in m)

Changes

src/pyoframe/_model.py — Extract _register_component() as single chokepoint for component registration. Add __setitem__, __getitem__, __contains__. Refactor __setattr__ to delegate to _register_component.

src/pyoframe/_core.py — Replace setattr(model, ...) / hasattr(m, ...) calls in Variable._on_add_to_model and Constraint.relax with dict-style access (model[name] = ... / name not in m).

tests/test_names.py — 8 new tests: non-identifier names, unified attr↔dict access, __contains__, expression bounds with non-identifier names, error cases (reserved name, duplicate, invalid type, missing key), and end-to-end solve.

Test plan

  • All existing tests pass (146 passed, 2 skipped due to missing ipopt/copt)
  • New test_names.py tests pass (16/16)

🤖 Generated with Claude Code

  - Added "_components" to _reserved_attributes
  - Initialized self._components: dict[str, Any] = {} at the top of __init__
  - Extracted _register_component(name, value) — the shared logic for type validation, duplicate check (now name in self._components),
  _on_add_to_model, list/map updates, logging, and storing in _components
  - Refactored __setattr__ to delegate to _register_component for non-reserved names
  - Added __setitem__ with reserved-name guard, calls _register_component, and also sets as attribute if the name is a valid identifier
  - Added __getitem__ looking up from _components
  - Added __contains__ checking _components

  src/pyoframe/_core.py

  - Variable._on_add_to_model: replaced setattr(model, ...) with model[...] = ... for _lb, _ub, _equals derived constraints
  - Constraint.relax: replaced hasattr(m, var_name) with var_name not in m, and setattr(m, ...) with m[...] = ...

  tests/test_names.py

  - Added 8 new tests covering: non-identifier names, unified access (attr↔dict), __contains__, expression bounds with non-identifier names, reserved
  name error, duplicate error, invalid type error, missing key error, and end-to-end solve

  Test results: 146 passed, 2 failed (pre-existing: ipopt/copt not installed), 105 skipped.
@FBumann FBumann changed the title src/pyoframe/_model.py Add dict-style component access to Model Feb 13, 2026
@FBumann FBumann marked this pull request as ready for review February 13, 2026 09:14
@FBumann FBumann requested a review from staadecker as a code owner February 13, 2026 09:14
@staadecker staadecker added the safe to test Used by priveleged users to enable running the workflows label Feb 13, 2026
@codecov
Copy link

codecov bot commented Feb 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.71%. Comparing base (b2f45a0) to head (1d3c306).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #226      +/-   ##
==========================================
+ Coverage   96.60%   96.71%   +0.10%     
==========================================
  Files          10       10              
  Lines        1563     1582      +19     
==========================================
+ Hits         1510     1530      +20     
+ Misses         53       52       -1     
Flag Coverage Δ
smart-tests 96.71% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@staadecker
Copy link
Member

I'm going to close this for now until the conversation in #227 is resolved at which point I'd be very happy to reopen this PR! Thank you again for the contribution.

@staadecker staadecker closed this Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe to test Used by priveleged users to enable running the workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants