Skip to content

Commit

Permalink
wrapper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcapriot committed Oct 10, 2024
1 parent 4e9ebed commit eefd9fb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pymatsolver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@

# Simple solvers
from .solvers import Diagonal, Triangle, Forward, Backward
from .wrappers import WrapDirect
from .wrappers import WrapIterative
from .wrappers import wrap_direct, WrapDirect
from .wrappers import wrap_iterative, WrapIterative

# Scipy Iterative solvers
from .iterative import SolverCG
Expand Down
11 changes: 8 additions & 3 deletions pymatsolver/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def _valid_kwargs_for_func(func, **kwargs):
sig.bind_partial(**{key: value})
valid_kwargs[key] = value
except TypeError:
warnings.warn(f'Unused keyword argument "{key}" for {func.__name__}.', stacklevel=3)
warnings.warn(f'Unused keyword argument "{key}" for {func.__name__}.', UserWarning, stacklevel=3)
# stack level of three because we want the warning issued at the call
# to the wrapped solver's `__init__` method.
return valid_kwargs


def WrapDirect(fun, factorize=True, name=None):
def wrap_direct(fun, factorize=True, name=None):
"""Wraps a direct Solver.
Parameters
Expand Down Expand Up @@ -121,6 +121,7 @@ def clean(self):
"__init__": __init__,
"_solve_single": _solve_single,
"_solve_multiple": _solve_multiple,
"kwargs": kwargs,
"clean": clean,
}
)
Expand All @@ -146,7 +147,7 @@ def clean(self):
return WrappedClass


def WrapIterative(fun, check_accuracy=None, accuracy_tol=None, name=None):
def wrap_iterative(fun, check_accuracy=None, accuracy_tol=None, name=None):
"""
Wraps an iterative Solver.
Expand Down Expand Up @@ -229,6 +230,7 @@ def _solve_multiple(self, rhs):
"__init__": __init__,
"_solve_single": _solve_single,
"_solve_multiple": _solve_multiple,
"kwargs": kwargs,
}
)
WrappedClass.__doc__ = f"""Wrapped {class_name} solver.
Expand All @@ -253,3 +255,6 @@ def _solve_multiple(self, rhs):

return WrappedClass


WrapDirect = wrap_direct
WrapIterative = wrap_iterative
4 changes: 4 additions & 0 deletions tests/test_Basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def _solve_single(self, rhs):
def _solve_multiple(self, rhs):
return rhs

def clean(self):
# this is to test that the __del__ still executes if the object doesn't successfully clean.
raise MemoryError("Nothing to cleanup!")

class NotTransposableIdentitySolver(IdentitySolver):
""" A class that can't be transposed."""

Expand Down
24 changes: 24 additions & 0 deletions tests/test_Wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pymatsolver import SolverCG, SolverLU
import pytest
import scipy.sparse as sp
import warnings


@pytest.mark.parametrize("solver_class", [SolverCG, SolverLU])
def test_wrapper_unused_kwargs(solver_class):
A = sp.eye(10)

with pytest.warns(UserWarning, match="Unused keyword argument.*"):
solver_class(A, not_a_keyword_arg=True)

def test_good_arg_iterative():
# Ensure this doesn't throw a warning!
with warnings.catch_warnings():
warnings.simplefilter("error")
SolverCG(sp.eye(10), rtol=1e-4)

def test_good_arg_direct():
# Ensure this doesn't throw a warning!
with warnings.catch_warnings():
warnings.simplefilter("error")
SolverLU(sp.eye(10, format='csc'), permc_spec='NATURAL')

0 comments on commit eefd9fb

Please sign in to comment.