Skip to content

Commit

Permalink
Fix recursion error in Formula.__init__ (#1292)
Browse files Browse the repository at this point in the history
* fix init and add tests

* add RNs

* fix formatting
  • Loading branch information
cdiener authored Nov 2, 2022
1 parent b3e0740 commit 108bbec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Correct reading and writing of subsystem in mat.
* General cleanup of code in mat.py
* fix the pandas deprecation warning in `find_external_compartment`
* fix an issue where creating a Formula object would give a recursion error

## Other

Expand Down
2 changes: 1 addition & 1 deletion src/cobra/core/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, formula: Optional[str] = None, **kwargs) -> None:
formula: str, optional
An string that will be parsed as a formula.
"""
super().__init__(self, formula, **kwargs)
super().__init__(id=formula, **kwargs)
self.formula = formula
self.elements = {}
if self.formula is not None:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_core/test_formula.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test functions of formula.py ."""

import pytest

from cobra.core.formula import Formula


def test_formula_init() -> None:
"""Test initialization."""
f = Formula("H2O")
assert "Formula H2O" in repr(f)
f = Formula("H2O", name="Water")
assert f.name == "Water"


@pytest.mark.parametrize(
["formula", "weight"], [["H2O", 18.01528], ["C6H12O6", 180.15588], ["NO3", 62.0049]]
)
def test_formula_weight(formula, weight) -> None:
"""Test molecular weight calculation."""
assert Formula(formula).weight == pytest.approx(weight)


def test_formula_wrong() -> None:
"""Test incorrect formula elements."""
with pytest.warns(UserWarning):
w = Formula("NOX").weight
assert w is None

0 comments on commit 108bbec

Please sign in to comment.