Skip to content
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

Allow dict(), **, and | to operate on non-dict mappings #13605

Closed
brandjon opened this issue Jun 24, 2021 · 4 comments
Closed

Allow dict(), **, and | to operate on non-dict mappings #13605

brandjon opened this issue Jun 24, 2021 · 4 comments
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel type: feature request

Comments

@brandjon
Copy link
Member

Currently, dict(v) allows v to be either an iterable of key-value pairs, or another dict object whose entries get included into the new dict. We should additionally allow v to be a non-dict mapping. In Bazel an example of such a type would be OutputGroupInfo.

See bazelbuild/starlark#198 for the corresponding spec change.

Implementation wise, following the precedent of StarlarkIterable vs Iterable, we'd probably want to create a marker interface StarlarkMapping, rather than using the standard Java Map interface. StarlarkMapping would extend StarlarkIterable and StarlarkIndexable. Using a marker interface as opposed to Map gives more control to the implementor; in particular, it lets us say that OutputGroupInfo is a mapping without having to implement an extra Java API we don't want it to have. Conversely, it also avoids changing the Starlark semantics of a type whenever its class gains or loses the Map interface.

OutputGroupInfo would be made to implement StarlarkMapping instead of StarlarkIterable and StarlarkIndexable (which it would still inherit indirectly). In contrast, TransitiveInfoCollection would continue to implement StarlarkIndexable without StarlarkIterable, since it does not support enumeration of providers. Other types, e.g. toolchain contexts, may or may not choose to be classified as a mapping.

Implementing StarlarkMapping wouldn't obligate you to support, e.g., efficient enumeration of keys via a view object. It's simply the join of iterable and indexable.

This issue came up in the discussion of Improving native.existing_rules, where it was pointed out that migrating the return value from a dict object to a dict-like view would require any users who wanted to copy the result to do dict(view.items()) rather than simply dict(view). @tetromino fyi.

@brandjon brandjon added type: feature request P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel labels Jun 24, 2021
@mahmoudimus
Copy link

Hi @brandjon - happy to take a stab at a PR to support this if you’re up for it.

@tetromino
Copy link
Contributor

tetromino commented Oct 8, 2021

Another wart we'd want to fix here is ** operator: currently, calls like func(x, y, **kwargs) require kwargs to be a dict, but they ought to support any mapping.


Note that after taking a quick stab at implementing StarlarkMapping using StarlarkNativeModule.DictLikeView as a model, I'm now leaning to having StarlarkMapping extend Map. Too much friction with Dict otherwise.

@tetromino
Copy link
Contributor

... and another wart: #14540 will add the Python 3.9 style union operator for dicts, which the dict-like views will not support.

Thus, I think we need to tackle problem sooner rather than later.

@tetromino tetromino changed the title Allow dict() to take in non-dict mappings Allow dict(), **, and | to operate on non-dict mappings Feb 23, 2022
copybara-service bot pushed a commit that referenced this issue Oct 3, 2022
…y dicts.

In particular, this allows immutable view objects returned by
native.existing_rule/s() with --incompatible_existing_rules_immutable_view to
be used in dict() and dict.update().

Partially addresses #13605

PiperOrigin-RevId: 478527510
Change-Id: Ic796df9d556027c5d797b03ca85aebf1c90165cd
copybara-service bot pushed a commit that referenced this issue Oct 3, 2022
…ry maps, not just dicts

In particular, this allows immutable view objects returned by
native.existing_rule/s() with --incompatible_existing_rules_immutable_view to
be used with | and |=.

Partially addresses #13605

PiperOrigin-RevId: 478547382
Change-Id: I141a4bfb97457c5e3ee435d155b0148c3e5ad3ae
@tetromino
Copy link
Contributor

I believe this should be fixed by cf99f84 + dfa9c62 + 16b82af

aiuto pushed a commit to aiuto/bazel that referenced this issue Oct 12, 2022
…y dicts.

In particular, this allows immutable view objects returned by
native.existing_rule/s() with --incompatible_existing_rules_immutable_view to
be used in dict() and dict.update().

Partially addresses bazelbuild#13605

PiperOrigin-RevId: 478527510
Change-Id: Ic796df9d556027c5d797b03ca85aebf1c90165cd
aiuto pushed a commit to aiuto/bazel that referenced this issue Oct 12, 2022
…ry maps, not just dicts

In particular, this allows immutable view objects returned by
native.existing_rule/s() with --incompatible_existing_rules_immutable_view to
be used with | and |=.

Partially addresses bazelbuild#13605

PiperOrigin-RevId: 478547382
Change-Id: I141a4bfb97457c5e3ee435d155b0148c3e5ad3ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel type: feature request
Projects
None yet
Development

No branches or pull requests

3 participants