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

Incorrect handling of property overriding in subclasses. #844

Closed
dcbaker opened this issue Mar 10, 2016 · 7 comments
Closed

Incorrect handling of property overriding in subclasses. #844

dcbaker opened this issue Mar 10, 2016 · 7 comments
Labels
Minor 💅 Polishing pylint is always nice

Comments

@dcbaker
Copy link

dcbaker commented Mar 10, 2016

pylint: 1.5.4
astroid 1.4.4
python: 2.7.11, 3.3.6, 3.5.1

If a class wants to override the getter or setter of a property in the parent class, then pylint incorrectly marks it as a "no-member" error, the code runs and works as expected.

class Parent(object):
    def __init__(self):
        self.__thing = 'foo'

    @property
    def thing(self):
        return self.__thing


class Child(Parent):
    @Parent.thing.getter
    def thing(self):
        return super(Child, self).thing + '!'


print(Child().thing)

I believe that the corollary to this case is that the following case is invalid, but marked as valid by pylint:

class Parent(object):
    def __init__(self):
        self.__thing = 'foo'

    @property
    def thing(self):
        return self.__thing


class Child(Parent):
    @thing.getter
    def thing(self):
        return super(Child, self).thing + '!'


print(Child().thing)
@PCManticore
Copy link
Contributor

True, but this seems like an edge right now. We might improve it though with Pylint / astroid 2.0.

@PCManticore PCManticore added the Minor 💅 Polishing pylint is always nice label Mar 21, 2016
@leynos
Copy link

leynos commented Apr 27, 2016

This seems to suddenly have started affecting my Tornado RequestHandler subclass since installing pylint-1.5.5. I get a slew of no-member errors from field accesses to objects returned by self.current_user (which is a property). Downgrading to pylint-1.5.4 and astroid-1.4.4 fixes this.

Since the OP reports this as affecting pylint-1.5.4 and astroid-1.4.4, I wonder if I am experiencing a separate unrelated issue, or if the two are connected.

@PCManticore
Copy link
Contributor

Can you show me an example of how the RequestHandler subclass looks with respect to the current_user property? I'm trying to see if it is the same issue as this one.

@leynos
Copy link

leynos commented Apr 27, 2016

An example of this:

from tornado.web import RequestHandler

class User(object):

    def __init__(self):
        self.foo = 'foo'
        self.bar = 'bar'
        self.qux = 'qux'

class MyRequestHandlerSubclass(RequestHandler):

    def get_current_user(self):
        return User()

class TestRequestHandler(MyRequestHandlerSubclass):

    def get(self):

        print(self.current_user.foo)

The method get_current_user() is called by the current_user property and cached. (See: http://www.tornadoweb.org/en/stable/_modules/tornado/web.html#RequestHandler.get_current_user).

The following is displayed by pylint:

E: 19,14: Method 'current_user' has no 'foo' member (no-member)

@sscherfke
Copy link

With Tornado 6, calling methods like tornado.web.RequestHandler.get_argument('name') leads to E1120: No value for argument 'default' in method call (no-value-for-parameter) errors b/c of their overload decorators

@backbord
Copy link

I think this related to #1623 and thusly to #1581

@sscherfke
Copy link

A workaround for Tornado 6 is the following astroid brain:

# astroid/brain/brain_tornado.py
"""
Astroid hooks for tornado.

"""
import astroid


def requesthandler_funcs():
    return astroid.parse(
        """
        from tornado.web import RequestHandler

        class RequestHandlerMock(RequestHandler):
            def get_argument(name, default=None, strip=True):
                return object()
        """
    )


astroid.register_module_extender(astroid.MANAGER, 'tornado.web', requesthandler_funcs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Minor 💅 Polishing pylint is always nice
Projects
None yet
Development

No branches or pull requests

5 participants