Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type check all exception arguments in component __call__ implementations #3538

Merged
merged 27 commits into from
Apr 1, 2025

Conversation

emma58
Copy link
Contributor

@emma58 emma58 commented Mar 25, 2025

Fixes #2922

Summary/Motivation:

Params and most other numeric- and Boolean-valued components are callable (to get the value), but they did not previously check that the exception argument was in fact a bool. This meant it was easy to make a typo missing a * character and silently rendering part of the expression constant (with the value of a Param, for example). This adds checks in the implementations of __call__ that exception is a bool. I still think we should discuss deprecating call to evaluate entirely, but this is a much less invasive change that will raise an error for many typos that would otherwise create silently wrong models.

Changes proposed in this PR:

  • Checks type of exception argument in implementations of __call__
  • Adds a test for the common Param-related typo

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@jsiirola
Copy link
Member

This is great, but should be promulgated to other expression objects:

./core/base/boolean_var.py
./core/base/constraint.py
./core/base/expression.py
./core/base/logical_constraint.py
./core/base/matrix_constraint.py
./core/base/objective.py
./core/base/param.py
./core/base/units_container.py
./core/base/var.py
./core/expr/base.py
./core/expr/boolean_value.py
./core/expr/numvalue.py
./core/expr/template_expr.py
./core/kernel/conic.py
./core/kernel/constraint.py
./core/kernel/expression.py
./core/kernel/matrix_constraint.py
./core/kernel/parameter.py
./core/kernel/variable.py
./repn/beta/matrix.py

@emma58 emma58 changed the title Params complain if called with a non-bool exception argument Type check all exception arguments in component __call__ implementations Mar 25, 2025
Copy link

codecov bot commented Mar 28, 2025

Codecov Report

Attention: Patch coverage is 97.75281% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.71%. Comparing base (6e71dec) to head (70cc92f).
Report is 47 commits behind head on main.

Files with missing lines Patch % Lines
pyomo/core/base/logical_constraint.py 66.66% 1 Missing ⚠️
pyomo/core/expr/numvalue.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3538   +/-   ##
=======================================
  Coverage   88.70%   88.71%           
=======================================
  Files         888      888           
  Lines      101963   102017   +54     
=======================================
+ Hits        90445    90502   +57     
+ Misses      11518    11515    -3     
Flag Coverage Δ
builders 26.57% <71.91%> (+0.02%) ⬆️
default 84.82% <97.75%> (?)
expensive 33.96% <79.77%> (?)
linux 86.17% <97.75%> (-2.28%) ⬇️
linux_other 86.17% <97.75%> (+<0.01%) ⬆️
osx 76.09% <97.75%> (+0.01%) ⬆️
win 84.67% <97.75%> (+0.01%) ⬆️
win_other 84.67% <97.75%> (+0.01%) ⬆️

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.

@emma58
Copy link
Contributor Author

emma58 commented Mar 28, 2025

@jsiirola this is ready for another review.

@jsiirola jsiirola merged commit 8f07dc9 into Pyomo:main Apr 1, 2025
64 of 65 checks passed
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.

Not throwing error when missing the * to multiply Param and Var
3 participants