brain_boto3.py 921 B

1234567891011121314151617181920212223242526272829
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. """Astroid hooks for understanding boto3.ServiceRequest()"""
  4. from astroid import extract_node
  5. from astroid.manager import AstroidManager
  6. from astroid.nodes.scoped_nodes import ClassDef
  7. BOTO_SERVICE_FACTORY_QUALIFIED_NAME = "boto3.resources.base.ServiceResource"
  8. def service_request_transform(node):
  9. """Transform ServiceResource to look like dynamic classes"""
  10. code = """
  11. def __getattr__(self, attr):
  12. return 0
  13. """
  14. func_getattr = extract_node(code)
  15. node.locals["__getattr__"] = [func_getattr]
  16. return node
  17. def _looks_like_boto3_service_request(node):
  18. return node.qname() == BOTO_SERVICE_FACTORY_QUALIFIED_NAME
  19. AstroidManager().register_transform(
  20. ClassDef, service_request_transform, _looks_like_boto3_service_request
  21. )