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 the GPR copy in Python 3.13 #1412

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Fixes

Fixes failures of GPR.copy() in Python 3.13.

## Other

## Deprecated features
Expand Down
8 changes: 6 additions & 2 deletions src/cobra/core/gene.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,13 @@ def to_string(self, names: dict = None) -> str:
"""
return self._ast2str(self, names=names)

def copy(self):
def copy(self) -> "GPR":
"""Copy a GPR."""
return deepcopy(self)
cls = type(self)
gpr = cls()
gpr._genes = deepcopy(self._genes)
gpr.body = deepcopy(self.body)
return gpr

def __copy__(self) -> "GPR":
"""Ensure a correct shallow copy."""
Expand Down
15 changes: 10 additions & 5 deletions tests/test_core/test_gpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ def test_gpr() -> None:
assert len(gpr1.genes) == 0


def test_grp_copy() -> None:
"""Test that copying a GPR works."""
gpr1 = GPR.from_string("(A and B) or C")
gpr2 = gpr1.copy()
assert gpr1 == gpr2
assert id(gpr1.body) is not id(gpr2.body)
assert gpr1._genes == gpr2._genes
assert id(gpr1._genes) is not id(gpr2._genes)


@pytest.mark.parametrize("test_input", ["", "", None])
def test_empty_gpr(test_input) -> None:
"""Test empty GPR."""
Expand Down Expand Up @@ -89,7 +99,6 @@ def test_and_gpr(gpr_input, num_genes, gpr_genes, gpr_output_string) -> None:
for ko_genes in powerset_ne(gpr_genes):
assert not gpr1.eval(ko_genes)
assert gpr1.body
gpr1.copy()


def all_except_one(iterable: Iterable[str]) -> Iterator[Tuple[str, ...]]:
Expand Down Expand Up @@ -132,7 +141,6 @@ def test_or_gpr(
assert gpr1.eval(ko_genes)
assert not gpr1.eval(gpr_genes)
assert gpr1.body
gpr1.copy()


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


@pytest.mark.parametrize(
Expand All @@ -185,7 +192,6 @@ def test_gpr_from_ast_or(
for ko_genes in all_except_one(gpr_genes):
assert gpr1.eval(ko_genes)
assert not gpr1.eval(gpr_genes)
gpr1.copy()


@pytest.mark.parametrize(
Expand All @@ -210,7 +216,6 @@ def test_gpr_from_ast_and(
assert gpr1.eval()
for ko_genes in powerset_ne(gpr_genes):
assert not gpr1.eval(ko_genes)
gpr1.copy()


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