Skip to content

Commit bcca778

Browse files
migrate Enum to Literal when possible
1 parent 4876a5c commit bcca778

18 files changed

+505
-684
lines changed

lib/controllers/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async def get_rocketpy_env_binary(
4141
return env_service.get_env_binary()
4242

4343
@controller_exception_handler
44-
async def simulate_env(
44+
async def get_environment_simulation(
4545
self, env_id: str
4646
) -> EnvironmentSummary:
4747
"""

lib/models/environment.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import datetime
2-
from enum import Enum
3-
from typing import Optional, ClassVar, Self
2+
from typing import Optional, ClassVar, Self, Literal
43
from lib.models.interface import ApiBaseModel
54

65

7-
class AtmosphericModelTypes(str, Enum):
8-
STANDARD_ATMOSPHERE: str = "STANDARD_ATMOSPHERE"
9-
CUSTOM_ATMOSPHERE: str = "CUSTOM_ATMOSPHERE"
10-
WYOMING_SOUNDING: str = "WYOMING_SOUNDING"
11-
FORECAST: str = "FORECAST"
12-
REANALYSIS: str = "REANALYSIS"
13-
ENSEMBLE: str = "ENSEMBLE"
14-
15-
166
class EnvironmentModel(ApiBaseModel):
177
NAME: ClassVar = 'environment'
188
METHODS: ClassVar = ('POST', 'GET', 'PUT', 'DELETE')
@@ -21,9 +11,7 @@ class EnvironmentModel(ApiBaseModel):
2111
elevation: Optional[int] = 1
2212

2313
# Optional parameters
24-
atmospheric_model_type: AtmosphericModelTypes = (
25-
AtmosphericModelTypes.STANDARD_ATMOSPHERE
26-
)
14+
atmospheric_model_type: Literal['standard_atmosphere', 'custom_atmosphere', 'wyoming_sounding', 'forecast', 'reanalysis', 'ensemble'] = 'standard_atmosphere'
2715
atmospheric_model_file: Optional[str] = None
2816
date: Optional[datetime.datetime] = (
2917
datetime.datetime.today() + datetime.timedelta(days=1)

lib/models/flight.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
from enum import Enum
2-
from typing import Optional, Self, ClassVar
1+
from typing import Optional, Self, ClassVar, Literal
32
from lib.models.interface import ApiBaseModel
43
from lib.models.rocket import RocketModel
54
from lib.models.environment import EnvironmentModel
65

76

8-
class EquationsOfMotion(str, Enum):
9-
STANDARD: str = "STANDARD"
10-
SOLID_PROPULSION: str = "SOLID_PROPULSION"
11-
12-
137
class FlightModel(ApiBaseModel):
148
NAME: ClassVar = "flight"
159
METHODS: ClassVar = ("POST", "GET", "PUT", "DELETE")
@@ -20,7 +14,7 @@ class FlightModel(ApiBaseModel):
2014
rail_length: float = 1
2115
time_overshoot: bool = True
2216
terminate_on_apogee: bool = True
23-
equations_of_motion: EquationsOfMotion = EquationsOfMotion.STANDARD
17+
equations_of_motion: Literal['standard', 'solid_propulsion'] = 'standard'
2418

2519
# Optional parameters
2620
inclination: Optional[int] = None

lib/models/motor.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pydantic import PrivateAttr, model_validator
44

55
from lib.models.interface import ApiBaseModel
6-
from lib.models.sub.tanks import MotorTank, InterpolationMethods, TankCoordinateSystemOrientation
6+
from lib.models.sub.tanks import MotorTank
77

88

99
class MotorKinds(str, Enum):
@@ -48,10 +48,8 @@ class MotorModel(ApiBaseModel):
4848
throat_radius: Optional[float] = None
4949

5050
# Optional parameters
51-
interpolation_method: InterpolationMethods = InterpolationMethods.LINEAR
52-
coordinate_system_orientation: TankCoordinateSystemOrientation = (
53-
TankCoordinateSystemOrientation.NOZZLE_TO_COMBUSTION_CHAMBER
54-
)
51+
interpolation_method: Literal['linear', 'spline', 'akima', 'polynomial', 'shepard', 'rbf'] = 'linear'
52+
coordinate_system_orientation: Literal['nozzle_to_combustion_chamber', 'combustion_chamber_to_nozzle'] = 'nose_to_combustion_chamber'
5553
reshape_thrust_curve: Union[bool, tuple] = False
5654

5755
# Computed parameters

lib/models/rocket.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from enum import Enum
2-
from typing import Optional, Tuple, List, Union, Self, ClassVar
1+
from typing import Optional, Tuple, List, Union, Self, ClassVar, Literal
32
from lib.models.interface import ApiBaseModel
43
from lib.models.motor import MotorModel
54
from lib.models.sub.aerosurfaces import (
@@ -11,11 +10,6 @@
1110
)
1211

1312

14-
class RocketCoordinateSystemOrientation(str, Enum):
15-
TAIL_TO_NOSE: str = "TAIL_TO_NOSE"
16-
NOSE_TO_TAIL: str = "NOSE_TO_TAIL"
17-
18-
1913
class RocketModel(ApiBaseModel):
2014
NAME: ClassVar = "rocket"
2115
METHODS: ClassVar = ("POST", "GET", "PUT", "DELETE")
@@ -32,9 +26,7 @@ class RocketModel(ApiBaseModel):
3226
] = (0, 0, 0)
3327
power_off_drag: List[Tuple[float, float]] = [(0, 0)]
3428
power_on_drag: List[Tuple[float, float]] = [(0, 0)]
35-
coordinate_system_orientation: RocketCoordinateSystemOrientation = (
36-
RocketCoordinateSystemOrientation.TAIL_TO_NOSE
37-
)
29+
coordinate_system_orientation: Literal['tail_to_nose', 'nose_to_tail'] = 'tail_to_nose'
3830
nose: NoseCone
3931
fins: List[Fins]
4032

lib/models/sub/aerosurfaces.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from enum import Enum
2-
from typing import Optional, Tuple, List, Union
1+
from typing import Optional, Tuple, List, Union, Literal
32
from pydantic import BaseModel, Field
43

54

@@ -28,18 +27,8 @@ class NoseCone(BaseModel):
2827
rocket_radius: float
2928

3029

31-
class FinsKinds(str, Enum):
32-
TRAPEZOIDAL: str = "TRAPEZOIDAL"
33-
ELLIPTICAL: str = "ELLIPTICAL"
34-
35-
36-
class AngleUnit(str, Enum):
37-
RADIANS: str = "RADIANS"
38-
DEGREES: str = "DEGREES"
39-
40-
4130
class Fins(BaseModel):
42-
fins_kind: FinsKinds
31+
fins_kind: Literal['trapezoidal', 'elliptical']
4332
name: str
4433
n: int
4534
root_chord: float
@@ -50,7 +39,7 @@ class Fins(BaseModel):
5039
tip_chord: Optional[float] = None
5140
cant_angle: Optional[float] = None
5241
rocket_radius: Optional[float] = None
53-
airfoil: Optional[Tuple[List[Tuple[float, float]], AngleUnit]] = None
42+
airfoil: Optional[Tuple[List[Tuple[float, float]], Literal['radians', 'degrees']]] = None
5443

5544
def get_additional_parameters(self):
5645
return {

lib/models/sub/tanks.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,11 @@ class TankKinds(str, Enum):
1010
ULLAGE: str = "ULLAGE"
1111

1212

13-
class TankCoordinateSystemOrientation(str, Enum):
14-
NOZZLE_TO_COMBUSTION_CHAMBER: str = "NOZZLE_TO_COMBUSTION_CHAMBER"
15-
COMBUSTION_CHAMBER_TO_NOZZLE: str = "COMBUSTION_CHAMBER_TO_NOZZLE"
16-
17-
1813
class TankFluids(BaseModel):
1914
name: str
2015
density: float
2116

2217

23-
class InterpolationMethods(str, Enum):
24-
LINEAR: str = "LINEAR"
25-
SPLINE: str = "SPLINE"
26-
AKIMA: str = "AKIMA"
27-
POLYNOMIAL: str = "POLYNOMIAL"
28-
SHEPARD: str = "SHEPARD"
29-
RBF: str = "RBF"
30-
31-
3218
class MotorTank(BaseModel):
3319
# Required parameters
3420
geometry: List[Tuple[Tuple[float, float], float]]

lib/routes/environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ async def read_rocketpy_env(environment_id: str):
116116

117117

118118
@router.get("/{environment_id}/summary")
119-
async def simulate_env(environment_id: str) -> EnvironmentSummary:
119+
async def get_environment_simulation(environment_id: str) -> EnvironmentSummary:
120120
"""
121121
Loads rocketpy.environment simulation
122122
123123
## Args
124124
``` environment_id: str ```
125125
"""
126-
with tracer.start_as_current_span("simulate_env"):
126+
with tracer.start_as_current_span("get_environment_simulation"):
127127
controller = EnvironmentController()
128128
return await controller.get_environment_summary(environment_id)

lib/services/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def from_env_model(cls, env: EnvironmentModel) -> Self:
2929
date=env.date,
3030
)
3131
rocketpy_env.set_atmospheric_model(
32-
type=env.atmospheric_model_type.value.lower(),
32+
type=env.atmospheric_model_type,
3333
file=env.atmospheric_model_file,
3434
)
3535
return cls(environment=rocketpy_env)

lib/services/flight.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def from_flight_model(cls, flight: FlightView) -> Self:
3434
rail_length=flight.rail_length,
3535
terminate_on_apogee=flight.terminate_on_apogee,
3636
time_overshoot=flight.time_overshoot,
37-
equations_of_motion=flight.equations_of_motion.value.lower(),
37+
equations_of_motion=flight.equations_of_motion,
3838
**flight.get_additional_parameters(),
3939
)
4040
return cls(flight=rocketpy_flight)

lib/services/motor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def from_motor_model(cls, motor: MotorView) -> Self:
4242
"dry_mass": motor.dry_mass,
4343
"dry_inertia": motor.dry_inertia,
4444
"center_of_dry_mass_position": motor.center_of_dry_mass_position,
45-
"coordinate_system_orientation": motor.coordinate_system_orientation.value.lower(),
46-
"interpolation_method": motor.interpolation_method.value.lower(),
45+
"coordinate_system_orientation": motor.coordinate_system_orientation,
46+
"interpolation_method": motor.interpolation_method,
4747
"reshape_thrust_curve": False or motor.reshape_thrust_curve,
4848
}
4949

lib/services/rocket.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def from_rocket_model(cls, rocket: RocketView) -> Self:
4343
power_off_drag=rocket.power_off_drag,
4444
power_on_drag=rocket.power_on_drag,
4545
center_of_mass_without_motor=rocket.center_of_mass_without_motor,
46-
coordinate_system_orientation=rocket.coordinate_system_orientation.value.lower(),
46+
coordinate_system_orientation=rocket.coordinate_system_orientation,
4747
)
4848

4949
# RailButtons
@@ -157,15 +157,15 @@ def get_rocketpy_finset(fins: Fins, kind: str) -> RocketPyFins:
157157
RocketPyEllipticalFins
158158
"""
159159
match kind:
160-
case "TRAPEZOIDAL":
160+
case "trapezoidal":
161161
rocketpy_finset = RocketPyTrapezoidalFins(
162162
n=fins.n,
163163
name=fins.name,
164164
root_chord=fins.root_chord,
165165
span=fins.span,
166166
**fins.get_additional_parameters(),
167167
)
168-
case "ELLIPTICAL":
168+
case "elliptical":
169169
rocketpy_finset = RocketPyEllipticalFins(
170170
n=fins.n,
171171
name=fins.name,

lib/views/motor.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import List, Any, Optional
22
from pydantic import BaseModel, ConfigDict
33
from lib.views.interface import ApiBaseView
4-
from lib.models.sub.tanks import TankCoordinateSystemOrientation
54
from lib.models.motor import MotorModel, MotorKinds
65
from lib.utils import to_python_primitive
76

@@ -12,9 +11,7 @@ class MotorSummary(BaseModel):
1211
burn_out_time: Optional[float] = None
1312
burn_start_time: Optional[float] = None
1413
center_of_dry_mass_position: Optional[float] = None
15-
coordinate_system_orientation: str = (
16-
TankCoordinateSystemOrientation.NOZZLE_TO_COMBUSTION_CHAMBER.value
17-
)
14+
coordinate_system_orientation: str = 'nozzle_to_combustion_chamber'
1815
dry_I_11: Optional[float] = None
1916
dry_I_12: Optional[float] = None
2017
dry_I_13: Optional[float] = None

lib/views/rocket.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
from typing import Any, Optional
22
from pydantic import ConfigDict
3-
from lib.models.rocket import RocketModel, RocketCoordinateSystemOrientation
3+
from lib.models.rocket import RocketModel
44
from lib.views.interface import ApiBaseView
55
from lib.views.motor import MotorView, MotorSummary
66
from lib.utils import to_python_primitive
77

88

99
class RocketSummary(MotorSummary):
1010
area: Optional[float] = None
11-
coordinate_system_orientation: str = (
12-
RocketCoordinateSystemOrientation.TAIL_TO_NOSE.value
13-
)
11+
coordinate_system_orientation: str = 'tail_to_nose'
1412
center_of_mass_without_motor: Optional[float] = None
1513
motor_center_of_dry_mass_position: Optional[float] = None
1614
motor_position: Optional[float] = None

tests/test_routes/conftest.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010

1111
@pytest.fixture
12-
def stub_env():
12+
def stub_environment_dump():
1313
env = EnvironmentModel(latitude=0, longitude=0)
1414
env_json = env.model_dump_json()
1515
return json.loads(env_json)
1616

1717

1818
@pytest.fixture
19-
def stub_motor():
19+
def stub_motor_dump():
2020
motor = MotorModel(
2121
thrust_source=[[0, 0]],
2222
burn_time=0,
@@ -30,7 +30,7 @@ def stub_motor():
3030

3131

3232
@pytest.fixture
33-
def stub_tank():
33+
def stub_tank_dump():
3434
tank = MotorTank(
3535
geometry=[[(0, 0), 0]],
3636
gas=TankFluids(name='gas', density=0),
@@ -81,7 +81,7 @@ def stub_mass_tank(stub_tank):
8181

8282

8383
@pytest.fixture
84-
def stub_nose_cone():
84+
def stub_nose_cone_dump():
8585
nose_cone = NoseCone(
8686
name='nose',
8787
length=0,
@@ -95,9 +95,9 @@ def stub_nose_cone():
9595

9696

9797
@pytest.fixture
98-
def stub_fins():
98+
def stub_fins_dump():
9999
fins = Fins(
100-
fins_kind='TRAPEZOIDAL',
100+
fins_kind='trapezoidal',
101101
name='fins',
102102
n=0,
103103
root_chord=0,
@@ -109,7 +109,7 @@ def stub_fins():
109109

110110

111111
@pytest.fixture
112-
def stub_rocket(stub_motor, stub_nose_cone, stub_fins):
112+
def stub_rocket_dump(stub_motor, stub_nose_cone, stub_fins):
113113
rocket = RocketModel(
114114
motor=stub_motor,
115115
radius=0,
@@ -121,7 +121,7 @@ def stub_rocket(stub_motor, stub_nose_cone, stub_fins):
121121
power_on_drag=[(0, 0)],
122122
nose=stub_nose_cone,
123123
fins=[stub_fins],
124-
coordinate_system_orientation='TAIL_TO_NOSE',
124+
coordinate_system_orientation='tail_to_nose',
125125
)
126126
rocket_json = rocket.model_dump_json()
127127
return json.loads(rocket_json)

0 commit comments

Comments
 (0)