Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint complains about method 'data_received' not overridden, for RequestHandler

For example:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('data.html', items = [])

It yields the following Pylint error:

warning (W0223, abstract-method, MainHandler) Method 'data_received' is abstract in class 'RequestHandler' but is not overridden

I understand that somehow it wants me to override this data_received method, but I do not understand why, and what it is for?

like image 237
PascalVKooten Avatar asked Aug 11 '15 10:08

PascalVKooten


2 Answers

This is actually a problem with Pylint that's sort of unavoidable with the nature of Python.

The RequestHandler class has a lot of methods that act as hooks you can override in order to do different things, but only some of those hooks may actually be called, depending on your application's code. To make sure you're implementing everything you're supposed to when you're using certain functionality, the default data_received implementation throws a NotImplementedError that will get triggered when you do something that expects your class to have a custom implementation.

Normally this isn't any kind of issue because Python lets you have code paths that fail and doesn't throw any errors. Because Pylint tries to "help" make sure you've done everything you're supposed to, it's seeing that NotImplementedError throw and is warning you that you could trigger it depending on what you do.

The real problem is that because Python is an interpreted language, it's hard for a tool like Pylint to look at your code and make sure it's "safe". Python gives you a lot of flexibility and power, but in turn you bear the burden of keeping your program's logic straight in your head and knowing what possible problems are actually problems, and what aren't.

Luckily, Pylint is aware of its own limitations and gives you nice tools to disable extraneous warnings. Add the comment line

# pylint: disable=W0223

right before your class definition and the warning should stop popping up for this instance while leaving everything else alone.

like image 103
R Phillip Castagna Avatar answered Nov 18 '22 06:11

R Phillip Castagna


I am running into the same issue as the OP, except my PyCharm (2018.3.4) seems not to be using Pylint, but its own inspection engine. I managed to solve a similar issue with the similar trick as R Phillip Castagna suggested:

# noinspection PyAbstractClass
class XyzRequestHandler(tornado.web.RequestHandler):
    def prepare(self):
        print('-' * 100)
        self.set_header('Access-Control-Allow-Origin', '*')

    def print_n_respond(self, result):
        response = json.dumps(result)
        print('responding with:\n', response)
        print()
        self.write(response)

Here is a list of PyCharm's inspections.

like image 39
Indominus Avatar answered Nov 18 '22 06:11

Indominus