Skip to content

Commit 0dab3f0

Browse files
committed
Rewrite how OneShotCfg.build_modes gets constructed
This is a bit tricky because self.build_modes comes from parsing an hjson dictionary and is actually a list of dictionaries that describe build modes. These get parsed and converted to BuildMode objects by Mode.create_modes. I've written a rather silly loose check that makes sure the types look reasonable, which is enough to let us convince Pyright to allow the code to claim the correct eventual types. This commit also fixes an incorrect type in the second argument to create_modes. Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
1 parent cc9c1f1 commit 0dab3f0

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

src/dvsim/flow/one_shot.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, flow_cfg_file, hjson_data, args, mk_config) -> None:
5353
self.dut = ""
5454
self.fusesoc_core = ""
5555
self.ral_spec = ""
56-
self.build_modes = []
56+
self.build_modes: Sequence[BuildMode] = []
5757
self.run_modes = []
5858
self.regressions = []
5959
self.max_msg_count = -1
@@ -110,17 +110,34 @@ def _expand(self) -> None:
110110
def _purge(self) -> None:
111111
assert self.scratch_path
112112
log.info("Purging scratch path %s", self.scratch_path)
113-
rm_path(self.scratch_path)
113+
rm_path(Path(self.scratch_path))
114114

115115
def _create_objects(self) -> None:
116-
# Create build and run modes objects
117-
build_modes = Mode.create_modes(BuildMode, self.build_modes)
118-
self.build_modes = build_modes
119-
120-
# All defined build modes are being built, h
121-
# ence extend all with the global opts.
122-
for build_mode in build_modes:
123-
build_mode.build_opts.extend(self.build_opts)
116+
# Create build modes objects based on the names that were stored in
117+
# self.build_modes by the superclass constructor's call to
118+
# _merge_hjson.
119+
#
120+
# We've lied to the type checker about the type of self.build_modes,
121+
# giving it the type that it will contain after this function is
122+
# complete.
123+
mode_dicts: list[dict[object, object]] = []
124+
for mode_dict in self.build_modes:
125+
if not isinstance(mode_dict, dict):
126+
msg = f"Found a build mode item of {mode_dict} when we expected a dict."
127+
raise TypeError(msg)
128+
129+
mode_dicts.append(mode_dict)
130+
131+
base_modes: Sequence[Mode] = Mode.create_modes(BuildMode, mode_dicts)
132+
133+
# All defined build modes are being built, so should be extended with
134+
# the global opts. Tighten up the inferred type at the same time.
135+
self.build_modes = []
136+
for mode in base_modes:
137+
if not isinstance(mode, BuildMode):
138+
raise TypeError("create_modes returned the wrong type")
139+
mode.build_opts.extend(self.build_opts)
140+
self.build_modes.append(mode)
124141

125142
def _print_list(self) -> None:
126143
for list_item in self.list_items:

src/dvsim/modes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def merge_mode(self, mode: "Mode") -> None:
146146
return True
147147

148148
@staticmethod
149-
def create_modes(mode_type: "type[Mode]", mdicts: Mapping) -> Sequence["Mode"]:
149+
def create_modes(mode_type: "type[Mode]", mdicts: Sequence[Mapping]) -> Sequence["Mode"]:
150150
"""Create modes of type mode_type.
151151
152152
Use the given list of raw dicts Process dependencies.

0 commit comments

Comments
 (0)