2

Consider:

@api.route('/my-resource/<id>', endpoint='my-resource')
@api.doc(params={'id': 'An ID'})
class MyResource(Resource):

    @api.doc(responses={403: 'Not Authorized'})
    def post(self, id):
        api.abort(403)

Pylint complains:

R0201: POST Method could be a function (no-self-use)"

How can I avoid this Pylint error?

I tried writing a constructor like:

    @api.route('/my-resource/<id>', endpoint='my-resource')
    @api.doc(params={'id': 'An ID'})
    class MyResource(Resource):
          def __init__(self):
              self.counter = 0

          @api.doc(responses={403: 'Not Authorized'})
           def post(self, id):
               api.abort(403)

But this does not work. It throws Internal server error while calling the API.

Error:

    lib/python3.7/site-packages/flask/app.py", line 1813, in     full_dispatch_request
    rv = self.dispatch_request()

    lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)

    lib/python3.7/site-packages/flask_restplus/api.py", line 319, in wrapper
    resp = resource(*args, **kwargs)

    /lib/python3.7/site-packages/flask/views.py", line 87, in view
    self = view.view_class(*class_args, **class_kwargs)
    TypeError: __init__() takes 1 positional argument but 2 were given
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gautam
  • 384
  • 5
  • 11
  • 1
    If you want to get rid of the pylint warning, just add `# pylint: disable=no-self-use` at the top of the file. It doesn't make any sense to add code that serves no purpose just to get rid of pylint warnings. You might also want to tag this with pylint as it really isn't a flask-restplus problem from what you describe. – dmulter Aug 14 '18 at 17:44

1 Answers1

0

Ref. flask-restplus/flask_restplus/api.py, def _init_app().

It may need one more argument.

Simple fix:

Change

class MyResource(Resource):
      def __init__(self):
          self.counter = 0

      @api.doc(responses={403: 'Not Authorized'})
       def post(self, id):
           api.abort(403)

to

class MyResource(Resource):
      def __init__(self, _):
          self.counter = 0

      @api.doc(responses={403: 'Not Authorized'})
       def post(self, id):
           api.abort(403)

or

class MyResource(Resource):

      @api.doc(responses={403: 'Not Authorized'})
       def post(self, id):
           api.abort(403)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
clucle
  • 164
  • 11