From 02a797d7422c7397b7e8742b999373f367c5b071 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Wed, 14 Sep 2022 14:55:01 -0700 Subject: [PATCH] [internal] remove `_unfreeze_instance` and replace with `with x._unfrozen()` (#16871) Following #16863, this removes other uses of `_unfreeze_instance` and replaces them with the `_unfrozen()` context manager --- src/python/pants/engine/target.py | 11 +++++------ src/python/pants/util/meta.py | 4 ---- src/python/pants/util/meta_test.py | 10 ++++------ 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/python/pants/engine/target.py b/src/python/pants/engine/target.py index 2238c0d359b..b77b0c0ad19 100644 --- a/src/python/pants/engine/target.py +++ b/src/python/pants/engine/target.py @@ -239,12 +239,11 @@ def __init__(self, raw_value: Optional[Any], address: Address) -> None: super().__init__(raw_value, address) # We must temporarily unfreeze the field, but then we refreeze to continue avoiding # subclasses from adding arbitrary fields. - self._unfreeze_instance() # type: ignore[attr-defined] - # N.B.: We store the address here and not in the Field base class, because the memory usage - # of storing this value in every field was shown to be excessive / lead to performance - # issues. - self.address = address - self._freeze_instance() # type: ignore[attr-defined] + with self._unfrozen(): # type: ignore[attr-defined] + # N.B.: We store the address here and not in the Field base class, because the memory usage + # of storing this value in every field was shown to be excessive / lead to performance + # issues. + self.address = address def __repr__(self) -> str: return ( diff --git a/src/python/pants/util/meta.py b/src/python/pants/util/meta.py index 939b27a0c79..1cf26e88e52 100644 --- a/src/python/pants/util/meta.py +++ b/src/python/pants/util/meta.py @@ -150,9 +150,6 @@ def frozen_after_init(cls: C) -> C: def freeze_instance(self) -> None: self._is_frozen = True - def unfreeze_instance(self) -> None: - self._is_frozen = False - @contextmanager def unfrozen(self) -> Iterator: old_is_frozen = self._is_frozen @@ -176,7 +173,6 @@ def new_setattr(self, key: str, value: Any) -> None: prev_setattr(self, key, value) # type: ignore[call-arg] cls._freeze_instance = freeze_instance - cls._unfreeze_instance = unfreeze_instance cls._unfrozen = unfrozen cls.__init__ = new_init cls.__setattr__ = new_setattr # type: ignore[assignment] diff --git a/src/python/pants/util/meta_test.py b/src/python/pants/util/meta_test.py index a784a9a581d..65a209e6373 100644 --- a/src/python/pants/util/meta_test.py +++ b/src/python/pants/util/meta_test.py @@ -256,10 +256,9 @@ def __init__(self, x: int) -> None: with pytest.raises(FrozenInstanceError): test.y = "abc" # type: ignore[attr-defined] - test._unfreeze_instance() # type: ignore[attr-defined] - test.y = "abc" # type: ignore[attr-defined] + with test._unfrozen(): # type: ignore[attr-defined] + test.y = "abc" # type: ignore[attr-defined] - test._freeze_instance() # type: ignore[attr-defined] with pytest.raises(FrozenInstanceError): test.z = "abc" # type: ignore[attr-defined] @@ -274,10 +273,9 @@ def __init__(self, x: int) -> None: with pytest.raises(FrozenInstanceError): setattr(test, "x", 1) - test._unfreeze_instance() # type: ignore[attr-defined] - setattr(test, "x", 1) + with test._unfrozen(): # type: ignore[attr-defined] + setattr(test, "x", 1) - test._freeze_instance() # type: ignore[attr-defined] with pytest.raises(FrozenInstanceError): test.y = "abc" # type: ignore[attr-defined]