Skip to content

Make data.Const comparable with list and dict objects compatible with the layout [0.5 backport] #1421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

whitequark
Copy link
Member

See #1420.

Previously, `KeyError` was raised, which should not be escaping
the implementation (`Layout.const` is not a retrieval method).
Before this commit, `data.Const` was only comparable to `data.Const` or
`data.View`.

After this commit, in addition, it is also comparable to `dict` or
`list` provided that such a value is accepted by `layout.const()` where
`layout` is the layout of the `data.Const` object.

This change greatly reduces boilerplate in tests by enabling e.g.:

    assert (await stream_get(ctx, stream)) == {"value": 1}

instead of:

    assert (await stream_get(ctx, stream)) == Const({"value": 1}, stream.p.shape())

Note that, unlike `Layout.const`, which accepts arbitrary `Mapping` or
`Sequence` objects, only `dict` and `list` are accepted in comparisons.
Also, `data.View` continues to be comparable only to `data.View` and
`data.Const`. This is to minimize the scope of the change and reduce
likelihood of undesirable side effects when backported to the 0.5.x
branch.

Fixes amaranth-lang#1414.
@whitequark whitequark merged commit 579af69 into amaranth-lang:v0.5.x Jun 27, 2024
20 checks passed
@whitequark whitequark deleted the data-const-compare-dict-list-v0.5.x branch June 27, 2024 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant