From f0b636a1080274fb487bf5ae87a78b7a5d418185 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 24 Sep 2026 01:52:46 -0300 Subject: [PATCH] Restore falsy attribute values on unpatch 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. --- tests/patch_attribute_testslide.py | 6 ++++++ testslide/core/patch.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/patch_attribute_testslide.py b/tests/patch_attribute_testslide.py index a15b13d..c3f9d07 100644 --- a/tests/patch_attribute_testslide.py +++ b/tests/patch_attribute_testslide.py @@ -217,3 +217,9 @@ def patch_attribute_passes_for_private_with_allow_private(self): self.patch_attribute( sample_module.SomeClass, "_private_attr", "notsoprivate", allow_private=True ) + + @context.example + def unpatching_restores_falsy_values(self): + self.patch_attribute(sample_module.SomeUnhashableClass, "class_attr", 1) + unpatch_all_mocked_attributes() + self.assertEqual(sample_module.SomeUnhashableClass.class_attr, 0) diff --git a/testslide/core/patch.py b/testslide/core/patch.py index 98d0566..8d3d0e9 100644 --- a/testslide/core/patch.py +++ b/testslide/core/patch.py @@ -96,7 +96,7 @@ def _patch( setattr(type(target), attribute, property(fget=lambda _: new_value)) def unpatcher() -> None: - if restore_value: + if restore or restore_value: setattr(type(target), attribute, original_property) else: delattr(target, attribute) @@ -105,7 +105,7 @@ def unpatcher() -> None: setattr(target, attribute, new_value) def unpatcher() -> None: - if restore_value: + if restore or restore_value: setattr(target, attribute, restore_value) else: