Avoid Pylint warning E1101: 'Instance of .. has no .. member' for class with dynamic attributes

后端 未结 6 1615
走了就别回头了
走了就别回头了 2020-11-27 04:02

Imagine a function which dynamically adds attributes to an object using setattr. The reason for doing so is that I want to map some external structure

相关标签:
6条回答
  • 2020-11-27 04:44

    PyLint gives this type of errors on two cases Link:

    • Used when an object (variable, function, …) is accessed for a non-existent member.

    • False positives: This message may report object members that are created dynamically, but exist at the time they are accessed.

    As this error is identified as E1101 error. You can solve the issue by adding the following line in your code.

    # pylint: disable=E1101
    
    0 讨论(0)
  • 2020-11-27 04:50

    For me just installing pylint-django solved the issue:

    pip install pylint-django
    
    0 讨论(0)
  • 2020-11-27 04:53

    Try this! My problem solved!

    Pylint doesn't understand the Django's dynamic filed. Thus, we need to teach what the Django is to Pylint

    *for vscode in Windows 10 *

    $ pip install pylint-django
    $ cd your_project_folder
    $ code . // run vscode  
    

    Install extension for Python, Django Snippets, Django Template in vscode

    Open .vscode/settings.json in vscode and add:

    {
       "python.linting.pylintEnabled": true,
       "python.linting.enabled": true,
       "python.pythonPath": "venv\\Scripts\\python.exe",
       "python.linting.pylintArgs": [
           "--load-plugins",
           "pylint_django"
       ],
    }
    
    0 讨论(0)
  • 2020-11-27 04:55

    Just to provide the answer that works for me now - as The Compiler suggested you can add a rule for the problematic class in your projects .pylintrc:

    [TYPECHECK]
    ignored-classes=Fysom,MyClass
    
    0 讨论(0)
  • 2020-11-27 04:55

    This page describes the error and gives an easy way to address it directly in the code. tl;dr

    Used when an object (variable, function, …) is accessed for a non-existent member.

    False positives: This message may report object members that are created dynamically, but exist at the time they are accessed.

    A commentor mentions that it can be disabled on a single line at the top of the file with # pylint: disable=no-member. I also found that you can use # pylint: disable=E1101 based on this reddit entry.

    0 讨论(0)
  • 2020-11-27 05:03

    I was able to avoid this warning by adding the __getattr__ method to my class, which python calls when an attribute is not found on an object. Although definitely not the cleanest solution, it worked for my particular use-case as pylint considers the object valid.

    import warnings
    class AppConfiguration(object):     
        ... 
        def __getattr__(self, name):
            ''' will only get called for undefined attributes '''
            warnings.warn('No member "%s" contained in settings config.' % name)
            return ''
    

    More information about the __getattr__ method can be found here.

    0 讨论(0)
提交回复
热议问题