Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pylint: Class 'message' has no 'startswith' member

For some reason, pylint 1.6.4 (astroid 1.4.9) does not like this:

try:
    some_package.their_function()
except Exception as ex:
    if ex.message.startswith(...):
        ...

It complains:

error (E1101, no-member, feed_sentiment) Class 'message' has no 'startswith' member

I find this surprising because:

>>> type(Exception("foo").message)
<type 'str'>
>>> Exception("foo").message.startswith
<built-in method startswith of str object at 0x10520d360>

I think this is a bug in pylint.

However, am I doing something wrong? What is the "pythonic" way here?

PS. Yes, I know that the right way is to define my own exception subclass, but I have no control over some_package.

PPS. Yes, I know I can annotate the code with pylint: disable=no-member.

like image 896
sds Avatar asked Nov 08 '22 02:11

sds


1 Answers

This is indeed a bug in astroid - a pylint's internal library used for building abstract syntax trees and value inference.

import astroid

node = astroid.builder.parse("""
    ex = Exception()
    msg = ex.message
""")
print list(node.locals['msg'][0].infer())

Output from this code snippet is:

[<ClassDef(message) l.0 [exceptions] at 0x34aadd0>, <ClassDef(message) l.0 [exceptions] at 0x3482cb0>]

Output means that message attribute on exception instance is inferred as custom class definition, and not string instance.

Thanks for submitting a bug!

like image 86
Łukasz Rogalski Avatar answered Nov 14 '22 21:11

Łukasz Rogalski