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

fix: Use a context manager for opening files #219

Conversation

qartik
Copy link
Member

@qartik qartik commented Aug 23, 2024

Description

ruff pointed out errors like the following:

pytket/phir/api.py:94:25: SIM115 Use a context manager for opening files
   |
92 |     if wasm_bytes:
93 |         try:
94 |             qasm_file = NamedTemporaryFile(suffix=".qasm", delete=False)
   |                         ^^^^^^^^^^^^^^^^^^ SIM115
95 |             wasm_file = NamedTemporaryFile(suffix=".wasm", delete=False)
96 |             qasm_file.write(qasm.encode())
   |

Turns out resolving these also fixes #108.

Additionally:

  • fixes warning "DeprecationWarning: The auto_rebase_pass() method is deprecated and will be removed after pytket 1.33, please use AutoRebase instead."
  • update deps

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the changelog with any user-facing changes

wasm_file = NamedTemporaryFile(suffix=".wasm", delete=False)
with (
NamedTemporaryFile(suffix=".qasm", delete=False) as qasm_file,
NamedTemporaryFile(suffix=".wasm", delete=False) as wasm_file,
Copy link
Member Author

@qartik qartik Aug 23, 2024

Choose a reason for hiding this comment

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

It's not obvious to me if we need delete=False anymore. See https://docs.python.org/3.11/library/tempfile.html#tempfile.NamedTemporaryFile

On Python 3.12 onward, there is a new parameter called delete_on_close that is preferable here:

Therefore to use the name of the temporary file to reopen the file after closing it, either make sure not to delete the file upon closure (set the delete parameter to be false) or, in case the temporary file is created in a with statement, set the delete_on_close parameter to be false.

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps @neal-erickson might have insight about why delete=False was needed?

cqc-alec
cqc-alec previously approved these changes Aug 23, 2024
@qartik
Copy link
Member Author

qartik commented Aug 26, 2024

Hi @peter-campora, would it possible for you to try this branch on Windows?

@neal-erickson shared:

the reason I wasn't using a context manager was because it seemed to be broken on Windows. I don't remember the exact details, I think I had Pete test something for me. But this commit where I changed it from a context manager to the try/finally makes me think there's a good reason.

f2e72af (#77)

But maybe you can work with him to make sure things are working on Windows?

I do see the CI on windows is passing fully, so I am unsure if there is any problem still unfixed on Windows.

@peter-campora
Copy link
Collaborator

I forget what the status of python tooling natively in my windows environment is exactly, but I can take a look if still desired that I do some testing

@qartik
Copy link
Member Author

qartik commented Aug 27, 2024

I forget what the status of python tooling natively in my windows environment is exactly, but I can take a look if still desired that I do some testing

If you don't mind, it will be helpful to eliminate the possibility that this is still an issue.

@qartik
Copy link
Member Author

qartik commented Aug 30, 2024

Going to merge this. If it still fails on Windows, we will fix it before the next release.

@qartik qartik merged commit 37c412f into main Aug 30, 2024
6 checks passed
@qartik qartik deleted the 108-test_pytket_with_wasm-fails-when-run-after-test_pytket_to_phir_no_machine_at_all branch August 30, 2024 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test_pytket_with_wasm fails when run after test_pytket_to_phir_no_machine_at_all
3 participants