diff --git a/aesara/tensor/__init__.py b/aesara/tensor/__init__.py index d726da3241..20084d1892 100644 --- a/aesara/tensor/__init__.py +++ b/aesara/tensor/__init__.py @@ -149,3 +149,27 @@ def _get_vector_length_Constant(op: Union[Op, Variable], var: Constant) -> int: __all__ = ["random"] # noqa: F405 + +# isort: off +from aesara.tensor.math import DEPRECATED_NAMES as MATH_DEPRECATED_NAMES + +# isort: on + + +DEPRECATED_NAMES = MATH_DEPRECATED_NAMES + + +def __getattr__(name): + """Intercept module-level attribute access of deprecated symbols. + + Adapted from https://stackoverflow.com/a/55139609/3006474. + + """ + from warnings import warn + + for old_name, msg, old_object in DEPRECATED_NAMES: + if name == old_name: + warn(msg, DeprecationWarning, stacklevel=2) + return old_object + + raise AttributeError(f"module {__name__} has no attribute {name}") diff --git a/aesara/tensor/math.py b/aesara/tensor/math.py index d5d2d6c6a8..108c3b911b 100644 --- a/aesara/tensor/math.py +++ b/aesara/tensor/math.py @@ -3145,6 +3145,11 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None DEPRECATED_NAMES = [ ("abs_", "`abs_` is deprecated; use `abs` instead.", abs), ("inv", "`inv` is deprecated; use `reciprocal` instead.", reciprocal), + ( + "true_div", + "`true_div` is deprecated; use `true_divide` or `divide` instead.", + true_divide, + ), ] diff --git a/tests/tensor/test_math.py b/tests/tensor/test_math.py index 3bb13dd912..0a41443fb6 100644 --- a/tests/tensor/test_math.py +++ b/tests/tensor/test_math.py @@ -3535,3 +3535,25 @@ def test_logdiffexp(): if isinstance(node.op, Elemwise) and isinstance(node.op.scalar_op, aes.Exp) ] assert len(ops_graph) != 2 + + +def test_deprecations(): + """Make sure we can import from deprecated modules.""" + + with pytest.deprecated_call(): + from aesara.tensor.math import abs_ # noqa: F401 F811 + + with pytest.deprecated_call(): + from aesara.tensor import abs_ # noqa: F401 F811 + + with pytest.deprecated_call(): + from aesara.tensor import inv # noqa: F401 F811 + + with pytest.deprecated_call(): + from aesara.tensor.math import inv # noqa: F401 F811 + + with pytest.deprecated_call(): + from aesara.tensor import true_div # noqa: F401 F811 + + with pytest.deprecated_call(): + from aesara.tensor.math import true_div # noqa: F401 F811