Skip to content

Commit b574253

Browse files
addresses PR review
1 parent a26e01c commit b574253

File tree

7 files changed

+37
-24
lines changed

7 files changed

+37
-24
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ disable = """
5555
no-member,
5656
protected-access,
5757
import-outside-toplevel,
58+
too-many-statements,
5859
"""
5960

6061
[tool.ruff]

src/models/environment.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import datetime
1+
from datetime import datetime, timezone, timedelta
22
from typing import Optional, ClassVar, Self, Literal
3+
from pydantic import Field
34
from src.models.interface import ApiBaseModel
45

56

7+
def _default_future_datetime() -> datetime:
8+
"""Factory function to create timezone-aware datetime one day in the future."""
9+
return datetime.now(timezone.utc) + timedelta(days=1)
10+
11+
612
class EnvironmentModel(ApiBaseModel):
713
NAME: ClassVar = 'environment'
814
METHODS: ClassVar = ('POST', 'GET', 'PUT', 'DELETE')
@@ -20,9 +26,7 @@ class EnvironmentModel(ApiBaseModel):
2026
'ensemble',
2127
] = 'standard_atmosphere'
2228
atmospheric_model_file: Optional[str] = None
23-
date: Optional[datetime.datetime] = (
24-
datetime.datetime.today() + datetime.timedelta(days=1)
25-
)
29+
date: Optional[datetime] = Field(default_factory=_default_future_datetime)
2630

2731
@staticmethod
2832
def UPDATED():

src/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ def rocketpy_encoder(obj, config: DiscretizeConfig = DiscretizeConfig()):
6262
Dictionary of encoded attributes
6363
"""
6464

65-
# Create a copy to avoid mutating the original object
66-
obj_copy = copy.deepcopy(obj)
65+
if config is None:
66+
config = DiscretizeConfig()
67+
try:
68+
# Create a copy to avoid mutating the original object
69+
obj_copy = copy.deepcopy(obj)
70+
except Exception:
71+
# Fall back to a shallow copy if deep copy is not supported
72+
obj_copy = copy.copy(obj)
6773

6874
for attr_name in dir(obj_copy):
6975
if attr_name.startswith('_'):

src/views/environment.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
from typing import Optional, Any
2-
from datetime import datetime, timedelta
3-
from pydantic import ConfigDict
2+
from datetime import datetime, timezone, timedelta
3+
from pydantic import ConfigDict, Field
44
from src.views.interface import ApiBaseView
55
from src.models.environment import EnvironmentModel
66

77

8+
def _default_future_datetime() -> datetime:
9+
"""Factory function to create timezone-aware datetime one day in the future."""
10+
return datetime.now(timezone.utc) + timedelta(days=1)
11+
12+
813
class EnvironmentSimulation(ApiBaseView):
914
"""
1015
Environment simulation view that handles dynamically
@@ -17,9 +22,7 @@ class EnvironmentSimulation(ApiBaseView):
1722
"""
1823

1924
model_config = ConfigDict(
20-
ser_json_exclude_none=True, # keep parent's behavior
21-
extra='allow',
22-
arbitrary_types_allowed=True
25+
ser_json_exclude_none=True, extra='allow', arbitrary_types_allowed=True
2326
)
2427

2528
message: str = "Environment successfully simulated"
@@ -41,9 +44,13 @@ class EnvironmentSimulation(ApiBaseView):
4144
initial_hemisphere: Optional[str] = None
4245
initial_ew: Optional[str] = None
4346
max_expected_height: Optional[int] = None
44-
date: Optional[datetime] = datetime.today() + timedelta(days=1)
45-
local_date: Optional[datetime] = datetime.today() + timedelta(days=1)
46-
datetime_date: Optional[datetime] = datetime.today() + timedelta(days=1)
47+
date: Optional[datetime] = Field(default_factory=_default_future_datetime)
48+
local_date: Optional[datetime] = Field(
49+
default_factory=_default_future_datetime
50+
)
51+
datetime_date: Optional[datetime] = Field(
52+
default_factory=_default_future_datetime
53+
)
4754

4855
# Function attributes
4956
# discretized by rocketpy_encoder

src/views/flight.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class FlightSimulation(RocketSimulation, EnvironmentSimulation):
1919
"""
2020

2121
model_config = ConfigDict(
22-
ser_json_exclude_none=True,
23-
extra='allow',
24-
arbitrary_types_allowed=True
22+
ser_json_exclude_none=True, extra='allow', arbitrary_types_allowed=True
2523
)
2624

2725
message: str = "Flight successfully simulated"

src/views/motor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ class MotorSimulation(ApiBaseView):
1616
"""
1717

1818
model_config = ConfigDict(
19-
ser_json_exclude_none=True,
20-
extra='allow',
21-
arbitrary_types_allowed=True
19+
ser_json_exclude_none=True, extra='allow', arbitrary_types_allowed=True
2220
)
2321

2422
message: str = "Motor successfully simulated"

src/views/rocket.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class RocketSimulation(MotorSimulation):
1919
"""
2020

2121
model_config = ConfigDict(
22-
ser_json_exclude_none=True,
23-
extra='allow',
24-
arbitrary_types_allowed=True
22+
ser_json_exclude_none=True, extra='allow', arbitrary_types_allowed=True
2523
)
2624

2725
message: str = "Rocket successfully simulated"
@@ -30,7 +28,8 @@ class RocketSimulation(MotorSimulation):
3028
radius: Optional[float] = None
3129
mass: Optional[float] = None
3230
inertia: Optional[
33-
tuple[float, float, float] | tuple[float, float, float, float, float, float]
31+
tuple[float, float, float]
32+
| tuple[float, float, float, float, float, float]
3433
] = None
3534
power_off_drag: Optional[list[tuple[float, float]]] = None
3635
power_on_drag: Optional[list[tuple[float, float]]] = None

0 commit comments

Comments
 (0)