dcd5c5emodule-09: small refactor
3 files changed, +21, -10
module-09/ex2/space_crew.py
+10, -4
@@ -1,7 +1,7 @@
1 1 import json
2 2 from datetime import datetime
3 -from typing import Optional
4 -from pydantic import BaseModel, Field, ValidationError
3+from typing import Optional, Any
4+from pydantic import BaseModel, Field, ValidationError # type: ignore
5 5
6 6
7 7 class SpaceStation(BaseModel):
@@ -15,7 +15,7 @@ class SpaceStation(BaseModel):
1515 notes: Optional[str] = Field(default=None, max_length=200)
1616
1717
18 -def load_json(path: str) -> list:
18+def load_json(path: str) -> list[dict[str, Any]]:
1919 try:
2020 with open(path) as f:
2121 return json.load(f)
@@ -1,8 +1,13 @@
1 1 import json
2 2 from datetime import datetime
3 3 from enum import Enum
4 -from typing import Optional
5 -from pydantic import BaseModel, Field, ValidationError, model_validator
4+from typing import Optional, Any
5+from pydantic import ( # type: ignore
6+ BaseModel,
7+ Field,
8+ ValidationError,
9+ model_validator
10+)
611
712
813 class ContactType(Enum):
@@ -46,7 +51,7 @@ class AlienContact(BaseModel):
4651 return self
4752
4853
49 -def load_json(path: str) -> list:
54+def load_json(path: str) -> list[dict[str, Any]]:
5055 try:
5156 with open(path) as f:
5257 return json.load(f)
+10, -4
@@ -1,7 +1,13 @@
1 1 import json
2 2 from datetime import datetime
3 3 from enum import Enum
4 -from pydantic import BaseModel, Field, ValidationError, model_validator
4+from typing import Any
5+from pydantic import ( # type: ignore
6+ BaseModel,
7+ Field,
8+ ValidationError,
9+ model_validator
10+)
511
612
713 class Rank(Enum):
@@ -36,7 +42,7 @@ class SpaceMission(BaseModel):
3642 def safety_requirements(self) -> "SpaceMission":
3743 if not self.mission_id.startswith("M"):
3844 raise ValueError("mission_id must start with 'M'")
39 - cap_com = {Rank.commander, Rank.captain}
45+ cap_com = {Rank.captain, Rank.commander}
4046 if not any(m.rank in cap_com for m in self.crew):
4147 raise ValueError(
4248 "Mission must have at least one Commander or Captain"
@@ -46,7 +52,7 @@ class SpaceMission(BaseModel):
4652 ratio = experienced / len(self.crew)
4753 if ratio < 0.5:
4854 raise ValueError(
49 - "Mission over 365 days require 50% experienced crew"
55+ "Mission over 365 days requires 50% experienced crew"
5056 )
5157 inactive = [m.name for m in self.crew if not m.is_active]
5258 if inactive:
@@ -54,7 +60,7 @@ class SpaceMission(BaseModel):
5460 return self
5561
5662
57 -def load_json(path: str) -> list:
63+def load_json(path: str) -> list[dict[str, Any]]:
5864 try:
5965 with open(path) as f:
6066 return json.load(f)