-
Notifications
You must be signed in to change notification settings - Fork 557
Fix boolean conversion in DeferredImportIndicator class #3746
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
Fix boolean conversion in DeferredImportIndicator class #3746
Conversation
This is a nonfunctional change in that diff --git a/pyomo/common/dependencies.py b/pyomo/common/dependencies.py
index 27167f12f..017b76c2c 100644
--- a/pyomo/common/dependencies.py
+++ b/pyomo/common/dependencies.py
@@ -344,13 +344,13 @@ class DeferredImportIndicator(_DeferredImportIndicatorBase):
if callback is not None:
DeferredImportCallbackFinder._callbacks.setdefault(name, []).append(self)
- def __bool__(self):
+ def __bool__(self) -> bool:
self.resolve()
return self._available
def resolve(self):
# Only attempt the import once, then cache some form of result
- if self._module is None:
+ if self._available is None:
package = self._original_globals.get('__name__', '')
try:
self._module, self._available = _perform_import(
( |
Even with the proposed changes it does not managed to infer that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK - I have thought about this a lot. The lack of a cast was previously defensive programming so that if - for whatever reason - resolve()
failed to set _available
to a bool
, then Python would raise an exception. I think I would still like that behavior, so I suggest we add an assertion and then a couple comments explaining why things look weird.
Co-authored-by: John Siirola <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3746 +/- ##
=======================================
Coverage 89.31% 89.32%
=======================================
Files 896 896
Lines 103697 103698 +1
=======================================
+ Hits 92619 92626 +7
+ Misses 11078 11072 -6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Fixes # NA
Summary/Motivation:
Some linters, such as Pyright, raise warnings when an object of class
DeferredImportIndicator
is used in an if statement, since its__bool__
method may returnNone
.This PR explicitly casts the result of that method to
bool
to ensure a proper boolean value is returned.Changes proposed in this PR:
__bool__
method ofDeferredImportIndicator
to be abool
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: