Skip to content

Commit a2b9d00

Browse files
authored
Fix the GPR copy in Python 3.13 (#1412)
* fix GPR copy overwrite * add specific test * add RNs
1 parent 3b599e1 commit a2b9d00

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

release-notes/next-release.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
## Fixes
66

7+
Fixes failures of GPR.copy() in Python 3.13.
8+
79
## Other
810

911
## Deprecated features

src/cobra/core/gene.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,13 @@ def to_string(self, names: dict = None) -> str:
545545
"""
546546
return self._ast2str(self, names=names)
547547

548-
def copy(self):
548+
def copy(self) -> "GPR":
549549
"""Copy a GPR."""
550-
return deepcopy(self)
550+
cls = type(self)
551+
gpr = cls()
552+
gpr._genes = deepcopy(self._genes)
553+
gpr.body = deepcopy(self.body)
554+
return gpr
551555

552556
def __copy__(self) -> "GPR":
553557
"""Ensure a correct shallow copy."""

tests/test_core/test_gpr.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ def test_gpr() -> None:
2727
assert len(gpr1.genes) == 0
2828

2929

30+
def test_grp_copy() -> None:
31+
"""Test that copying a GPR works."""
32+
gpr1 = GPR.from_string("(A and B) or C")
33+
gpr2 = gpr1.copy()
34+
assert gpr1 == gpr2
35+
assert id(gpr1.body) is not id(gpr2.body)
36+
assert gpr1._genes == gpr2._genes
37+
assert id(gpr1._genes) is not id(gpr2._genes)
38+
39+
3040
@pytest.mark.parametrize("test_input", ["", "", None])
3141
def test_empty_gpr(test_input) -> None:
3242
"""Test empty GPR."""
@@ -89,7 +99,6 @@ def test_and_gpr(gpr_input, num_genes, gpr_genes, gpr_output_string) -> None:
8999
for ko_genes in powerset_ne(gpr_genes):
90100
assert not gpr1.eval(ko_genes)
91101
assert gpr1.body
92-
gpr1.copy()
93102

94103

95104
def all_except_one(iterable: Iterable[str]) -> Iterator[Tuple[str, ...]]:
@@ -132,7 +141,6 @@ def test_or_gpr(
132141
assert gpr1.eval(ko_genes)
133142
assert not gpr1.eval(gpr_genes)
134143
assert gpr1.body
135-
gpr1.copy()
136144

137145

138146
@pytest.mark.parametrize(
@@ -158,7 +166,6 @@ def test_complicated_gpr(gpr_input: str) -> None:
158166
assert not gpr1.eval("c")
159167
assert not gpr1.eval(["a", "b"])
160168
assert not gpr1.eval(["a", "b", "c"])
161-
gpr1.copy()
162169

163170

164171
@pytest.mark.parametrize(
@@ -185,7 +192,6 @@ def test_gpr_from_ast_or(
185192
for ko_genes in all_except_one(gpr_genes):
186193
assert gpr1.eval(ko_genes)
187194
assert not gpr1.eval(gpr_genes)
188-
gpr1.copy()
189195

190196

191197
@pytest.mark.parametrize(
@@ -210,7 +216,6 @@ def test_gpr_from_ast_and(
210216
assert gpr1.eval()
211217
for ko_genes in powerset_ne(gpr_genes):
212218
assert not gpr1.eval(ko_genes)
213-
gpr1.copy()
214219

215220

216221
@pytest.mark.parametrize("test_input", [["a", "b"], {"a", "b"}])

0 commit comments

Comments
 (0)