From 9d0cb6096151d6882bb837aef4eed13c7285c8e7 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 16 Nov 2026 15:19:08 +0100 Subject: [PATCH] CI: don't leak PySB symbols from tests `pysb.SelfExporter.cleanup()` does not remove a model exported under an explicit name, e.g. `pysb.Model("test_model")`. The model remained in the test module's globals, and a later test defining a model of the same name failed with `SymbolExistsWarning`. Replace the duplicated `uses_pysb` fixtures with a shared `clean_pysb_exports` fixture that also removes the named model and fails if other exported PySB symbols remain. Co-Authored-By: Claude Opus 5.5 --- tests/conftest.py | 45 +++++++++++++++++++++++++++++++++++++ tests/v1/test_model_pysb.py | 15 +++---------- tests/v2/test_lint.py | 13 ++--------- 3 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..8c2ff0ce --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,45 @@ +import pytest + + +def _cleanup_pysb_exports(): + """Remove all symbols exported by :class:`pysb.SelfExporter`. + + :meth:`pysb.SelfExporter.cleanup` does not remove a model that was + exported under an explicit name, so remove that one first. + Fails if any other exported PySB symbols remain in the target namespace. + """ + import pysb + + exporter = pysb.SelfExporter + namespace = exporter.target_globals + module = exporter.target_module + model = exporter.default_model + if model is not None and namespace.get(model.name) is model: + del namespace[model.name] + exporter.cleanup() + + if namespace is None: + return + + # objects bound under their own name were exported by `SelfExporter` + leaked = [ + name + for name, value in namespace.items() + if isinstance(value, pysb.Model | pysb.Component) + and value.name == name + ] + for name in leaked: + del namespace[name] + assert not leaked, f"Leaked PySB symbols in {module}: {leaked}" + + +@pytest.fixture +def clean_pysb_exports(): + """Clean up PySB auto-exported symbols before and after the test. + + Yields the cleanup function for use within the test, e.g., before + defining another model. + """ + _cleanup_pysb_exports() + yield _cleanup_pysb_exports + _cleanup_pysb_exports() diff --git a/tests/v1/test_model_pysb.py b/tests/v1/test_model_pysb.py index a3f9ea77..8abb7604 100644 --- a/tests/v1/test_model_pysb.py +++ b/tests/v1/test_model_pysb.py @@ -1,7 +1,6 @@ """Test related to petab.models.model_pysb""" import pysb -import pytest from petab.v1.models.pysb_model import ( PySBModel, @@ -10,14 +9,6 @@ ) -@pytest.fixture(scope="function") -def uses_pysb(): - """Cleanup PySB auto-exported symbols before and after test""" - pysb.SelfExporter.cleanup() - yield () - pysb.SelfExporter.cleanup() - - def test_parse_species_name(): assert parse_species_name("cyclin(Y='U', b=None)") == [ ("cyclin", None, {"Y": "U", "b": None}) @@ -49,7 +40,7 @@ def test_parse_species_name(): # TODO: MultiState -def test_pysb_model(uses_pysb): +def test_pysb_model(clean_pysb_exports): model = pysb.Model() pysb.Compartment("c1") pysb.Monomer("A") @@ -75,7 +66,7 @@ def test_pysb_model(uses_pysb): assert petab_model.is_state_variable("A(s='a')") is False -def test_pattern_parsing(uses_pysb): +def test_pattern_parsing(clean_pysb_exports): model = pysb.Model() c1 = pysb.Compartment("c1") A = pysb.Monomer("A") @@ -90,7 +81,7 @@ def test_pattern_parsing(uses_pysb): assert str(pattern) == str(pattern_from_string("B(s='a') ** c1", model)) -def test_pysb_model_repr(uses_pysb): +def test_pysb_model_repr(clean_pysb_exports): model = pysb.Model(name="test") petab_model = PySBModel(model) assert repr(petab_model) == "" diff --git a/tests/v2/test_lint.py b/tests/v2/test_lint.py index 65d4796f..25d2440a 100644 --- a/tests/v2/test_lint.py +++ b/tests/v2/test_lint.py @@ -3,7 +3,6 @@ from copy import deepcopy import pysb -import pytest from petab.v2 import Problem from petab.v2.lint import * @@ -11,14 +10,6 @@ from petab.v2.models.sbml_model import SbmlModel -@pytest.fixture -def uses_pysb(): - """Cleanup PySB auto-exported symbols before and after test""" - pysb.SelfExporter.cleanup() - yield () - pysb.SelfExporter.cleanup() - - def test_check_experiments(): """Test ``CheckExperimentTable``.""" problem = Problem() @@ -121,7 +112,7 @@ def test_validate_initial_change_symbols(): assert "contains additional symbols: {'p2'}" in error.message -def test_check_mapping_table(uses_pysb): +def test_check_mapping_table(clean_pysb_exports): """Test checks related to the mapping table.""" problem = Problem() @@ -155,7 +146,7 @@ def test_check_mapping_table(uses_pysb): assert check.run(problem) is None # Invalid: petabEntityId is referenced in the model - pysb.SelfExporter.cleanup() + clean_pysb_exports() pysb_model_invalid = pysb.Model("test_model_invalid") pysb.Monomer("A_") pysb.Initial(A_() ** pysb.Compartment("C"), pysb.Parameter("A", 1))