From e3a3d6e925a32174d6bd6724fa5074e091cedd38 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sun, 26 May 2024 23:44:26 +0200 Subject: Add comment and exception for problematic edge case in from_any() --- polymatrix/expression/from_.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/polymatrix/expression/from_.py b/polymatrix/expression/from_.py index 65b636d..a3ee56e 100644 --- a/polymatrix/expression/from_.py +++ b/polymatrix/expression/from_.py @@ -7,12 +7,14 @@ from typing import Iterable, Any import polymatrix.expression.init as init +from polymatrix.expression.init import init_variable_expr from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin from polymatrix.expression.typing import FromSupportedTypes from polymatrix.expressionstate import ExpressionState from polymatrix.statemonad import StateMonad from polymatrix.polymatrix.mixins import PolyMatrixMixin from polymatrix.utils.deprecation import deprecated +from polymatrix.variable import Variable from polymatrix.expression.expression import ( init_expression, Expression, @@ -43,6 +45,29 @@ def from_any_or(value: FromSupportedTypes, value_if_not_supported: Any) -> Expre elif isinstance(value, ExpressionBaseMixin): return init_expression(value) + elif isinstance(value, Variable) and not isinstance(value, ExpressionBaseMixin): + # This happens when a variable is constructed somewhere using + # polymatrix.variable.init_variable. What should happen here? + + # This is problematic because if there is already another variable in + # the state with the same name, but different type (eg a VariableExpr) + # it will throw an error. + + # Also we need to consider the case when a variable was created by + # another package, e.g. is an optimization variable. What happens then? + + # Should polymatrix.variable.VariableImpl even exist in the first + # place? It was part of the proposed architecture but I think it causes + # more problems than it is solving. + + # Commented code below won't work + + # value = init_variable_expr(value.name, shape=value.shape) + # return init_variable_expression(value) + + raise NotImplementedError("You have encountered a convoluted edge case. " + "I'm very sorry it doesn't work yet.") + elif isinstance(value, int | float): return from_number(value) -- cgit v1.2.1