Skip to content

Handle RuleDelayed in Map over Association expressions#1694

Merged
rocky merged 5 commits intomasterfrom
more_on_fix_map
Feb 16, 2026
Merged

Handle RuleDelayed in Map over Association expressions#1694
rocky merged 5 commits intomasterfrom
more_on_fix_map

Conversation

@mmatera
Copy link
Contributor

@mmatera mmatera commented Feb 12, 2026

One slight change over #1692. Also, I add a comment about differences with WMA output in the case that the Association expression does not have the form Association[__(Rule|RuleDelayed)].

@rocky
Copy link
Member

rocky commented Feb 12, 2026

LGTM. Thanks for the generalization and clarification.

@Li-Xiang-Ideal
Copy link
Contributor

btw now MapAt maps on values for Association[__Rule] only:

if hasattr(replace_element, "head") and replace_element.head is SymbolRule:
new_elements[j] = Expression(
SymbolRule,
replace_element.elements[0],
Expression(f, replace_element.elements[1]),
)

#

if is_association and level.has_form(("Rule", "RuleDeyaled"), 2):
if is_association and level.has_form(("Rule", "RuleDelayed"), 2):
Copy link
Member

@rocky rocky Feb 12, 2026

Choose a reason for hiding this comment

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

This mistake wasn't caught in the existing tests. So, probably a test needs to be added here for the new features added.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The question is, what should we test? For the full behavior, I have in mind a bunch of tests, that in the current implementation would not pass. The example in the comment is one of them.
To match with the WMA behavior, this method should be rewritten, and probably it would be also be interesting to define a specific class (like ListExpression) to store valid Association expressions. None of this would be too complicated, but right now I do not have the time to handle it.
What I could do is to list the tests here, or just add the tests to a pytest module, and mark them as xfail.

Copy link
Member

Choose a reason for hiding this comment

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

The question is, what should we test?

So you are saying that nothing in this PR right now benefits a user? There is promise, though, that one day it will?

For the full behavior, I have in mind a bunch of tests, that in the current implementation would not pass. The example in the comment is one of them. To match with the WMA behavior, this method should be rewritten, and probably it would be also be interesting to define a specific class (like ListExpression) to store valid Association expressions. None of this would be too complicated, but right now I do not have the time to handle it. What I could do is to list the tests here, or just add the tests to a pytest module, and mark them as xfail.

Yes, it is better to add xfail or commented-out tests now while the issue is fresh in your mind rather than do it later. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The question is, what should we test?

So you are saying that nothing in this PR right now benefits a user? There is promise, though, that one day it will?

For the full behavior, I have in mind a bunch of tests, that in the current implementation would not pass. The example in the comment is one of them. To match with the WMA behavior, this method should be rewritten, and probably it would be also be interesting to define a specific class (like ListExpression) to store valid Association expressions. None of this would be too complicated, but right now I do not have the time to handle it. What I could do is to list the tests here, or just add the tests to a pytest module, and mark them as xfail.

Yes, it is better to add xfail or commented-out tests now while the issue is fresh in your mind rather than do it later. Thanks.

This PR improves handling of some typical cases, but to cover the most general situation, we need to reimplement this.

Copy link
Member

Choose a reason for hiding this comment

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

For the full behavior, I have in mind a bunch of tests, that in the current implementation would not pass. The example in the comment is one of them. To match with the WMA behavior, this method should be rewritten, and probably it would be also be interesting to define a specific class (like ListExpression) to store valid Association expressions. None of this would be too complicated, but right now I do not have the time to handle it. What I could do is to list the tests here, or just add the tests to a pytest module, and mark them as xfail.

Yes, it is better to add xfail or commented-out tests now while the issue is fresh in your mind rather than do it later. Thanks.

This PR improves handling of some typical cases, but to cover the most general situation, we need to reimplement this.

It occurs to me that it might be good to open an issue with this information, so we don't forget after this PR is merged. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just opened a related issue #1696, with the basic content of this discussion.

"Map[F, Association[a->1, b:>Association[p->3,q->4]], {0}]",
None,
"F[Association[a->1, b:>Association[p->3, q->4]]]",
"Special behavior happends at the first level.",
Copy link
Member

Choose a reason for hiding this comment

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

happends -> occurs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

"Map[F, Association[a->1, b:>Association[p->3,q->4]], {1}]",
None,
"Association[a->F[1], b:>F[Association[p->3, q->4]]]",
"Special behavior happends at the first level.",
Copy link
Member

Choose a reason for hiding this comment

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

happends -> occurs

"Map[F, Association[a->1, b:>Q[p->3, q->4]], {2}]",
None,
"Association[a->1, b:>Q[F[p->3],F[q->4]]]",
"Special behavior happends at the first level.",
Copy link
Member

Choose a reason for hiding this comment

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

happends -> occurs

@mmatera
Copy link
Contributor Author

mmatera commented Feb 12, 2026

btw now MapAt maps on values for Association[__Rule] only:

if hasattr(replace_element, "head") and replace_element.head is SymbolRule:
new_elements[j] = Expression(
SymbolRule,
replace_element.elements[0],
Expression(f, replace_element.elements[1]),
)

thanks for the observation!

@rocky
Copy link
Member

rocky commented Feb 16, 2026

Let's merge. If there are other changes that can be done in another PR.

@rocky rocky merged commit 4ec3e0b into master Feb 16, 2026
21 checks passed
@rocky rocky deleted the more_on_fix_map branch February 16, 2026 17:13
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