Skip to content

Commit 6186af7

Browse files
committed
with_version: necessary checks
1 parent dfb92bb commit 6186af7

4 files changed

Lines changed: 42 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ result = optimizer.solve(x0=[1.0, 0.0], xref=[0.0, 0.0])
122122

123123
See the dedicated [OCP documentation](https://alphaville.github.io/optimization-engine/docs/python-ocp-1) for more details and examples.
124124

125-
## Parametric Problems
125+
## General Parametric Optimization
126126

127127
**OpEn** can solve nonconvex parametric optimization problems of the general form
128128

@@ -132,8 +132,6 @@ See the dedicated [OCP documentation](https://alphaville.github.io/optimization-
132132
where *f* is a smooth cost, *U* is a simple - possibly nonconvex - set, *F<sub>1</sub>* and *F<sub>2</sub>* are nonlinear smooth mappings and *C* is a convex set ([read more](https://alphaville.github.io/optimization-engine/docs/open-intro)).
133133

134134

135-
## Code Generation Example
136-
137135
Code generation in **Python** in just a few lines of code (read the [docs](https://alphaville.github.io/optimization-engine/docs/python-examples) for details)
138136

139137
<a href="https://colab.research.google.com/drive/14F6IQWo8Q65mIz5Ru4FGxRPqyFMRMyzE?usp=sharing" target="_blank">

open-codegen/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Note: This is the Changelog file of `opengen` - the Python interface of OpEn
2929

3030
- Avoid duplicate builder log messages by disabling propagation to the root logger
3131
- Use reliable package version lookup when recording the installed CasADi version in optimizer manifests
32+
- Check valid version in `with_version`
3233

3334

3435
## [0.9.6] - 2026-03-14

open-codegen/opengen/config/meta.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import re # regular expressions
22

33

4+
SEMVER_PATTERN = re.compile(
5+
r"^(0|[1-9]\d*)\."
6+
r"(0|[1-9]\d*)\."
7+
r"(0|[1-9]\d*)"
8+
r"(?:-"
9+
r"(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)"
10+
r"(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*"
11+
r")?"
12+
r"(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$"
13+
)
14+
15+
416
class OptimizerMeta:
517
"""Metadata of auto-generated parametric optimizer
618
@@ -107,8 +119,20 @@ def with_version(self, optimizer_version):
107119
108120
:param optimizer_version: version of auto-generated optimizer
109121
122+
:raises ValueError: if ``optimizer_version`` is not a valid Cargo
123+
package version following Semantic Versioning, for example
124+
``0.1.0`` or ``1.2.3-alpha.1``
125+
110126
:returns: The current instance of OptimizerMeta
111127
"""
128+
if not isinstance(optimizer_version, str) or not SEMVER_PATTERN.match(optimizer_version):
129+
raise ValueError(
130+
"invalid optimizer version {!r}; expected a valid Cargo package "
131+
"version following Semantic Versioning, for example "
132+
"'0.1.0', '1.2.3-alpha.1', or '1.0.0+build.5'".format(
133+
optimizer_version
134+
)
135+
)
112136
self.__optimizer_version = optimizer_version
113137
return self
114138

open-codegen/test/test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,22 @@ def test_squared_norm(self):
549549
y = fun([3, 4])
550550
self.assertAlmostEqual(25., y, places=12)
551551

552+
def test_optimizer_meta_valid_version(self):
553+
meta = og.config.OptimizerMeta().with_version("1.2.3-alpha.1+build.5")
554+
self.assertEqual("1.2.3-alpha.1+build.5", meta.version)
555+
556+
def test_optimizer_meta_invalid_version1(self):
557+
with self.assertRaises(ValueError) as context:
558+
og.config.OptimizerMeta().with_version("^1.2")
559+
560+
self.assertIn("Cargo package version", str(context.exception))
561+
562+
def test_optimizer_meta_invalid_version2(self):
563+
with self.assertRaises(ValueError) as context:
564+
og.config.OptimizerMeta().with_version("0.1")
565+
566+
self.assertIn("Cargo package version", str(context.exception))
567+
552568

553569
if __name__ == '__main__':
554570
logging.getLogger('retry').setLevel(logging.ERROR)

0 commit comments

Comments
 (0)