Skip to content

Commit 01d3f18

Browse files
committed
Convert typedefs in AST module to serde compat.
Replace usage of Sequence in the typedefs for an AST with a bare list because pyserde doesn't support the Sequence type alias (see yukinarit/pyserde#440). Once support is added for a Sequence it should be possible to revert the changes. Replaced custom serialization with the json stdlib with a call to pyserde.
1 parent fc8c39f commit 01d3f18

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

yass/__init__.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from typing import MutableSequence, TypeAlias, Iterable, Literal, Any, cast
66
import re
77
import sys
8-
import json
98
import enum
109
import logging
1110
import argparse
1211
import datetime
1312
import itertools
1413
import dataclasses
1514

15+
import serde.json
1616
import requests
1717
import lxml.html
1818

@@ -67,17 +67,5 @@ def main() -> None:
6767

6868
ast = parse_ast(s_periods, s_time_tables)
6969

70-
def encode(thing: Any):
71-
if dataclasses.is_dataclass(thing):
72-
return dataclasses.asdict(thing) # type: ignore
73-
if isinstance(thing, datetime.date):
74-
return thing.isoformat()
75-
if isinstance(thing, datetime.time):
76-
return thing.isoformat()
77-
if isinstance(thing, enum.Enum):
78-
return thing.value
79-
80-
return None
81-
82-
json.dump(ast, sys.stdout, indent=4, default=encode)
83-
sys.stdout.write("\n")
70+
serialized = serde.json.to_json(ast, indent=4)
71+
print(serialized)

yass/ast.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
An AST.
33
"""
44

5-
from typing import Sequence, TypeAlias, NewType
5+
from typing import TypeAlias, NewType
66
import enum
77
import datetime
88
import dataclasses
99

10+
import serde
11+
1012

1113
class StopPart(enum.Enum):
1214
"""
@@ -25,20 +27,20 @@ class StopPart(enum.Enum):
2527
TimeTableCell: TypeAlias = datetime.time | None
2628

2729

28-
@dataclasses.dataclass(frozen=True)
30+
@serde.serde
2931
class TimeTable:
3032
"""
3133
A TimeTable (mapping of Stop + StopPart to rows of times).
3234
"""
3335

34-
cols: Sequence[TimeTableColumn]
35-
time_matrix: Sequence[Sequence[TimeTableCell]]
36+
cols: list[TimeTableColumn]
37+
time_matrix: list[list[TimeTableCell]]
3638

3739

3840
TimeTableIdx = NewType("TimeTableIdx", int)
3941

4042

41-
@dataclasses.dataclass(frozen=True)
43+
@serde.serde
4244
class SubPeriod:
4345
"""
4446
A SubPeriod (e.g. Weekday, Weekend).
@@ -50,7 +52,7 @@ class SubPeriod:
5052
SubPeriodIdx = NewType("SubPeriodIdx", int)
5153

5254

53-
@dataclasses.dataclass(frozen=True)
55+
@serde.serde
5456
class Route:
5557
"""
5658
A Route (e.g. 3 RIT Inn).
@@ -64,7 +66,7 @@ class Route:
6466
RouteIdx = NewType("RouteIdx", int)
6567

6668

67-
@dataclasses.dataclass(frozen=True)
69+
@serde.serde
6870
class Period:
6971
"""
7072
A Period (e.g. Spring 2025 Shuttle Schedule).
@@ -76,21 +78,21 @@ class Period:
7678
PeriodIdx = NewType("PeriodIdx", int)
7779

7880

79-
@dataclasses.dataclass(frozen=True)
81+
@serde.serde
8082
class Ast:
8183
"""
8284
A cohesive collection of Stops, Routes, Periods, SubPeriods, and TimeTables.
8385
"""
8486

85-
routes: Sequence[Route]
86-
stops: Sequence[Stop]
87-
time_tables: Sequence[TimeTable]
87+
routes: list[Route]
88+
stops: list[Stop]
89+
time_tables: list[TimeTable]
8890

89-
periods: Sequence[Period]
90-
sub_periods: Sequence[SubPeriod]
91+
periods: list[Period]
92+
sub_periods: list[SubPeriod]
9193

92-
route_stops: dict[RouteIdx, Sequence[StopIdx]]
94+
route_stops: dict[RouteIdx, list[StopIdx]]
9395
route_time_table: dict[RouteIdx, TimeTableIdx]
9496

95-
period_to_sub_periods: dict[PeriodIdx, Sequence[SubPeriodIdx]]
96-
sub_period_routes: dict[SubPeriodIdx, Sequence[RouteIdx]]
97+
period_to_sub_periods: dict[PeriodIdx, list[SubPeriodIdx]]
98+
sub_period_routes: dict[SubPeriodIdx, list[RouteIdx]]

yass/parse.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ def finish(self) -> Ast:
7777
time_tables=self.time_tables,
7878
periods=self.periods,
7979
sub_periods=self.sub_periods,
80-
route_stops=self.route_stops, # type: ignore
80+
route_stops=self.route_stops,
8181
route_time_table=self.route_time_table,
82-
period_to_sub_periods=self.period_to_sub_periods, # type: ignore
83-
sub_period_routes=self.sub_period_routes, # type: ignore
82+
period_to_sub_periods=self.period_to_sub_periods,
83+
sub_period_routes=self.sub_period_routes,
8484
)
8585

8686

0 commit comments

Comments
 (0)