"Doesn't have a comparable type" error when returning a list from an on-chain view

Viewed 33

When I return a list from an on-chain view I am receiving a type error but I am not sure where to set the type in order to fix it.

Returning a string or integer from the view works fine. Is there a set limit on the types that can be returned from on-chain views, or is there somewhere I should be explicitly declaring the return types?

I have included a link to a minimal Tezos smart contract on SmartPy IDE with issue reproduction, and include the code below for reference.

Example Contract:

class OnChainViewTestContract(sp.Contract):
    def __init__(self, **kargs):
        self.init(**kargs)

    @sp.onchain_view()
    def run_str_example(self):
        sp.result(self.data.strExample)

    @sp.onchain_view()
    def run_int_example(self):
        sp.result(self.data.intExample)

    @sp.onchain_view()
    def run_list_example(self):
        sp.result(self.data.listExample)

The int and str examples work in the tests below, but the list example fails.

@sp.add_test(name = "OnChainViewReturnTypes")
def test():
    scenario = sp.test_scenario()
    scenario.h1("On-chain view return types test")
    contract = OnChainViewTestContract(
        strExample = 'some_string',
        intExample = 2,
        listExample = ['a', 'b', 'c']        
    )
    scenario += contract
    
    s = contract.run_str_example()
    i = contract.run_int_example()
    l = contract.run_list_example()

    scenario.verify(s == 'some_string')
    scenario.verify(i == 2)
    scenario.verify(l == ['a', 'b', 'c'])

The error:

Error: Type error (sp.contract_view(0, "run_list_example", sp.unit).open_some(message = 'View run_list_example is invalid!') : sp.TList(sp.TString)) doesn't have a comparable type
(__main__, line 36)
1 Answers

You cannot compare lists in Tezos. However, you can use scenario.verify_equal. You have a list of characteristics including comparability for each type in this documentation

Related