Pylint: False positive for E0202 (method-hidden)

Created on 16 Dec 2014  路  11Comments  路  Source: PyCQA/pylint

Originally reported by: Anonymous


With this code:

#!python

import json


class MyEncoder(json.JSONEncoder):
    def default(self, o):
        json.JSONEncoder.default(self, o)

I have the following pylint output:

#!bash

C:\Documents\Projects\Test>pylint --disable=C0111,R0903 --msg-template="{msg_id}:{line:3d},{column}: {obj}: {msg} ({symbol})" --report=n example.py
No config file found, using default configuration
************* Module example
E0202:  5,4: MyEncoder.default: An attribute defined in json.encoder line 152 hides this method (method-hidden)

Version:

#!bash

C:\Documents\Projects\Test>pylint --version
No config file found, using default configuration
pylint 1.4.0,
astroid 1.3.2, common 0.63.2
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)]

bug

Most helpful comment

Workaround: def default(self, o): # pylint: disable=E0202

All 11 comments

I'm also having the problem. Looks like pylint does not like overloading a method with 'default' as the method name. Our build system is using pylint to check code, and the result is I cannot extend the JSONEncoder class.

I have the same problem

Same problem.

Workaround: def default(self, o): # pylint: disable=E0202

This is indeed a false positive, but it has nothing to do with the fact that the method is called 'default' and partially, it is an oddity with the json.JSONEncoder class as well. The problem is that it can accept a default argument, which gets set into a property here https://hg.python.org/cpython/file/tip/Lib/json/encoder.py#l157, although later it also defines a default method for being overridden by subclasses. I need to think of a potential fix though.

I get this error too when overriding the run method of flask_script's Command class

[pylint] E0202:An attribute defined in flask_script.commands line 153 hides this method

Possible solutions:

  • Only raise message at site of assignment instead of at FunctionDef. This would leave the maintainers of the module with the warning, but not the users.
  • Automatically ignore this message if the any of the overridden functions are "abstract" (i.e. always raises NotImplementedError)
  • Ignore method-hidden if the FunctionDef is below the assignment location in the mro

Any thoughts?

@brycepg the first one sounds like the best option in my opinion.

What's the status of this bug? Is there any fix likely to be implemented soon?

pylint: disable=E0202

pylint: disable=method-hidden will be better.

Use of _disable_ workaround also ends up leaving one with long lines.

def default(self, o): # pylint: disable=E0202

works but

def default(self,
            o): # pylint: disable=E0202

and neither does:

# pylint: disable=E0202
def default(self, o):
# pylint: enable=E0202
Was this page helpful?
0 / 5 - 0 ratings

Related issues

PCManticore picture PCManticore  路  3Comments

TBoshoven picture TBoshoven  路  3Comments

sambarluc picture sambarluc  路  3Comments

glmdgrielson picture glmdgrielson  路  3Comments

jrial picture jrial  路  3Comments