Fix boolean conversion in DeferredImportIndicator class#3746
Fix boolean conversion in DeferredImportIndicator class#3746jsiirola merged 2 commits intoPyomo:mainfrom
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 |
jsiirola
left a comment
There was a problem hiding this comment.
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
DeferredImportIndicatoris used in an if statement, since its__bool__method may returnNone.This PR explicitly casts the result of that method to
boolto ensure a proper boolean value is returned.Changes proposed in this PR:
__bool__method ofDeferredImportIndicatorto be aboolLegal Acknowledgement
By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution: