Disable UserWarning in terminal

I get a UserWarning in the terminal when using my app:

UserWarning: Operand 'foo' is found in the params. Did you mean to use Lookup('foo') instead?

I am absolutely sure I don’t want to use Lookup in this case. Can I disable this Warning?

Hi Reinout,

UserWarnings can not be disabled. Generally, it is a good idea to solve the warnings generated by the app.
If you need help with this, let me know.

Hi Reinout,

We are trying to understand your situation. What are you trying to do with ‘foo’?

Is ‘foo’ a paramater used in a Visibility argument or similar?
or, are you doing a string compare somwhere, were the string happens to match a parameter name?

In the second case , the warning is a false positive and we should consider removing it.

I have a dictionary

default_values ={"foo": 16}

I let the user choose if he wants to use the default dictionary or a manual input. If the input is default I use an OutputField to show the corresponding value in the dictionary. If the user wants to override he gets a NumberField.

option = BooleanField("manual input?")
foo_automatic = OutputField("foo value", value=FunctionLookup(get_default_value, "foo"), visible=Lookup("option"))
foo_manual = NumberField("foo value", name="foo", visible=IsFalse(Lookup("option")), default=default_values["foo"])

def get_default_value(key: str):
   return default_values[key]

If I didn’t mess up something in this simplification this wil give the above described UserWarning. I added the FunctionLookup because I use that for something else and I am not sure if it is related.

Hi Reinout,

I think you are using the FunctionLookup when there is no need for it. I think that is why you see this warning.

I am a bit confused as to why you would want to use “foo” as a function argument, it seems like it would be a hardcoded value? You can also use the hardcoded value directly for an OutputField as follows:

foo_automatic = OutputField("foo value",  value=default_value["foo"], visible=Lookup("option") )

In case this default does change depending on the input, you also do not need to use FunctionLookup, as shown here. You can use a function that directly uses the params or the entity_id instead.

def get_default_foo_value(params, **kwargs):
    default_values =  create_changing_defaults(params)
    return default_values["foo"]

class Parametrization(ViktorParametrization):
    ...
    foo_automatic = OutputField("foo value",  value=get_default_foo_value, visible=Lookup("option") )

Hello,

I am sorry. I tried to simplify my code and this led to some confusion.
There are multiple options for foo in this, selected by the user in an OptionField (and the dict with “foo” is actually 8 keys long)

default_values = {option_1: {"foo": 16, "bar": 17, "baz": 18}, option_2 : {"foo": 20, "bar": 21, "baz": 22}}
option_field = OptionField("Option choice", options=[option_1, option_2])

Other code becomes:

option = BooleanField("manual input?")
foo_automatic = OutputField("foo value", value=FunctionLookup(get_default_value, Lookup("option_field"),  "foo"), visible=Lookup("option"))
foo_manual = NumberField("foo value", name="foo", visible=IsFalse(Lookup("option")), default=default_values["option_1"]["foo"])

And because I need to catch if option_field is empty in a try-except the get_default_key funtion looks like this:

def get_default_value(key_1: str, key_2: str):
    try:
        return default_values[key_1][key_2]
    except KeyError:
        return None

Hi Reinout,

We have given this some thought and currently it is not possible to suppress UserWarning. Your current warning arises since you use the key "foo" as input for a FunctionLookup function, and the name foo is also present in params.

As a workaround you can change the function signature such that "foo" does not arise as input for your function, e.g. by using a tuple as input. Hence:

def get_default_value(inputs = Tuple[str, str]):
    key_1, key_2 = inputs
    try:
        return default_values[key_1][key_2]
    except KeyError:
        return None


foo_automatic = OutputField("foo value", value=FunctionLookup(get_default_value, (Lookup("option_field"),  "foo")), visible=Lookup("option"))

Can you verify that this indeed works in your case, and that the warning is not raised?