Skip to content

Commit

Permalink
modernize pydantic validator. fix makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanbconaway committed Nov 9, 2024
1 parent 886ac5e commit 4496a26
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ format:
@ ruff format

.PHONY:
test:
@ pytest tests --verbose
pytest:
pytest test --verbose

.PHONY:
lint-test: lint test
lint-test: lint pytest
1 change: 1 addition & 0 deletions src/underground/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""A realtime MTA module."""

from pathlib import Path

from .models import SubwayFeed
Expand Down
1 change: 1 addition & 0 deletions src/underground/cli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Command line tools for the MTA module."""

from . import cli

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion src/underground/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ def resolve_url(route_or_url: str) -> str:
return route_or_url

if route_or_url not in ROUTE_REMAP:
raise ValueError("Unknown route or url: %s" % route_or_url)
raise ValueError(f"Unknown route or url: {route_or_url}")

return ROUTE_FEED_MAP[ROUTE_REMAP[route_or_url]]
36 changes: 18 additions & 18 deletions src/underground/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
class UnixTimestamp(pydantic.BaseModel):
"""A unix timestamp model."""

time: datetime.datetime = None
time: typing.Optional[datetime.datetime] = None

@property
def timestamp_nyc(self):
def timestamp_nyc(self) -> typing.Optional[datetime.datetime]:
"""Return the NYC datetime."""
if not self.time:
return None
Expand All @@ -29,7 +29,7 @@ class FeedHeader(pydantic.BaseModel):
timestamp: datetime.datetime

@property
def timestamp_nyc(self):
def timestamp_nyc(self) -> datetime.datetime:
"""Return the NYC datetime of the header."""
return self.timestamp.astimezone(pytz.timezone(metadata.DEFAULT_TIMEZONE))

Expand All @@ -38,19 +38,19 @@ class Trip(pydantic.BaseModel):
"""Model describing a train trip."""

trip_id: str
start_time: datetime.time = None
start_time: typing.Optional[datetime.time] = None
start_date: int
route_id: str

@pydantic.validator("start_date")
@pydantic.field_validator("start_date")
def check_start_date(cls, start_date):
"""Start_date is an int, so check it conforms to date expectations."""
if start_date < 19000101:
raise ValueError("Probably not a date.")

return start_date

@pydantic.validator("route_id")
@pydantic.field_validator("route_id")
def check_route(cls, route_id):
"""Check for a valid route ID value."""
if route_id not in metadata.ROUTE_REMAP:
Expand All @@ -61,15 +61,15 @@ def check_route(cls, route_id):
return route_id

@property
def route_id_mapped(self):
def route_id_mapped(self) -> str:
"""Find the parent route ID.
This is helpful for grabbing the, e.g., 5 Train when you might have a 5X.
"""
return metadata.ROUTE_REMAP[self.route_id]

@property
def route_is_assigned(self):
def route_is_assigned(self) -> bool:
"""Return a flag indicating that there is a route."""
return self.route_id != ""

Expand All @@ -92,11 +92,11 @@ class StopTimeUpdate(pydantic.BaseModel):
"""

stop_id: str
arrival: UnixTimestamp = None
departure: UnixTimestamp = None
arrival: typing.Optional[UnixTimestamp] = None
departure: typing.Optional[UnixTimestamp] = None

@property
def depart_or_arrive(self) -> UnixTimestamp:
def depart_or_arrive(self) -> typing.Optional[UnixTimestamp]:
"""Return the departure or arrival time if either are specified.
This OR should usually be called because the MTA is inconsistent about when
Expand All @@ -122,7 +122,7 @@ class TripUpdate(pydantic.BaseModel):
"""

trip: Trip
stop_time_update: typing.List[StopTimeUpdate] = None
stop_time_update: typing.Optional[typing.List[StopTimeUpdate]] = None


class Vehicle(pydantic.BaseModel):
Expand Down Expand Up @@ -152,9 +152,9 @@ class Vehicle(pydantic.BaseModel):
"""

trip: Trip
timestamp: datetime.datetime = None
current_stop_sequence: int = None
stop_id: str = None
timestamp: typing.Optional[datetime.datetime] = None
current_stop_sequence: typing.Optional[int] = None
stop_id: typing.Optional[str] = None


class Entity(pydantic.BaseModel):
Expand All @@ -165,8 +165,8 @@ class Entity(pydantic.BaseModel):
"""

id: str
vehicle: Vehicle = None
trip_update: TripUpdate = None
vehicle: typing.Optional[Vehicle] = None
trip_update: typing.Optional[TripUpdate] = None


class SubwayFeed(pydantic.BaseModel):
Expand Down Expand Up @@ -214,7 +214,7 @@ def get(

def extract_stop_dict(
self, timezone: str = metadata.DEFAULT_TIMEZONE, stalled_timeout: int = 90
) -> dict:
) -> typing.Dict[str, typing.Dict[str, typing.List[datetime.datetime]]]:
"""Get the departure times for all stops in the feed.
Parameters
Expand Down

0 comments on commit 4496a26

Please sign in to comment.