Giter VIP home page Giter VIP logo

google-ads-stubs's Introduction

Type stubs for the Google Ads API Client Library for Python

PyPI version

This package provides type stubs for the Google Ads API Client Library for Python. It's currently compatible with v23.1.0 of this library (v16 of the API). It allows you to type check usage of the library with e.g. mypy and will also improve autocomplete in many editors.

This is in no way affiliated with Google.

Most stubs were created automatically by stubgen, the rest are handwritten or generated by self-made scripts.

If you find incorrect annotations, please create an issue. Contributions for fixes are also welcome.

Installation

$ pip install google-ads-stubs

The stubs should be automatically picked up by your editor or typechecker, but note that if you're using Mypy you need to use the --namespace-packages option as google and google.ads are namespace packages.

Caveats

The stubs assume the client is constructed with use_proto_plus=True. If you use use_proto_plus=False, they are unlikely to be of much use to you.

Type inference does not work for the get_type method of GoogleAdsClient.The workaround is to explicitly state the type. You can also instantiate the object directly to get inference.

# Replace this:
campaign_operation = client.get_type('CampaignOperation')
# With this:
from google.ads.googleads.v16 import CampaignOperation
campaign_operation: CampaignOperation = client.get_type('CampaignOperation')
# Or this:
from google.ads.googleads.v16 import CampaignOperation
campaign_operation = CampaignOperation()

While it is technically possible to type this method using a combination of overloading and literal types, this is not included in these stubs. The reason is that it requires about 10,000 overloads, which makes most typecheckers fairly slow.

Certain parts of the stubs assume you are using the latest included API version. This includes GoogleAdsClient.enums and calls to GoogleAdsClient.get_type and GoogleAdsClient.get_service without specifying the version argument. If you use an older version, you should import enums directly and specify version in these calls even if you specified it when constructing the client.

Certain types are too lenient compared to what's allowed at runtime. The first argument to protobuf message constructors accepts any mapping. On the other hand certain types are more strict than what's allowed at runtime. You can't substitute a protobuf message for an equivalent dict or an enum with it's equivalent name or value.

# Replace this:
AdGroupAd({
    "status": "ENABLED", 
    "ad": {"type": 2},
})
# With this:
from google.ads.googleads.v16 import AdGroupAdStatusEnum, AdStrengtEnum, AdTypeEnum, Ad
AdGroupAd(
    status=AdGroupAdStatusEnum.AdGroupAdStatus.ENABLED, 
    ad=Ad(type=AdTypeEnum.AdType.TEXT_AD),
)

google-ads-stubs's People

Contributors

henribru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

google-ads-stubs's Issues

feature request: Support for v16 of Google API

v16 of Google Ads API was released a few weeks back and exposes a new set of modules, .v16, for most types of the google-ads package. Here's the release notes: https://github.com/googleads/google-ads-python/releases/tag/23.1.0

Importing from the new v16 modules results in mypy complaining, even with this stubs library present:

tap_googleads/streams/base.py:5: error: Skipping analyzing "google.ads.googleads.v16": module is installed, but missing library stubs or py.typed marker  [import-untyped]

No huge changes seem present in the new API version, but new users will likely use .v16 in their code and will be confused by this library not working out of the box.

Thanks for the great work! This is a great resource to run in tandem with mypy to minimize "dumb" bugs in production.

Support versions past v9

Unfortunately this involves throwing away almost all of the stubs and redoing them as v10 completely changed how protobuf objects are represented ... Can't use mypy-protobuf either, would have to write a custom stub generator for proto-plus-python.

Technically the existing work could be used for the use_proto_plus=False case, but I suspect most people will use use_proto_plus=True.

Issues using stubs

Hi,

I'd be really to stoked to have typechecking for google-ads so interested in getting this working.

Using versions:

google-ads==5.1.0
google-ads-stubs==3.1.0

Currently having the following issues:

(Partially resolved issue) 1. re namespace_packages

With config:

[mypy]
namespace_packages = True

getting errors like

some/file.py:2: error: Module 'google.cloud' has no attribute 'bigquery'  [attr-defined]
some/file2.py:9: error: Module 'google.cloud' has no attribute 'storage'  [attr-defined]

Making namespace_packages = False avoids these errors.

Unresolved 2. re standard usage

With an example file like:

from google.ads import google_ads
def get_client() -> google_ads.client.GoogleAdsClient:
    """Get an authenticated Google Ads client."""
   ... [pulling credentials] ...
   return google_ads.client.GoogleAdsClient.load_from_dict(credentials)  

gives error:

example/file.py:2: error: Name 'google_ads.client.GoogleAdsClient' is not defined  [name-defined]
example/file.py:5 error: Module has no attribute "client"  [attr-defined]

Any ideas?

Incompatibility with PyCharm / VSC Pylance

Following the first issue of this repository #1, I've discovered that my import failures on Pycharm and VSC where coming from the editor themselves and not this package.

For reference, here are the opened tickets on the subject for PyCharm:

I was unable to locate similar issues for Pylance. If someone have it working in Visual, please let me know :)

As it seems unlikely that it will be fixed in the near future, would you accept a PR with a note on the IDEs current limitation and non implementation of PEP-561 in the 'Caveats' section of the package README ? It seems to me like a major pain point for users of this package, and something I would like to know before using it.

Thanks !

Ideas on using stubs alongside other google packages

Hello, me again.
These stubs have been awesome. Thanks again.

A little while ago I noticed PyCharm complains about missing imports in other, non ads, google packages. Guessing due to their namespacing pattern of having completely separate pypi packages install in the same google/ folder.

As a workaround, I've just been uninstalling these stubs when doing non ads related work. Which works no problem, so this isn't anything important.

I was just curious if you knew any workarounds to make these coexist.

Screen Shot 2020-02-15 at 8 49 59 PM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.