Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"'float' object is not iterable" while parsing list arguments from json in GET request #892

Closed
samaranin opened this issue Aug 21, 2020 · 1 comment

Comments

@samaranin
Copy link

samaranin commented Aug 21, 2020

I've got a problem while sending JSON data with a list in it using GET request.
I have a JSON like

{
    "dataset": [0.5, 0.5]
}

And when I am trying to parse it from the request using reqparse.RequestParser I am getting an error

'float' object is not iterable

It works fine when arg type is str or int or float, but for the list, there is trouble with this.

Some code below.
I've made a helper method to setup a request parameters parsing

def parse_arguments(arguments):
    """
    Method to parse request arguments
    :param arguments: list of arguments to parse
    :return: list of parsed arguments
    """
    # create arguments parser and add arguments
    parser = reqparse.RequestParser(bundle_errors=True)
    for arg in arguments:
        parser.add_argument(arg.get('name', None), type=arg.get('type', None),
                            help=arg.get('help', ""), location=ARGS_LOCATIONS_LIST,
                            required=arg.get('required', False), action=arg.get('action', None))
    # hack to get json from get request
    try:
        _ = request.get_json(force=True)
    except HTTPException:
        print("No json found in this request. Trying to parse args from values")

    # parsing argument for request
    return parser.parse_args()

And parameters for the request setup looks like this

# list of arguments for the request
arguments = [
   {"name": "dataset", "type": list, "help": "dataset must be list", "required": True, "action": "append"}
]
# parsing arguments
args = parse_arguments(arguments)

And with the code above, I get an error.
I've made a workaround to fix this problem for me, but I think it should not work like this.

def parse_arguments(arguments):
    """
    Method to parse request arguments
    :param arguments: list of arguments to parse
    :return: list of parsed arguments
    """
    # create arguments parser and add arguments
    parser = reqparse.RequestParser(bundle_errors=True)
    for arg in arguments:
        parser.add_argument(arg.get('name', None), type=arg.get('type', None),
                            help=arg.get('help', ""), location=ARGS_LOCATIONS_LIST,
                            required=arg.get('required', False), action=arg.get('action', None))
    # hack to get json from get request
    try:
        json_values = request.get_json(force=True)  # getting values from request json directly
        parser_values = parser.parse_args()  # parsing values from request using reqparse

        for key, value in parser_values.items():
            # if we have type error while parsing arguments - replace it from json
            if isinstance(parser_values[key], TypeError):
                parser_values[key] = json_values.get(key, None)

        return parser_values
    except HTTPException:
        print("No json found in this request. Trying to parse args from values")

    # parsing argument for request
    return parser.parse_args()

So the question is: am I doing something wrong or this is a bug?

@joshfriend
Copy link
Member

please read the docs for how to parse lists of values and also the pinned issue #883 for why you should choose something else for parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants