From 65d01a8875537b2a223d1da42fa4882864c1d26d Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov Date: Tue, 26 Nov 2024 14:46:52 +0200 Subject: [PATCH 1/9] Deprecate operator dispatch --- src/spox/_var.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/src/spox/_var.py b/src/spox/_var.py index 15dd2186..109a756a 100644 --- a/src/spox/_var.py +++ b/src/spox/_var.py @@ -1,7 +1,7 @@ # Copyright (c) QuantCo 2023-2024 # SPDX-License-Identifier: BSD-3-Clause - import typing +import warnings from typing import Any, Callable, ClassVar, Optional, TypeVar, Union import numpy as np @@ -144,57 +144,129 @@ def __deepcopy__(self, _) -> "Var": raise ValueError("'Var' objects cannot be deepcopied.") def __add__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.add(self, other) def __sub__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.sub(self, other) def __mul__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.mul(self, other) def __truediv__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.truediv(self, other) def __floordiv__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.floordiv(self, other) def __neg__(self) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.neg(self) def __and__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.and_(self, other) def __or__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.or_(self, other) def __xor__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.xor(self, other) def __invert__(self) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.not_(self) def __radd__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.add(other, self) def __rsub__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.sub(other, self) def __rmul__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.mul(other, self) def __rtruediv__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.truediv(other, self) def __rfloordiv__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.floordiv(other, self) def __rand__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.and_(other, self) def __ror__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.or_(other, self) def __rxor__(self, other) -> "Var": + warnings.warn( + "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) return Var._operator_dispatcher.xor(other, self) From fc8816342be4f79e7da2492ba3e80caac1554d0c Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov Date: Tue, 26 Nov 2024 14:57:50 +0200 Subject: [PATCH 2/9] Add changelog --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7e6db108..a67ebf73 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,6 +18,10 @@ Change log - Value propagation of string tensors no longer raises an erroneous ``ValueError`` in some instances. +**Pending breaking changes** + +- Usage of operator overloading via ``_operator_dispatcher`` now triggers a ``DeprecationWarning``. This API is scheduled for removal in Spox ``0.14.0``. Consider using ``ndonnx`` as an alternative. + **Other changes** - The adaption logic is not using inferred values as initializers anymore. From e6c5201cf4583f4145b8e4a1a6f51c2b795413c5 Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov Date: Tue, 26 Nov 2024 15:30:42 +0200 Subject: [PATCH 3/9] Comments after code review --- CHANGELOG.rst | 2 +- src/spox/_future.py | 5 ++++ src/spox/_var.py | 73 --------------------------------------------- 3 files changed, 6 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a67ebf73..2220a3f0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,7 +20,7 @@ Change log **Pending breaking changes** -- Usage of operator overloading via ``_operator_dispatcher`` now triggers a ``DeprecationWarning``. This API is scheduled for removal in Spox ``0.14.0``. Consider using ``ndonnx`` as an alternative. +- Usage of ``operator_overloading`` now triggers a ``DeprecationWarning``. This API is scheduled for removal in Spox ``0.14.0``. Consider using ``ndonnx`` as an alternative. **Other changes** diff --git a/src/spox/_future.py b/src/spox/_future.py index ecaa5c7b..43313818 100644 --- a/src/spox/_future.py +++ b/src/spox/_future.py @@ -3,6 +3,7 @@ """Module containing experimental Spox features that may be standard in the future.""" +import warnings from collections.abc import Iterable from contextlib import contextmanager from typing import Optional, Union @@ -201,6 +202,10 @@ def operator_overloading( ... return x * y >>> assert foo()._get_value() == np.array(6) """ + warnings.warn( + "using operator_overloading is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) prev_dispatcher = Var._operator_dispatcher Var._operator_dispatcher = _NumpyLikeOperatorDispatcher( op, type_promotion, constant_promotion diff --git a/src/spox/_var.py b/src/spox/_var.py index 109a756a..55672e39 100644 --- a/src/spox/_var.py +++ b/src/spox/_var.py @@ -1,7 +1,6 @@ # Copyright (c) QuantCo 2023-2024 # SPDX-License-Identifier: BSD-3-Clause import typing -import warnings from typing import Any, Callable, ClassVar, Optional, TypeVar, Union import numpy as np @@ -144,129 +143,57 @@ def __deepcopy__(self, _) -> "Var": raise ValueError("'Var' objects cannot be deepcopied.") def __add__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.add(self, other) def __sub__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.sub(self, other) def __mul__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.mul(self, other) def __truediv__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.truediv(self, other) def __floordiv__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.floordiv(self, other) def __neg__(self) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.neg(self) def __and__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.and_(self, other) def __or__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.or_(self, other) def __xor__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.xor(self, other) def __invert__(self) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.not_(self) def __radd__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.add(other, self) def __rsub__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.sub(other, self) def __rmul__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.mul(other, self) def __rtruediv__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.truediv(other, self) def __rfloordiv__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.floordiv(other, self) def __rand__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.and_(other, self) def __ror__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.or_(other, self) def __rxor__(self, other) -> "Var": - warnings.warn( - "using _operator_dispatcher is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) return Var._operator_dispatcher.xor(other, self) From 1729b65fa1d131c71d16cceea95e2c56e4a30e4e Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov Date: Tue, 26 Nov 2024 15:31:28 +0200 Subject: [PATCH 4/9] Fix diff --- src/spox/_var.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/spox/_var.py b/src/spox/_var.py index 55672e39..15dd2186 100644 --- a/src/spox/_var.py +++ b/src/spox/_var.py @@ -1,5 +1,6 @@ # Copyright (c) QuantCo 2023-2024 # SPDX-License-Identifier: BSD-3-Clause + import typing from typing import Any, Callable, ClassVar, Optional, TypeVar, Union From 780e22b411c03f7feb25dff6b1fc2e72dad30fe0 Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov <70822030+neNasko1@users.noreply.github.com> Date: Wed, 27 Nov 2024 22:34:12 +0200 Subject: [PATCH 5/9] Update CHANGELOG.rst Co-authored-by: Christian Bourjau --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2220a3f0..65158e26 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,7 +20,7 @@ Change log **Pending breaking changes** -- Usage of ``operator_overloading`` now triggers a ``DeprecationWarning``. This API is scheduled for removal in Spox ``0.14.0``. Consider using ``ndonnx`` as an alternative. +- Importing :func:`spox._future.operator_overloading`` now triggers a ``DeprecationWarning``. The function will be removed in a future release. Consider using ``ndonnx`` as an alternative. **Other changes** From 30401ea2b82bd94d8e1a31365f0580047479607d Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov Date: Thu, 28 Nov 2024 01:15:55 +0200 Subject: [PATCH 6/9] Make module level import report warning --- src/spox/_future.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/spox/_future.py b/src/spox/_future.py index 43313818..f6ec5c8f 100644 --- a/src/spox/_future.py +++ b/src/spox/_future.py @@ -165,7 +165,7 @@ def not_(self, a: Var) -> Var: @contextmanager -def operator_overloading( +def _operator_overloading( op, type_promotion: bool = False, constant_promotion: bool = True ): """Enable operator overloading on Var for this block. @@ -202,10 +202,6 @@ def operator_overloading( ... return x * y >>> assert foo()._get_value() == np.array(6) """ - warnings.warn( - "using operator_overloading is deprecated, consider using https://github.com/Quantco/ndonnx instead", - DeprecationWarning, - ) prev_dispatcher = Var._operator_dispatcher Var._operator_dispatcher = _NumpyLikeOperatorDispatcher( op, type_promotion, constant_promotion @@ -214,6 +210,17 @@ def operator_overloading( Var._operator_dispatcher = prev_dispatcher +def __getattr__(name): + if name == "operator_overloading": + warnings.warn( + "using 'operator_overloading' is deprecated, consider using https://github.com/Quantco/ndonnx instead", + DeprecationWarning, + ) + return _operator_overloading + + raise AttributeError(f"module {__name__} has no attribute {name}") + + __all__ = [ # Type warning levels "TypeWarningLevel", @@ -226,5 +233,6 @@ def operator_overloading( "set_value_prop_backend", "value_prop_backend", # Operator overloading on Var - "operator_overloading", + "operator_overloading", # noqa: F822 + "__getattr__", ] From efc211b70384ef7f1ea78147b1a5ccc99dcaaf3b Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov <70822030+neNasko1@users.noreply.github.com> Date: Fri, 29 Nov 2024 15:01:25 +0200 Subject: [PATCH 7/9] Update src/spox/_future.py Co-authored-by: Christian Bourjau --- src/spox/_future.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/spox/_future.py b/src/spox/_future.py index f6ec5c8f..24c3db07 100644 --- a/src/spox/_future.py +++ b/src/spox/_future.py @@ -233,6 +233,4 @@ def __getattr__(name): "set_value_prop_backend", "value_prop_backend", # Operator overloading on Var - "operator_overloading", # noqa: F822 - "__getattr__", ] From 9a5701ff3e517710036103d9fe7f70700feccd2c Mon Sep 17 00:00:00 2001 From: Atanas Dimitrov Date: Fri, 29 Nov 2024 15:05:03 +0200 Subject: [PATCH 8/9] Fix coment --- src/spox/_future.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/spox/_future.py b/src/spox/_future.py index 24c3db07..7cd8d0a5 100644 --- a/src/spox/_future.py +++ b/src/spox/_future.py @@ -232,5 +232,4 @@ def __getattr__(name): "ValuePropBackend", "set_value_prop_backend", "value_prop_backend", - # Operator overloading on Var ] From a4f70e6b02bb6666c287821f81ed12b28fedb838 Mon Sep 17 00:00:00 2001 From: Christian Bourjau Date: Fri, 29 Nov 2024 16:01:48 +0100 Subject: [PATCH 9/9] Update CHANGELOG.rst --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 65158e26..bc906e08 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,7 +18,7 @@ Change log - Value propagation of string tensors no longer raises an erroneous ``ValueError`` in some instances. -**Pending breaking changes** +**Deprecation** - Importing :func:`spox._future.operator_overloading`` now triggers a ``DeprecationWarning``. The function will be removed in a future release. Consider using ``ndonnx`` as an alternative.