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

Using contextlib.closing() ”removes” all attributes from argument #1437

Open
Marrin opened this Issue Apr 21, 2017 · 6 comments

Comments

Projects
None yet
6 participants
@Marrin

Marrin commented Apr 21, 2017

Given this source:

import sqlite3
from contextlib import closing

with closing(sqlite3.connect(':memory:')) as connection:
    cursor = connection.cursor()

Pylint 1.7.1 finds:

$ pylint test.py
No config file found, using default configuration
************* Module test
C:  5, 0: Trailing whitespace (trailing-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  5, 4: Invalid constant name "cursor" (invalid-name)
E:  5,13: Instance of 'closing' has no 'cursor' member (no-member)

The last error wasn't there with Pylint 1.6.3. It seems the closing() wrapper ”removes” all attributes from its argument although it is just a wrapper that virtually adds the context manager magic methods. This repeats with the cursor object if used with with and closing(), resulting in a block of lines covered in false positives non-existent attributes.

pylint --version output

$ pylint --version
No config file found, using default configuration
pylint 1.7.1, 
astroid 1.5.2
Python 2.7.9 (default, Aug 13 2016, 17:56:53) 
[GCC 4.9.2]
@ignamv

This comment has been minimized.

ignamv commented Jul 11, 2017

Minimal example:

class ContextManager(object):
    def __init__(self, thing):
        self.thing = thing
    def __enter__(self):
        return self.thing
    def __exit__(self, *args):
        return

with ContextManager('hello') as thing:
    print thing.upper()

Result:

$ pylint --disable=C context.py
************* Module context
context.py:10:  E1101  no-member,    Instance of 'ContextManager' has no 'upper' member

------------------------------------------------------------------
Your code has been rated at 4.44/10 (previous run: 4.44/10, +0.00)

If ContextManager has no attributes, pylint generates no errors:

class ContextManager(object):
    def __init__(self, thing):
        pass
    def __enter__(self):
        return 'hello'
    def __exit__(self, *args):
        return

with ContextManager('hello') as thing:
    print thing.upper()

gerrit-ovirt-org pushed a commit to oVirt/vdsm that referenced this issue Dec 13, 2017

pylint: silence no-member error when using contextlib.closing
when upgrading pylint version a few new issues were raised:

00:06:53.873 ************* Module vdsm.v2v
00:06:53.874 E:1368,27: Instance of 'closing' has no 'read' member (no-member)

00:06:53.892 ************* Module vdsm.network.netlink.monitor
00:06:53.893 E:172,58: Instance of 'closing' has no 'poll' member (no-member)

This is a bug in pylint: PyCQA/pylint#1437
pylint does not recognize object attributes when calling 'closing'
context manager.

Change-Id: Ide9737bfde8bc8c5a97fa3ee4e5d821b85f1bfd7
Signed-off-by: Irit Goihman <igoihman@redhat.com>
@ttencate

This comment has been minimized.

ttencate commented Feb 13, 2018

Also stumbled into this. A workaround for the specific contextlib.closing case, which avoids having to add a bunch of # pylint: disable=no-member comments:

connection = sqlite3.connect(':memory:')
with closing(connection):
    cursor = connection.cursor()
@Lucas-C

This comment has been minimized.

Contributor

Lucas-C commented Mar 20, 2018

This is a duplicate of #1437 I think

@ttencate

This comment has been minimized.

ttencate commented Mar 20, 2018

I'm sure it is. It's exactly identical, down to the issue ID ;)

@Lucas-C

This comment has been minimized.

Contributor

Lucas-C commented Mar 20, 2018

Sorry ^^
I meant PyCQA/astroid#347
But this is not really a duplicate, it's just the upstream bug

Thank you very much for the workaround by the way !

aignas added a commit to aignas/omnisharp-vim that referenced this issue May 5, 2018

@pheanex

This comment has been minimized.

Contributor

pheanex commented Nov 15, 2018

Still an issue in:

pylint 2.1.1
astroid 2.0.2
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment