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

[FIX] beesdoo_shift_swap: Prevent a skip of subsequent items #534

Merged
merged 2 commits into from
Nov 1, 2024

Conversation

carmenbianca
Copy link
Collaborator

Description

Modifying a list while looping over it is not allowed. It causes subsequent items to be skipped.

I suspect that this is the cause of a bug where a person who wants to swap a shift is correctly unsubscribed from their old shift, but not subscribed to their new shift. But I have not been able to verify this.

It is not clear to me why this code exists.

Odoo task (if applicable)

T12851

Checklist before approval

  • Tests are present (or not needed).
  • Credits/copyright have been changed correctly.
  • Change log snippet is present.
  • (If a new module) Moving this to OCA has been considered.

Copy link
Collaborator

@polchampion polchampion left a comment

Choose a reason for hiding this comment

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

Successful functional test on fhmunich-test: no side-effect detected

@polchampion
Copy link
Collaborator

@remytms can you review?

Copy link
Member

@huguesdk huguesdk left a comment

Choose a reason for hiding this comment

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

this change seems correct to me, even though i don’t know much about the code.

@huguesdk
Copy link
Member

/ocabot merge patch

@github-grap-bot
Copy link
Contributor

Hey, thanks for contributing! Proceeding to merge this for you.
Prepared branch 12.0-ocabot-merge-pr-534-by-huguesdk-bump-patch, awaiting test results.

github-grap-bot added a commit that referenced this pull request Sep 24, 2024
Signed-off-by huguesdk
@github-grap-bot
Copy link
Contributor

@huguesdk your merge command was aborted due to failed check(s), which you can inspect on this commit of 12.0-ocabot-merge-pr-534-by-huguesdk-bump-patch.

After fixing the problem, you can re-issue a merge command. Please refrain from merging manually as it will most probably make the target branch red.

Copy link
Member

@huguesdk huguesdk left a comment

Choose a reason for hiding this comment

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

@carmenbianca some tests are failing but i don’t know whether it’s because of the change. would you please check this?

Modifying a list while looping over it is not allowed. It causes
subsequent items to be skipped.

I suspect that this is the cause of a bug where a person who wants to
swap a shift is correctly unsubscribed from their old shift, but not
subscribed to their new shift. But I have not been able to verify this.

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
@carmenbianca
Copy link
Collaborator Author

@huguesdk fixed the test

@huguesdk
Copy link
Member

@carmenbianca while trying to understand the code, i changed it a little to make it more obvious. what do you think?

@carmenbianca
Copy link
Collaborator Author

Your proposal is not at all easy to ready for me, sorry :(

I think my code is easier to read, although it does require a comment for clarity. I do like one change you made, though. I think we should rename changes to to_process.

@huguesdk
Copy link
Member

with the previous code, i couldn’t understand at first why elements had to be removed from the list, and thus, why the list which is iterated over must be a copy. i think it was confusing for you too at first, which is probably why you simply removed the code removing the elements.

it is only after debugging the tests that i understood that changes is used across iterations on shifts, which is why it had to be updated to contain only the elements that still need to be processed.

what bothers me with the previous code is the use of list.remove(), which will have to search for the element in the list, while we’re already iterating over the elements. that’s why i think it makes more sense to construct a new list with all not-yet-processed elements along the way. what do you think about this? how would you make that code more clear?

@carmenbianca
Copy link
Collaborator Author

@huguesdk I gave it another go, using sets. I added a rationale to the commit message.

Copy link
Member

@huguesdk huguesdk left a comment

Choose a reason for hiding this comment

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

i’m sorry but the part of my brain that likes performance hurts when reading this, because of 2 reasons:

  1. the set is sorted in each iteration on the shifts instead of just once.
  2. the set of processed changes are removed from the set, which is correct but much less performant (in a theoretical way) than just building a new list along the way.

sets are cool to ensure elements are there only once, but to iterate over a collection in a certain order, lists are better.

i’ve searched for recommended ways of removing elements of a list while iterating over it, and the answers suggest to use a list comprehension and either create a new list, or assign the new elements to a slice of the list. i added a commit doing that. what do you think?

Copy link
Collaborator Author

@carmenbianca carmenbianca left a comment

Choose a reason for hiding this comment

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

@huguesdk I like this proposal. One comment. Good to merge also if you disagree with my comment.

changes -= processed
# process the remaining changes and update the list to contain
# only the changes that have not be processed yet.
changes[:] = (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is not clear to me if [:] is needed. Can't you do a list comprehension instead of a generator? i.e. replace (rec for rec in ...) with [rec for rec in ...]

Copy link
Member

Choose a reason for hiding this comment

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

you’re right. this makes sense only if the list is really big and making a copy of it should be avoided. i’ve changed it.

@huguesdk
Copy link
Member

huguesdk commented Nov 1, 2024

/ocabot merge patch

@github-grap-bot
Copy link
Contributor

Hey, thanks for contributing! Proceeding to merge this for you.
Prepared branch 12.0-ocabot-merge-pr-534-by-huguesdk-bump-patch, awaiting test results.

@github-grap-bot github-grap-bot merged commit ddcfbef into 12.0 Nov 1, 2024
4 checks passed
@github-grap-bot github-grap-bot deleted the 12.0-remove-skip branch November 1, 2024 10:42
@github-grap-bot
Copy link
Contributor

Congratulations, your PR was merged at c894f16. Thanks a lot for contributing to beescoop. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants