'non-iterable ellipsis in Flask_Restful parser.parse_args()

I've come back to a project I worked on previously, utilising flask RESTful, though whenever I attempt a post request the line args = parser.parse_args() breaks the code, giving an error reading:

File "/usr/local/lib/python3.9/site-packages/werkzeug/datastructures.py", line 554, in update

for key, value in iter_multi_items(mapping):

TypeError: cannot unpack non-iterable ellipsis object

For some reason the parser seems to be having issues with its own arguments, the post function is:

def post(self):
    from flask_restful import reqparse
    parser = reqparse.RequestParser()
    parser.add_argument('rate', type=int, help='Rate cannot be converted')
    parser.add_argument('name')
    args = parser.parse_args()

Any help or direction would be greatly appreciated.

-- Edit

To give more context, my init file looks like this, the error occurring when the POST function is called in postman.

from flask import Flask, g
from flask_restful import Resource, Api, reqparse
import os
import shelve

app = Flask(__name__)
api = Api(app)

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = shelve.open("rates.db")
    return db

@app.teardown_appcontext
def teardown_db(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

@app.route("/")
def index():
    return "Hello World"

class RateList(Resource):
    def get(self):
        shelf = get_db()
        keys = list(shelf.keys())

        devices = []

        for key in keys:
            devices.append(shelf[key])

        return {'message' : 'Success', 'data' : devices}, 200

    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('rate', type=int, help='Rate cannot be converted')
        parser.add_argument('name')
        args = parser.parse_args()

        shelf = get_db()
        shelf[args['rate']] = args

        return {'message' : 'Rate registered', 'data' : args}, 201

class Rate(Resource):
    def get(self, rate):
        shelf = get_db()

        if not(rate in shelf):
            return {'message' : 'Rate not found', 'data' : {}}, 404
        
        return {'message' : 'Rate found', 'data' : shelf[rate]}, 200

    def delete(self, rate):
        shelf = get_db()

        if not(rate in shelf):
            return {'message' : 'Rate not found', 'data' : {}}, 404
        
        del shelf[rate]
        return {'message' : 'Rate deleted', 'data' : {}}, 200

api.add_resource(RateList, '/Rate')
api.add_resource(Rate, '/Rate/<string:rate>')

Docker-compose file:

version: '3.4'

services:
    stock-registry:
        build: .
        volumes:
            - .:/usr/src/app
        ports:
            - 5001:80

Dockerfile:

FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./run.py"]

Run.py:

from stock_registry import app
app.run(host='0.0.0.0', port=80, debug=True)

-- Edit 2

The JSON body sent on the post request, using postman:

{
    "rate" : 1,
    "name" : "Test" 
}

The complete error log that shows when this is attempted:

172.22.0.1 - - [30/Jun/2021 12:37:12] "POST /shares HTTP/1.1" 500 -

Traceback (most recent call last):

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1997, in __call__

return self.wsgi_app(environ, start_response)

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1985, in wsgi_app

response = self.handle_exception(e)

File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 265, in error_router

return original_handler(e)

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1540, in handle_exception

reraise(exc_type, exc_value, tb)

File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 32, in reraise

raise value.with_traceback(tb)

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1982, in wsgi_app

response = self.full_dispatch_request()

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1614, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 265, in error_router

return original_handler(e)

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1517, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 32, in reraise

raise value.with_traceback(tb)

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1612, in full_dispatch_request

rv = self.dispatch_request()

File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1598, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 446, in wrapper

resp = resource(*args, **kwargs)

File "/usr/local/lib/python3.9/site-packages/flask/views.py", line 84, in view

return self.dispatch_request(*args, **kwargs)

File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 550, in dispatch_request

resp = meth(*args, **kwargs)

File "/usr/src/app/stock_registry/__init__.py", line 65, in post

args = parser.parse_args()

File "/usr/local/lib/python3.9/site-packages/flask_restful/reqparse.py", line 261, in parse_args

value, found = arg.parse(req)

File "/usr/local/lib/python3.9/site-packages/flask_restful/reqparse.py", line 143, in parse

source = self.source(request)

File "/usr/local/lib/python3.9/site-packages/flask_restful/reqparse.py", line 101, in source

values.update(value)

File "/usr/local/lib/python3.9/site-packages/werkzeug/datastructures.py", line 554, in update

for key, value in iter_multi_items(mapping):


TypeError: cannot unpack non-iterable ellipsis object


Solution 1:[1]

I had the same issue. Can you share your version numbers of flask and of flask-restful?

What worked for me is updating both packages, since somehow I apparently got 2 versions that don't work together in my environment. I upgraded them to the following and now it's working:

Try: pip install flask==2.0.1 flask-restful==0.3.9

Hope this works!

Solution 2:[2]

It is a version compatibility error in flask or flask-restful or werkzeug. What worked for me:

Werkzeug==1.0.1
Flask==0.11.1
Flask-RESTful==0.3.5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom H.
Solution 2 Archit Saxena