Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent python pylint complaining about socket class sendall method

Tags:

python

pylint

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?

like image 315
Danny Staple Avatar asked Apr 24 '12 14:04

Danny Staple


2 Answers

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

like image 121
bossylobster Avatar answered Sep 23 '22 12:09

bossylobster


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.

like image 30
sthenault Avatar answered Sep 19 '22 12:09

sthenault