votes up 3

jsonify() behavior undefined when passed both args and kwargs

Package:
flask
github stars 56479
Exception Class:
TypeError

Raise code

    separators = (",", ":")

    if current_app.config["JSONIFY_PRETTYPRINT_REGULAR"] or current_app.debug:
        indent = 2
        separators = (", ", ": ")

    if args and kwargs:
        raise TypeError("jsonify() behavior undefined when passed both args and kwargs")
    elif len(args) == 1:  # single args are passed directly to dumps()
        data = args[0]
    else:
        data = args or kwargs

    return current_app.response_class(
        f"{dumps(data, indent=indent, separators=separators)}\n",
😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 3rd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 0 votes down

Arguments and keyword arguments should not be passed at the same time to jsonify.

How to reproduce:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def index():
    return jsonify("text", i="test")

app.run()

Code with fix

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def index():
    return jsonify(j="text", i="test") #<-- pass only arg or kwarg, not both

app.run()

May 27, 2021 cRyp70s answer
cRyp70s 113

Add a possible fix

Please authorize to post fix