Conversation
The unpatchers in _patch only restored the original value when it was truthy and deleted the attribute otherwise, so patching an attribute whose value was 0, False, None or an empty string removed it once the test finished. Use the restore flag that patch_attribute already computes.
|
@oxo42 has imported this pull request. If you are a Meta employee, you can view this in D122127123. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What:
Unpatching now restores attributes whose original value was falsy.
Why:
The unpatchers in
testslide/core/patch.pydecided whether to restore or delete based on the truthiness of the saved value. Patching something likeSomeClass.count = 0or a module levelFLAG = Falsewithpatch_attribute()deleted the attribute when the test finished, which then breaks every later access to it.How:
Restore when
restoreis set (whichpatch_attribute()already computes from whether the attribute existed), keeping the existing value check so themock_callable()callers behave as before.Risks:
Low. The only behavior change is for attributes that existed with a falsy value.
Checklist:
I ran the
*_testslide.pysuites and the unittest suites locally on Python 3.12, plus ruff, flake8 and mypy. The new example intests/patch_attribute_testslide.pyfails before the change.mock_constructor_testslide.pyhas one failure on 3.12 (can call class methods) andcli_unittestneedstermios, both the same on main.