Skip to content

Resolve change in InvalidNumber handling in writers#3390

Merged
blnicho merged 7 commits intoPyomo:mainfrom
jsiirola:invalid-number
Oct 31, 2024
Merged

Resolve change in InvalidNumber handling in writers#3390
blnicho merged 7 commits intoPyomo:mainfrom
jsiirola:invalid-number

Conversation

@jsiirola
Copy link
Copy Markdown
Member

@jsiirola jsiirola commented Oct 22, 2024

Fixes #3386 .

Summary/Motivation:

This resolves #3386 by making it so that both InvalidNumber.__str__ and InvalidNumber.__repr__ raise exceptions. It was relatively straightforward to remove the use of string conversions in the tests so that we no longer needed to cast InvalidNumber to a string.

Note that this includes an exception to the "str(InvalidNumber) raises an exception rule: if we are in the middle of processing an exception, this does not raise another exception and instead returns the old string representation.

Changes proposed in this PR:

  • Update InvalidNumber so that both str() and repr() raise exceptions
  • Clean up how some deprecation warnings generate warnings about NaN
  • Update tests

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.

Copy link
Copy Markdown
Member

@blnicho blnicho left a comment

Choose a reason for hiding this comment

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

I like this solution!

Comment on lines +64 to +67
def _inv2str(val):
return f"{val._str() if hasattr(val, '_str') else val}"


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.

Because this is exactly the same as lines 173-176 in pyomo/repn/ampl, wouldn't it make sense to instead put this in pyomo/repn/utils?

Copy link
Copy Markdown
Member Author

@jsiirola jsiirola Oct 28, 2024

Choose a reason for hiding this comment

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

Possibly? I didn't really want to advertise / test this as public functionality, but accessing "private" (underscored) objects from outside their module is also frowned upon. As the function was pretty trivial, I went with duplication.

@blnicho blnicho merged commit 2361980 into Pyomo:main Oct 31, 2024
@jsiirola jsiirola deleted the invalid-number branch November 15, 2024 03: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.

Resolve (change in) IPOPT solver/NL writer exception type for problems with invalid constants

3 participants