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

Simplify dev_tools.notebooks.utils.rewrite_notebook #6030

Merged
merged 8 commits into from
Mar 14, 2023

Conversation

pavoljuhas
Copy link
Collaborator

Do not return the OS file descriptor as it was only used for
closing that file in rewrite_notebook callers.
Return only the filename of the rewritten notebook.

Do not return the OS file descriptor, it was only used
to close the file in rewrite_notebook callers.
Return only the filename of the rewritten notebook.
@pavoljuhas pavoljuhas requested review from a team, vtomole and cduck as code owners March 11, 2023 02:11
@pavoljuhas pavoljuhas requested a review from maffoo March 11, 2023 02:11
@CirqBot CirqBot added the size: S 10< lines changed <50 label Mar 11, 2023
@pavoljuhas pavoljuhas added the kind/health For CI/testing/release process/refactoring/technical debt items label Mar 11, 2023
Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

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

LGTM once temp file leak is fixed.

@@ -177,7 +177,7 @@ def test_notebooks_against_released_cirq(partition, notebook_path, cloned_env):

notebook_file = os.path.basename(notebook_path)

rewritten_notebook_descriptor, rewritten_notebook_path = rewrite_notebook(notebook_path)
rewritten_notebook_path = rewrite_notebook(notebook_path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems we are (and have always been) leaking temporary files. Could you fix while you're at it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@viathor - thank you for the review. The temporary files cleanup was a bit more involved - can you please take a quick look again?

@@ -67,7 +67,7 @@ def test_notebooks_against_released_cirq(notebook_path):
notebook_file = os.path.basename(notebook_path)
notebook_rel_dir = os.path.dirname(os.path.relpath(notebook_path, "."))
out_path = f"out/{notebook_rel_dir}/{notebook_file[:-6]}.out.ipynb"
rewritten_notebook_descriptor, rewritten_notebook_path = rewrite_notebook(notebook_path)
rewritten_notebook_path = rewrite_notebook(notebook_path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

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

Thank you for going the extra mile here and fixing the pre-existing leak. I have a suggestion to make the fix a little more robust.

notebook_path.endswith('-rewrite.ipynb')
and os.path.isfile(notebook_path)
and os.path.dirname(notebook_path) == tempfile.gettempdir()
):
Copy link
Collaborator

@viathor viathor Mar 14, 2023

Choose a reason for hiding this comment

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

This implements the pattern wherein we make a decision (here: whether or not we need to rewrite a notebook into a temporary file) and then later try to work out what decision was taken to correlate earlier and later actions. This coding pattern is brittle since it's easy for the code to evolve in a way that breaks perfect correlation between the two decision points.

One way to solve this is to return a bool from rewrite_notebook indicating whether the file is temporary or not. This way we can ensure that the file is deleted if and only if it is temporary while also ensuring that the decision is made once.

If you really dislike two return values you could try to hide the bool in the type by e.g. returning an instance of tempfile.NamedTemporaryFile or a file object (they both have .name attribute I think...). This is fine too as long as it can be made to work (haven't tried).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the feedback - per our offline talk, it is even simpler to have rewrite_notebook always create a temporary file which can be removed without any extra logic.

Done in 92477a7 and 0d5895f.

Not needed if rewrite_notebook always produces temporary files.
Just use os.remove to clean up after rewrite_notebook.
@pavoljuhas pavoljuhas merged commit f636c5f into quantumlib:master Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/health For CI/testing/release process/refactoring/technical debt items size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants