Skip to content

Commit 9da4565

Browse files
Bizo883C8H10O2Marchma0
authored
Enh/custom warning no motor parachute aerosurface (#871)
* DOC: Add entry for custom warnings in CHANGELOG.md Added a placeholder in the [Unreleased] section for the upcoming feature to add custom warnings when a rocket is missing motors and/or aero-surface. See #285. * ENH: add custom warning for Rocket with no components This enhancement adds a warning when a Rocket object has no motors, parachutes, or AeroSurface components. It notifies the user so that they can add missing components before running simulations. See #285 * ENH:Add tests for _check_missing_components method in Rocket class * TST:improve test_check_missing_no_components_missing to correctly handle warnings * BUG: do not warn for rockets without parachute Only warn if motor or aerodynamic surfaces are missing. Never raise a warning for no parachute. See #285 * TST:Update the test:do not warn when missing parachute * MNT: simplify checks and update docstrings in rocket.py - Removed redundant len() check for aerodynamic_surfaces. - Added 'Returns: None' section to NumPy-style docstring. - Removed redundant '[WARNING]' prefix in warnings messages. * TST:update test_check_missing_no_components_missing to avoid TypeError: exceptions must be derived from Warning, not <class 'NoneType'> * TST:update test_check_missing_no_components_missing:import the lib at the top of file * TST:update test_check_missing_no_components_missing:import the lib at the top of file * Fix lint errors --------- Co-authored-by: Tang Xiaoyu <[email protected]> Co-authored-by: Marchma0 <[email protected]>
1 parent b451c38 commit 9da4565

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Attention: The newest changes should be on top -->
3232

3333
### Added
3434

35+
- ENH: Custom Exception errors and messages [#285](https://github.com/RocketPy-Team/RocketPy/issues/285)
36+
3537
### Changed
3638

3739
### Fixed

rocketpy/rocket/rocket.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,35 @@ def __init__( # pylint: disable=too-many-statements
384384
self.prints = _RocketPrints(self)
385385
self.plots = _RocketPlots(self)
386386

387+
def _check_missing_components(self):
388+
"""Check if the rocket is missing any essential components and issue a warning.
389+
390+
This method verifies whether the rocket has the following key components:
391+
- motor
392+
- aerodynamic surface(s)
393+
394+
If any of these components are missing, a single warning message is issued
395+
listing all missing components. This helps users quickly identify potential
396+
issues before running simulations or analyses.
397+
398+
Notes
399+
-----
400+
- The warning uses Python's built-in `warnings.warn` function.
401+
402+
Returns
403+
-------
404+
None
405+
"""
406+
missing_components = []
407+
if isinstance(self.motor, EmptyMotor):
408+
missing_components.append("motor")
409+
if not self.aerodynamic_surfaces:
410+
missing_components.append("aerodynamic surfaces")
411+
412+
if missing_components:
413+
component_list = ", ".join(missing_components)
414+
warnings.warn(f"Rocket has no {component_list} defined.", UserWarning)
415+
387416
@property
388417
def nosecones(self):
389418
"""A list containing all the nose cones currently added to the rocket."""

tests/unit/rocket/test_rocket.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from unittest.mock import patch
23

34
import numpy as np
@@ -370,6 +371,39 @@ def test_add_motor(calisto_motorless, cesaroni_m1670):
370371
assert center_of_mass_motorless is not center_of_mass_with_motor
371372

372373

374+
def test_check_missing_all_components(calisto_motorless):
375+
"""Tests the _check_missing_components method for a Rocket with no components."""
376+
with pytest.warns(UserWarning) as record:
377+
calisto_motorless._check_missing_components()
378+
379+
assert len(record) == 1
380+
msg = str(record[0].message)
381+
assert "motor" in msg
382+
assert "aerodynamic surfaces" in msg
383+
384+
385+
def test_check_missing_some_components(calisto):
386+
"""Tests the _check_missing_components method for a Rocket missing some components."""
387+
calisto.aerodynamic_surfaces = []
388+
389+
with pytest.warns(UserWarning) as record:
390+
calisto._check_missing_components()
391+
392+
assert len(record) == 1
393+
msg = str(record[0].message)
394+
assert "aerodynamic surfaces" in msg
395+
396+
397+
def test_check_missing_no_components_missing(calisto_robust):
398+
"""Tests the _check_missing_components method for a complete Rocket."""
399+
# Catch all warnings that occur inside this 'with' block.
400+
with warnings.catch_warnings(record=True) as w:
401+
warnings.simplefilter("always")
402+
calisto_robust._check_missing_components()
403+
# For a complete rocket, this method should NOT issue any warnings.
404+
assert len(w) == 0
405+
406+
373407
def test_set_rail_button(calisto):
374408
rail_buttons = calisto.set_rail_buttons(0.2, -0.5, 30)
375409
# assert buttons_distance

0 commit comments

Comments
 (0)