Skip to content

Commit

Permalink
api: avoid duplicate deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mloubout committed Sep 23, 2024
1 parent 958720a commit 77e0021
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
15 changes: 15 additions & 0 deletions devito/deprecations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from functools import cached_property
from warnings import warn


class DevitoDeprecation():

@cached_property
def coeff_warn(self):
warn("The Coefficient API is deprecated and will be removed, coefficients should"
"be passed directly to the derivative object `u.dx(weights=...)",
DeprecationWarning, stacklevel=2)
return


deprecations = DevitoDeprecation()
10 changes: 3 additions & 7 deletions devito/finite_differences/coefficients.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from warnings import warn
from devito.deprecations import deprecations

__all__ = ['Coefficient', 'Substitutions']


class Coefficient:
def __init__(self, deriv_order, function, dimension, weights):
warn("The Coefficient API is deprecated and will be removed, coefficients should"
"be passed directly to the derivative object `u.dx(weights=...)",
DeprecationWarning, stacklevel=2)
deprecations.coeff_warn
self._weights = weights
self._deriv_order = deriv_order
self._function = function
Expand Down Expand Up @@ -36,9 +34,7 @@ def weights(self):

class Substitutions:
def __init__(self, *args):
warn("The Coefficient API is deprecated and will be removed, coefficients should"
"be passed directly to the derivative object `u.dx(weights=...)",
DeprecationWarning, stacklevel=2)
deprecations.coeff_warn
if any(not isinstance(arg, Coefficient) for arg in args):
raise TypeError("Non Coefficient object within input")

Expand Down
3 changes: 3 additions & 0 deletions devito/finite_differences/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ def generate_indices(expr, dim, order, side=None, matvec=None, x0=None, nweights
o_min -= 1

if nweights > 0 and (o_max - o_min + 1) != nweights:
# We cannot infer how the stencil should be centered
# if nweights is more than one extra point.
assert nweights == (o_max - o_min + 1) + 1
# In the "one extra" case we need to pad with one point to symmetrize
if (o_max - mid) > (mid - o_min):
o_min -= 1
else:
Expand Down
14 changes: 6 additions & 8 deletions devito/types/equation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""User API to specify equations."""
from warnings import warn

import sympy

from devito.deprecations import deprecations
from devito.tools import as_tuple, frozendict
from devito.types.lazy import Evaluable

Expand Down Expand Up @@ -64,15 +63,14 @@ class Eq(sympy.Eq, Evaluable):
def __new__(cls, lhs, rhs=0, subdomain=None, coefficients=None, implicit_dims=None,
**kwargs):
if coefficients is not None:
warn("The Substitution API is deprecated and will be removed, "
"coefficients should be passed directly to the derivative object"
" i.e `u.dx(weights=...)",
DeprecationWarning, stacklevel=2)
deprecations.coeff_warn
kwargs['evaluate'] = False
# Backward compat
# Backward compatibility
rhs = cls._apply_coeffs(rhs, coefficients)
lhs = cls._apply_coeffs(lhs, coefficients)

obj = sympy.Eq.__new__(cls, lhs, rhs, **kwargs)

obj._subdomain = subdomain
obj._substitutions = coefficients
obj._implicit_dims = as_tuple(implicit_dims)
Expand All @@ -82,7 +80,7 @@ def __new__(cls, lhs, rhs=0, subdomain=None, coefficients=None, implicit_dims=No
@classmethod
def _apply_coeffs(cls, expr, coefficients):
"""
This process legacy API of Substitution/Coefficients applying the weights
This processes legacy API of Substitution/Coefficients applying the weights
to the target Derivatives.
"""
from devito.symbolics import retrieve_derivatives
Expand Down

0 comments on commit 77e0021

Please sign in to comment.