Skip to content

Commit 8af45c8

Browse files
authored
Fix SolutionInform dict access (#451)
* recover ability to retrieve solution via key * map text to message * add test * try and see if post releases work * bump to 2.14.2 apparently we do not support .post1
1 parent dd1e3bc commit 8af45c8

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

pyoptsparse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "2.14.1"
1+
__version__ = "2.14.2"
22

33
from .pyOpt_history import History
44
from .pyOpt_variable import Variable

pyoptsparse/pyOpt_solution.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Standard Python modules
22
import copy
3-
from dataclasses import dataclass
3+
from dataclasses import asdict, dataclass
44
from typing import Optional
55

66
# External modules
@@ -23,6 +23,12 @@ class SolutionInform:
2323
def from_informs(cls, informs: dict[int, str], value: int):
2424
return cls(value=value, message=informs[value])
2525

26+
def __getitem__(self, key):
27+
d = asdict(self)
28+
if key == "text":
29+
return d["message"]
30+
return d[key]
31+
2632

2733
class Solution(Optimization):
2834
def __init__(self, optProb, xStar, fStar, lambdaStar, optInform: Optional[SolutionInform], info):

tests/test_other.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# First party modules
77
from pyoptsparse import Optimizers, list_optimizers
8+
from pyoptsparse.pyOpt_solution import SolutionInform
89
from pyoptsparse.pyOpt_utils import try_import_compiled_module_from_path
910

1011
# we have to unset this environment variable because otherwise
@@ -36,3 +37,12 @@ def test_list_optimizers(self):
3637
self.assertIn(Optimizers.PSQP, all_opt)
3738
self.assertIn(Optimizers.ALPSO, all_opt)
3839
self.assertIn(Optimizers.NSGA2, all_opt)
40+
41+
42+
class TestSolInform(unittest.TestCase):
43+
def test_sol_inform_key_access(self):
44+
sol = SolutionInform(value=1, message="test message")
45+
assert sol.value == 1
46+
assert sol["value"] == 1
47+
assert sol.message == "test message"
48+
assert sol["text"] == "test message"

0 commit comments

Comments
 (0)