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 SwigPtrView.__getattr__ #2259

Merged
merged 5 commits into from
Jan 6, 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
5 changes: 4 additions & 1 deletion python/sdist/amici/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@

:returns: value
"""
return self.__getitem__(item)
try:
return self.__getitem__(item)
except KeyError as e:

Check warning on line 85 in python/sdist/amici/numpy.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/numpy.py#L85

Added line #L85 was not covered by tests
raise AttributeError(item) from e

def __init__(self, swigptr):
"""
Expand Down
25 changes: 25 additions & 0 deletions python/tests/test_swig_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import copy
import numbers

import pytest

import amici
import numpy as np

Expand Down Expand Up @@ -500,3 +502,26 @@ def test_model_is_deepcopyable(pysb_example_presimulation_module):
assert model1.t0() == model2.t0()
model2.setT0(100 + model2.t0())
assert model1.t0() != model2.t0()


def test_rdataview(sbml_example_presimulation_module):
"""Test some SwigPtrView functionality via ReturnDataView."""
model_module = sbml_example_presimulation_module
model = model_module.getModel()
rdata = amici.runAmiciSimulation(model, model.getSolver())
assert isinstance(rdata, amici.ReturnDataView)

# fields are accessible via dot notation and [] operator,
# __contains__ and __getattr__ are implemented correctly
with pytest.raises(AttributeError):
_ = rdata.nonexisting_attribute

with pytest.raises(KeyError):
_ = rdata["nonexisting_attribute"]

assert not hasattr(rdata, "nonexisting_attribute")
assert "x" in rdata
assert rdata.x == rdata["x"]

# field names are included by dir()
assert "x" in dir(rdata)