Skip to content

Commit cbafd72

Browse files
thc1006Gui-FernandesBR
authored andcommitted
Raise the sampler type check rather than asserting it
Gui's point on the review. `python -O` strips an assert, and this is what keeps a non-sampler out of the model, so it has to be a raise. Same shape as #1103, which took the identical route for the parachute triggers. AssertionError is kept rather than swapped for TypeError, because the docstring on develop already documents it and a caller catching it should keep working. Two tests. One is the behaviour; the other runs a child interpreter under -O, since that is the mechanism and the plain test passes either way. Note this module carries thirteen more asserts on develop, none of them mine. Happy to send them separately if you want the same treatment there. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 2ff425c commit cbafd72

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎rocketpy/stochastic/stochastic_model.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,13 @@ def _validate_custom_sampler(self, input_name, sampler):
545545
AssertionError
546546
If the input is not in a valid format.
547547
"""
548-
assert isinstance(sampler, CustomSampler), (
549-
f"`{input_name}` must be a CustomSampler, not {type(sampler).__name__}"
550-
)
548+
# Raised rather than asserted, the same way #1103 handles it: `python -O`
549+
# strips an assert, and the documented AssertionError is kept so callers
550+
# that already catch it still do.
551+
if not isinstance(sampler, CustomSampler):
552+
raise AssertionError(
553+
f"`{input_name}` must be a CustomSampler, not {type(sampler).__name__}"
554+
)
551555
return sampler
552556

553557
def _validate_airfoil(self, airfoil):

‎tests/unit/stochastic/test_custom_sampler.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,38 @@ def test_a_group_key_does_not_depend_on_the_order_it_is_given():
346346
assert _sampler_seed(4242, ("wind_x", "wind_y")) == _sampler_seed(
347347
4242, ("wind_y", "wind_x")
348348
)
349+
350+
351+
def test_a_non_sampler_is_refused_even_under_optimisation():
352+
"""`python -O` strips an assert, so the check that keeps a non-sampler out
353+
of the model has to be a raise. The documented AssertionError is kept, so a
354+
caller already catching it is unaffected."""
355+
model = StochasticModel(SimpleNamespace(mass=0.0))
356+
357+
with pytest.raises(AssertionError, match="must be a CustomSampler"):
358+
model._validate_custom_sampler("mass", object())
359+
360+
361+
def test_the_refusal_survives_python_dash_o():
362+
"""The mechanism, not just the behaviour: run it in a child with -O and
363+
check the exception still arrives."""
364+
import subprocess
365+
import sys
366+
367+
program = (
368+
"from types import SimpleNamespace;"
369+
"from rocketpy.stochastic.stochastic_model import StochasticModel;"
370+
"m = StochasticModel(SimpleNamespace(mass=0.0));"
371+
"\ntry:\n"
372+
" m._validate_custom_sampler('mass', object())\n"
373+
"except AssertionError:\n"
374+
" print('refused')\n"
375+
)
376+
done = subprocess.run(
377+
[sys.executable, "-O", "-c", program],
378+
capture_output=True,
379+
text=True,
380+
check=True,
381+
)
382+
383+
assert "refused" in done.stdout, done.stderr

0 commit comments

Comments
 (0)