IDE Code completion

IDEs such as VS Code and PyCharm offer built-in code completion for the Python language.

The google-ads-python library generates protobuf message classes dynamically at runtime using getter methods on the GoogleAdsClient class, which can inhibit IDE code completion features that rely on static analysis of source code.

You can improve compatibility with code completion tools that rely on static analysis by importing protobuf message classes directly, instead of using getter methods.

  • Using dynamically imported protobuf message classes. This approach is typically not compatible with code completion tools.

    from google.ads.googleads.client import GoogleAdsClient
    
    client = GoogleAdsClient.load_from_storage()
    
    # The Campaign class is imported dynamically, preventing the IDE from
    # reading the class definition.
    campaign = client.get_type("Campaign")
    
  • Using directly imported protobuf message classes. This approach enables code completion tools to work.

    from google.ads.googleads.v22.resources import Campaign
    
    # The Campaign class is imported directly, enabling the IDE to read the
    # class definition and make code completion suggestions.
    campaign = Campaign()
    

While directly importing generated classes helps with code completion, it also introduces some disadvantages:

  1. It's not always obvious which module a given class is located in, so finding the correct import path can be difficult.
  2. The directory structure of generated classes can change with new versions of the client library. If you import classes directly, your code may break when you upgrade, whereas code that uses getter methods will be unaffected by these types of changes.
  3. The get_service method initializes services before returning them. If you import services directly, you will need to initialize them manually before making requests.