What causes this mysterious pylint E1101 error?

Viewed 360

I have following code:

#!/usr/bin/env python                                                                 
"""pylint behavior test"""                                                            
                                                                                      
                                                                                      
def autodetect_method(method, data):                                                  
    """autodetect method"""                                                           
    if not method:                                                                    
        method = 'POST' if data else 'GET'                                            
    else:                                                                             
        method = method.upper()                                                       
                                                                                      
    return method

pylint produces the following error:

tt.py:10:17: E1101: Class 'method' has no 'upper' member (no-member)

Error is not reported if I rename method variable to something else, f.e. to method_name! So, I know several ways to get rid of this error message. But I am very curious what is so special with variable name method and why the error is generated?

Just in case this problem is version-specific, my versions are:

$ pylint --version
pylint 2.4.4
astroid 2.3.3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0]
1 Answers

There is the way you could satisfy your curiosity - debugging. You could run pylint from the same file and trace its behavior.

if __name__ == "__main__":
    import pylint
    import sys
    sys.argv.append(__file__)
    pylint.run_pylint()

Actually it's easier to say than to do, I've tried, but was unable to understand what exactly happens in details. In high-level terms, pylint tries to infer the type for method in the expression method.upper(), unable to do it itself, falling into astroid library, and there the method type inferred as ClassDef.method. Obviously the word method means something special for astroid, as some other words. For example, the same E1101 error would detected if you use name function instead of method.

Looks like it's behavior somehow relate to builtin's names, but function and method are not builtins. I guess astroid treats them as some kind if "aliases" for ast classes, but I'm not sure.

Related