-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Prevent notebook tests from changing the pytest Python environment #6032
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
Prevent notebook tests from changing the pytest Python environment #6032
Conversation
test_notebooks_against_released_cirq should not change the installed packages in the Python pytest environment. Here we expose downgrade of ply to ply==3.4 in the test of interop.ipynb.
Execute papermill and notebook tests in an OS environment that tells pip to install new packages to a temporary directory. `pip install` commands in the notebook should thus not change the environment that runs pytest.
Keep users safe from downgrading their ply package installed with cirq. Related to quantumlib#6032
viathor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a minor comment and a question to confirm the intended behavior.
dev_tools/notebooks/notebook_test.py
Outdated
| packages_before = set((d.name, d.version) for d in importlib.metadata.distributions()) | ||
| yield | ||
| packages_after = set((d.name, d.version) for d in importlib.metadata.distributions()) | ||
| packages_changed = packages_before.difference(packages_after) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorter and more readable:
assert packages_before.issubset(packages_after)or
assert packages_before == packages_afterfor symmetric difference (see other comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this, see the fix below.
dev_tools/notebooks/notebook_test.py
Outdated
| packages_before = set((d.name, d.version) for d in importlib.metadata.distributions()) | ||
| yield | ||
| packages_after = set((d.name, d.version) for d in importlib.metadata.distributions()) | ||
| packages_changed = packages_before.difference(packages_after) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we actually want the symmetric difference here? IOW, don't we also want to prevent tests from changing the environment by installing new packages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Changed in e1d2ada.
Also fail if test_notebooks_against_released_cirq adds any new package. Executions of `pip install` should deploy to a temporary directory.
test_notebooks_against_released_cirqshould not change the installedpackages in the Python pytest environment. Adjust OS environment
so that pip invoked from tested notebooks installs to a temporary directory.
Also add test for changed packages that catches downgrade to ply==3.4
caused by a test of interop.ipynb.