Skip to content

Commit

Permalink
Narrow pytest execution check in deprecation warning (#4032)
Browse files Browse the repository at this point in the history
Instead of depending on `PYTEST_CURRENT_TEST` environment variable introduces `CIRQ_TESTING` to ensure that only cirq tests will see this env var. Otherwise downstream projects will fail when using deprecated cirq objects in their tests.
  • Loading branch information
balopat committed Apr 29, 2021
1 parent 83e0bd3 commit 902e737
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cirq-core/cirq/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def proper_eq(a: Any, b: Any) -> bool:
def _warn_or_error(msg):
from cirq.testing.deprecation import ALLOW_DEPRECATION_IN_TEST

called_from_test = 'PYTEST_CURRENT_TEST' in os.environ
called_from_test = 'CIRQ_TESTING' in os.environ
deprecation_allowed = ALLOW_DEPRECATION_IN_TEST in os.environ
if called_from_test and not deprecation_allowed:
raise ValueError(f"Cirq should not use deprecated functionality: {msg}")
Expand Down
3 changes: 2 additions & 1 deletion cirq-core/cirq/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import inspect

import os
import matplotlib.pyplot as plt


def pytest_configure(config):
# Use matplotlib agg backend which does not require a display.
plt.switch_backend('agg')
os.environ['CIRQ_TESTING'] = "true"


def pytest_pyfunc_call(pyfuncitem):
Expand Down
23 changes: 18 additions & 5 deletions cirq-core/cirq/protocols/json_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import contextlib

import datetime
import importlib
import io
import json
import os
Expand Down Expand Up @@ -407,13 +408,25 @@ def test_internal_serializer_types():
_ = json_serialization._ContextualSerialization._from_json_dict_(**serialization_json)


def _list_public_classes_for_tested_modules():
cirq_google_on_path = importlib.util.find_spec("cirq_google") is not None

ctx_manager = (
cirq.testing.assert_deprecated("cirq.google", deadline="v0.14")
if cirq_google_on_path
else contextlib.suppress()
)
with ctx_manager:
return [
(mod_spec, o, n)
for mod_spec in MODULE_TEST_SPECS
for (o, n) in mod_spec.find_classes_that_should_serialize()
]


@pytest.mark.parametrize(
'mod_spec,cirq_obj_name,cls',
[
(mod_spec, o, n)
for mod_spec in MODULE_TEST_SPECS
for (o, n) in mod_spec.find_classes_that_should_serialize()
],
_list_public_classes_for_tested_modules(),
)
def test_json_test_data_coverage(mod_spec: ModuleJsonTestSpec, cirq_obj_name: str, cls):
if cirq_obj_name == "SerializableByKey":
Expand Down

0 comments on commit 902e737

Please sign in to comment.