Search code examples
pythonpython-3.xvisual-studio-codeaws-lambdapylint

Pylint and classes that share the same name showing no member error


I've have a project that contains multiple AWS Lambdas. Each Lambda has their own Config class which will have different members depending on the Lambda. The issue I'm having is that Pylint is showing "Instance of class has no member" (E1101) error because its picking the Config class from a different Lambda. Is there a way to force it to select the class within its directory? I'm using VSCode and the structure of my project is as follows:

Lambda1 (folder)
 |_ config.py
 |_ lambda_function.py 
Lambda2 (folder)
 |_ config.py
 |_ lambda_function.py 

Solution

  • I managed to get it working by using relative import, so instead of

    from config import Config

    do

    from .config import Config

    I also need to add an empty __init__.py file:

     Lambda1 (folder)
     |_  __init__.py
     |_  config.py
     |_  lambda_function.py 
    Lambda2 (folder)
     |_  __init__.py
     |_  config.py
     |_  lambda_function.py 
    

    This would get Pylint to detect the correct class.

    The next issue was that these changes would cause the lambda to no longer work.

    To get the lambda working again I added a folder within each lambda folder like this:

     Lambda1 (folder)
     |_  app (folder)
        |_  __init__.py
        |_  config.py
        |_  lambda_function.py 
    Lambda2 (folder)
     |_  app (folder)
        |_  __init__.py
        |_  config.py
        |_  lambda_function.py 
    

    And updated the lambda handler from this:

    Handler: lambda_function.lambda_handler
    

    to this:

    Handler: app.lambda_function.lambda_handler
    

    I'm not too happy that I need to restructure the files and update the lambda handler, so will continue looking at alternative solutions to avoid this.