Search code examples
pythonpython-3.xflake8

flake8 (E902) TokenError in python format


I dont understand why i got a E902 flake8 Error. The code is as follows:

def mi_func(x):

    # Asignar los valores a una lista
    lista_massn = x['MASSN'].values.tolist()
    lista_flag = x['UFLAG'].values.tolist()

    # Obtener los valores unicos
    unique_list_massn = unique_values(lista_massn)
    unique_list_flag = unique_values(lista_flag

    # Reglas de negocio para indicar desviaciones
    if (
            (len(unique_list_massn) == 1)
            & (unique_list_massn[0] == 'XE')
            & (max(unique_list_flag) == 0)):
        result = True
    else:
        result = False

    return result

I think the problem is in the "if/else" statement. I put the condition in several lines in order to satisfy the numbre of character <80 (flake8 E501). Is there a way to put this "if/else" statement satifaying the both flake8 rules (flake8 E501 and E902)? Thanks in advance!


Solution

  • E902 is a catchall for SyntaxErrors (in this case a TokenError)

    python or pypy give you a more useful SyntaxError in this case

    $ python3 t.py 
      File "t.py", line 14
        & (max(unique_list_flag) == 0)))
                                       ^
    SyntaxError: invalid syntax
    $ pypy3 t.py 
      File "t.py", line 8
        unique_list_flag = unique_values(lista_flag
                                        ^
    SyntaxError: parenthesis is never closed
    

    after fixing that the code passes flake8:

    $ flake8 t.py
    $
    

    disclaimer: I'm the maintainer of flake8