11

I have a bit of code using a simple tcp socket setup to test something. We run pylint --errors-only on our python files, generally as a way to validate all our code.

However, the simple example code given on the python socket library documentation - http://docs.python.org/library/socket.html - will output:

************* Module SocketExample
E: 16: Instance of '_socketobject' has no 'recv' member
E: 18: Instance of '_socketobject' has no 'sendall' member

The documentation shows these members, the code runs and works.

A dir of socket shows them existing too:

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> dir(s)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_sock', 'accept', 'bind', 'close', 'connect', 'connect_ex', 'dup', 'family', 'fileno', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout', 'listen', 'makefile', 'proto', 'recv', 'recv_into', 'recvfrom', 'recvfrom_into', 'send', 'sendall', 'sendto', 'setblocking', 'setsockopt', 'settimeout', 'shutdown', 'type']

This is down to the snippet:

   for method in _delegate_methods:
        setattr(self, method, getattr(_sock, method))

In the socket.py implementation.

Can pylint be made to accept this style (and validate it) or is the only choice to ignore the "no member" warnings with # pylint: disable-msg=E1101?

Danny Staple
  • 7,101
  • 4
  • 43
  • 56

2 Answers2

10

You can use pylint --errors-only --ignored-classes=_socketobject or add

[TYPECHECK]
ignored-classes=SQLObject,_socketobject

to your ~/.pylintrc file.

From the documenation,

ignored-classes:

List of classes names for which member attributes should not be checked (useful for classes with attributes dynamically set).

Default: SQLObject

Nick T
  • 25,754
  • 12
  • 83
  • 121
bossylobster
  • 9,993
  • 1
  • 42
  • 61
  • Adding that to the rc file makes plenty of sense. It means that the object gets no checking at all, but is nicer than local disables. – Danny Staple Apr 24 '12 at 20:56
  • Calling this the answer - because it will work today. sthenault's answer is good - but will require more effort. It is a noble effort though - get it into github and the pylint using community can start to contribute core and common lib parts. – Danny Staple Apr 24 '12 at 20:57
  • 1
    Yes my answer felt hacky in comparison, and certainly too broad for what we really need, but hopefully not so broad as to cause problems. – bossylobster Apr 25 '12 at 00:36
1

using the trick defined in http://www.logilab.org/blogentry/78354, we could start adding to pylint a generic astng plugin that would help him to understand such things from the stdlib (there is also various tickets/comments about hashlib in the tracker).

That would be a great improvment indeed. Any volunteer ? :)

Beside this, I don't think there is other options than disabling the message.

sthenault
  • 14,397
  • 5
  • 38
  • 32
  • I like this - it is a very flexible approach, and could also make pylint more friendly with things like Django setups. – Danny Staple Apr 24 '12 at 20:55