Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use lazy % formatting in logging functions pylint error message

I have a python function as follows, when enabled pylint to code scan, it throws me an lazy formatting error.

def modify_response(data):
    try:
        response = {}
        response["User_ID"] = data[0]["User_ID"]["S"]
        response["Triggered_Timestamp"] = data[0]["Triggered_Timestamp"]["S"]
        return response
    except Exception as e:
        logging.exception("ModifyResponseError: {}".format(e))
        raise ModifyResponseError(json.dumps({"httpStatus": 501,"message": internal_error_message}))
like image 580
ashakshan Avatar asked Apr 07 '21 20:04

ashakshan


1 Answers

Asssuming that the line is

logging.exception("ModifyResponseError: {}".format(e))

and the warning is

W1202: Use lazy % formatting in logging functions (logging-format-interpolation)

the logging functions should be passed a format string and parameters, instead of an already-formatted string. Otherwise you risk the formatting operation itself raising an exception before the logging happens. These are old school printf style format strings. pylint will complain about f-strings too for the same reasons.

the linter would be happy with

logging.exception("ModifyResponseError: %s", e)

See logging.debug for details.

like image 78
tdelaney Avatar answered Nov 04 '22 07:11

tdelaney