Search code examples
pythonpython-3.xpython-2.7odooodoo-11

Python3 TypeError: can only concatenate list (not "str") to list


I'm porting odoo 11, python 2.7 to python 3. I have been edited an addon which has belongs to odoo, python code.

The code is:

vat = invoice.partner_id.vat or ''
vat = list(filter(lambda x: x.isnumeric(), vat[:2])) + vat[2:]

Error is:

TypeError: can only concatenate list (not "str") to list

How can I fix that, what is wrong with this code? Please help me.


Solution

  • list(filter(lambda x: x.isnumeric(), vat[:2]))
    

    This operation above always returns the list.

    vat = invoice.partner_id.vat or '' 
    
    • seems, this operation returns str (because of or '').

    If you expect that your type(vat)==list, you should use

    vat = invoice.partner_id.vat or []
    

    If you expect type(vat)==str, you should convert your filtered list to str, for example

    "".join(list(filter(lambda x: x.isnumeric(), vat[:2]))) + vat[2:]