Skip to content

Tighten up type of Deploy._process_exports - #155

Merged
rswarbrick merged 1 commit into
lowRISC:masterfrom
rswarbrick:process-exports-type
Apr 6, 2026
Merged

rswarbrick merged 1 commit into
lowRISC:masterfrom
rswarbrick:process-exports-type

Conversation

@rswarbrick

@rswarbrick rswarbrick commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Firstly, the stricter return type is needed because the CovMerge constructor updates self.exports (which is generated with that function). We also know the key and value types, so we may as well give them as well.

The rest of the change is more complicated and a little ugly (we're confusing Pyright on purpose!). The trick is to pass the value that we wrote to self.exports from the hjson file as an argument to _process_exports. Doing it like this avoids Pyright trying to check that the field is both a list of dictionaries and a merged dictionary.

Comment thread src/dvsim/job/deploy.py Outdated
exports_list: object = self.exports
if not isinstance(exports_list, list):
raise TypeError("Loaded exports value should be a list of dicts.")
self.exports: dict[str, str] = self._process_exports(exports_list)

@AlexJones0 AlexJones0 Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable - below I've suggested an alternative pattern which may feel like a bit less of a dance around the type checker, but I'll leave it up to you if you prefer this or your existing solution:

def __init__(self, ...):
    self.exports: list[dict[str, str]] = []
    self.merged_exports: dict[str, str] = {}
    # ... self._define_attrs() then set_attrs, check_attrs, subst_vars, etc. ...
    self.merged_exports = self._process_exports(self.exports)
    self.exports.clear()  # if we're being really paranoid about memory usage (likely unnecessary)
    # ...

def get_job_spec(self) -> "JobSpec":
    # ...
        exports=self.merged_exports,
    # ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's dramatically nicer than what I had! Something like this?

Comment thread src/dvsim/job/deploy.py Resolved
Firstly, the stricter return type is needed because the CovMerge
constructor updates self.exports (which is generated with that
function). We also know the key and value types, so we may as well
give them as well.

For the rest of the change, we avoid making the type of Deploy.exports
change through the run, by defining a new "merged_exports" field to
use instead. This allows the type checker to reason more sensibly
about what's going on.

Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
@rswarbrick
rswarbrick force-pushed the process-exports-type branch from 229aa53 to c470bc7 Compare April 3, 2026 22:33
@rswarbrick
rswarbrick requested a review from AlexJones0 April 3, 2026 22:33
@rswarbrick
rswarbrick added this pull request to the merge queue Apr 6, 2026
Merged via the queue into lowRISC:master with commit ed6beb9 Apr 6, 2026
6 checks passed
@rswarbrick
rswarbrick deleted the process-exports-type branch April 6, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants