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

False positive for E0202 (method-hidden) #414

Closed
pylint-bot opened this issue Dec 16, 2014 · 13 comments · Fixed by #3206
Closed

False positive for E0202 (method-hidden) #414

pylint-bot opened this issue Dec 16, 2014 · 13 comments · Fixed by #3206
Labels

Comments

@pylint-bot
Copy link

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)]

@yuyang00
Copy link

yuyang00 commented Mar 1, 2016

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.

@Lothiraldan
Copy link
Contributor

I have the same problem

@cemsbr
Copy link

cemsbr commented Apr 19, 2016

Same problem.

@cemsbr
Copy link

cemsbr commented Apr 19, 2016

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

@PCManticore
Copy link
Contributor

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.

nicwaller pushed a commit to nicwaller/jira-text that referenced this issue Jan 10, 2017
@sylann
Copy link

sylann commented Oct 10, 2017

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

@brycepg
Copy link
Contributor

brycepg commented Feb 21, 2018

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?

@PCManticore
Copy link
Contributor

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

arinto added a commit to arinto/mlflow that referenced this issue Aug 7, 2018
by ignoring pylint E0202 due to false positive.
Reference: pylint-dev/pylint#414
atodorov added a commit to atodorov/lorax that referenced this issue Oct 24, 2018
atodorov added a commit to atodorov/lorax that referenced this issue Oct 24, 2018
atodorov added a commit to atodorov/lorax that referenced this issue Oct 24, 2018
@nickdallege
Copy link

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

widdowquinn added a commit to widdowquinn/find_differential_primers that referenced this issue Jun 5, 2019
We need to override the json.JSONEncoder default() method, but pylint
sees this as an error - even though it's the documented approach.

See here for other complaints: pylint-dev/pylint#414

See here for justification: https://docs.python.org/3/library/json.html
@Meng-Gen
Copy link

pylint: disable=E0202

pylint: disable=method-hidden will be better.

@brianjmurrell
Copy link

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

jdlesage added a commit to jdlesage/mlflow that referenced this issue Feb 5, 2020
jdlesage added a commit to jdlesage/mlflow that referenced this issue Feb 5, 2020
ernestask added a commit to abrt/faf that referenced this issue Mar 17, 2020
Since the JSONEncoder class conditionally mangles default(), pylint
freaks out and starts spitting out method-hidden warnings. It was
supposedly fixed with pylint-dev/pylint#414, but
seemingly wasn’t.

This commit adds a disabler comment with an explanation.
@jaanisfehling
Copy link

jaanisfehling commented Jun 1, 2023

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

What works is this ugly variant:

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

@Pierre-Sassoulas
Copy link
Member

If you use black to format automatically you'll get this:

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

The disable algorithm is based on the lineno of the affected node given by the ast module and is very unlikely to change.

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

Successfully merging a pull request may close this issue.