torch/config: fix mock behaviour (#140779)

Mock only replaces the value that was removed, if after deletion, it
does not see the attribute.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/140779
Approved by: https://github.com/ezyang
This commit is contained in:
Colin L. Rice
2024-11-19 17:16:27 -07:00
committed by PyTorch MergeBot
parent 878a849c92
commit 241d2259d3
2 changed files with 26 additions and 0 deletions

View File

@ -1,6 +1,7 @@
# Owner(s): ["module: unknown"]
import os
import pickle
from unittest.mock import patch
os.environ["ENV_TRUE"] = "1"
@ -320,6 +321,14 @@ torch.testing._internal.fake_config_module._save_config_ignore = ['e_ignored']""
revert()
self.assertTrue(config.e_bool)
def test_unittest_patch(self):
with patch("torch.testing._internal.fake_config_module.e_bool", False):
with patch("torch.testing._internal.fake_config_module.e_bool", False):
self.assertFalse(config.e_bool)
# unittest.mock has some very weird semantics around deletion of attributes when undoing patches
self.assertFalse(config.e_bool)
self.assertTrue(config.e_bool)
if __name__ == "__main__":
run_tests()