Skip to content

Commit

Permalink
Support sub-package-level deprecations in aesara.tensor
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonwillard committed Feb 15, 2023
1 parent 9252254 commit 9e3c2f9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions aesara/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
5 changes: 5 additions & 0 deletions aesara/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
]


Expand Down
22 changes: 22 additions & 0 deletions tests/tensor/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9e3c2f9

Please sign in to comment.