diff --git a/dimod/binary/binary_quadratic_model.py b/dimod/binary/binary_quadratic_model.py index 7c2270411..d28f9d834 100644 --- a/dimod/binary/binary_quadratic_model.py +++ b/dimod/binary/binary_quadratic_model.py @@ -41,7 +41,7 @@ from dimod.binary.cybqm import cyBQM_float32, cyBQM_float64 from dimod.binary.pybqm import pyBQM from dimod.binary.vartypeview import VartypeView -from dimod.decorators import forwarding_method, unique_variable_labels +from dimod.decorators import forwarding_method, unique_variable_labels, property_or_method from dimod.quadratic import QuadraticModel, QM from dimod.quadratic.quadratic_model import _VariableArray from dimod.serialization.fileview import SpooledTemporaryFile, _BytesIO, VariablesSection @@ -544,7 +544,7 @@ def offset(self) -> np.number: def offset(self, offset): self.data.offset = offset - @property + @property_or_method def num_interactions(self) -> int: """Number of interactions in the model. @@ -552,7 +552,7 @@ def num_interactions(self) -> int: """ return self.data.num_interactions() - @property + @property_or_method def num_variables(self) -> int: """Number of variables in the model.""" return self.data.num_variables() @@ -614,7 +614,7 @@ def variables(self) -> Variables: """The variables of the binary quadratic model.""" return self.data.variables - @property + @property_or_method def vartype(self) -> Vartype: """The model's variable type. diff --git a/dimod/decorators.py b/dimod/decorators.py index c4468ba33..94c534bc3 100644 --- a/dimod/decorators.py +++ b/dimod/decorators.py @@ -22,6 +22,7 @@ from numbers import Integral from dimod.exceptions import BinaryQuadraticModelStructureError, WriteableError +from dimod.future import shape_methods from dimod.utilities import new_variable_label from dimod.vartypes import as_vartype @@ -421,3 +422,29 @@ def conditional_unique_label(label = None, *args, **kwargs): return qm return conditional_unique_label + + +class property_or_method: + def __init__(self, func): + self.func = func + + def __get__(self, obj, objtype=None): + func = self.func + + caller = inspect.currentframe().f_back + if caller.f_globals.get("shape_methods", None) is not shape_methods: + # warnings.warn( + # f"Accessing '{type(obj).__name__}.{func.__name__}' as a property is deprecated, " + # "use 'from dimod.future import shape_methods' and " + # f"'{type(obj).__name__}.{func.__name__}()' instead.", + # DeprecationWarning, stacklevel=2) + return func(obj) + + @wraps(func) + def wrapper(*args, **kwargs): + return func(obj, *args, **kwargs) + + return wrapper + + def __set__(self, obj, value): + raise AttributeError("can't set attribute") # same error as property diff --git a/dimod/future.py b/dimod/future.py new file mode 100644 index 000000000..1ef91afb7 --- /dev/null +++ b/dimod/future.py @@ -0,0 +1,17 @@ +# Copyright 2018 D-Wave Systems Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +class shape_methods: + pass diff --git a/dimod/quadratic/quadratic_model.py b/dimod/quadratic/quadratic_model.py index 99a9d73e0..02a837feb 100644 --- a/dimod/quadratic/quadratic_model.py +++ b/dimod/quadratic/quadratic_model.py @@ -33,7 +33,7 @@ ArrayLike = Any DTypeLike = Any -from dimod.decorators import forwarding_method, unique_variable_labels +from dimod.decorators import forwarding_method, unique_variable_labels, property_or_method from dimod.quadratic.cyqm import cyQM_float32, cyQM_float64 from dimod.serialization.fileview import SpooledTemporaryFile, _BytesIO from dimod.serialization.fileview import VariablesSection, Section @@ -352,7 +352,7 @@ def dtype(self) -> np.dtype: """Data-type of the model's biases.""" return self.data.dtype - @property + @property_or_method def num_interactions(self) -> int: """Number of interactions in the model. @@ -360,7 +360,7 @@ def num_interactions(self) -> int: """ return self.data.num_interactions() - @property + @property_or_method def num_variables(self) -> int: """Number of variables in the model.""" return self.data.num_variables()