Skip to content

Commit

Permalink
[internal] remove _unfreeze_instance and replace with `with x._unfr…
Browse files Browse the repository at this point in the history
…ozen()` (#16871)

Following #16863, this removes other uses of `_unfreeze_instance` and replaces them with the `_unfrozen()` context manager
  • Loading branch information
Christopher Neugebauer authored Sep 14, 2022
1 parent e75db55 commit 02a797d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
11 changes: 5 additions & 6 deletions src/python/pants/engine/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 0 additions & 4 deletions src/python/pants/util/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
10 changes: 4 additions & 6 deletions src/python/pants/util/meta_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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]

Expand Down

0 comments on commit 02a797d

Please sign in to comment.