Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[internal] Add context manager to handle temporary unfreezing of frozen_after_init objects #16863

Merged

Conversation

chrisjrn
Copy link
Contributor

This adds a safer way to temporarily unfreeze frozen_after_init objects, for example in tests. The API is not exposed to static typing (just like the existing _unfreeze_instance), but it will re-freeze once finished.

…nit` objects

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]

(cherry picked from commit a6190ec)
@chrisjrn chrisjrn changed the title Add context manager to handle temporary unfreezing of frozen_after_init objects [internal] Add context manager to handle temporary unfreezing of frozen_after_init objects Sep 14, 2022
@chrisjrn chrisjrn added the category:internal CI, fixes for not-yet-released features, etc. label Sep 14, 2022
@chrisjrn
Copy link
Contributor Author

This is needed to strip out plugin fields in environments unit tests in order to do equality testing

@chrisjrn chrisjrn marked this pull request as ready for review September 14, 2022 16:05
@@ -125,6 +126,15 @@ def freeze_instance(self) -> None:
def unfreeze_instance(self) -> None:
self._is_frozen = False

@contextmanager
def unfrozen(self) -> Iterator:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should name this something like unsafe_unfrozen() or unfrozen_for_tests()? I don't think we want this used in production. While pantsbuild/pants contributors will know better, plugin authors might not.

Copy link
Contributor Author

@chrisjrn chrisjrn Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's added as _unfrozen, and you need to do cast(Any, frozen_instance)._unfrozen(), so I don't think there's a good chance that people will find this unless they go looking.

@chrisjrn chrisjrn merged commit 7e266c7 into pantsbuild:main Sep 14, 2022
@chrisjrn chrisjrn deleted the chrisjrn/unfreeze_context_manager branch September 14, 2022 17:25
chrisjrn pushed a commit that referenced this pull request Sep 14, 2022
…ozen()` (#16871)

Following #16863, this removes other uses of `_unfreeze_instance` and replaces them with the `_unfrozen()` context manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:internal CI, fixes for not-yet-released features, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants