Skip to content

Commit

Permalink
Add stubs for GoogleAdsClient.enums
Browse files Browse the repository at this point in the history
  • Loading branch information
henribru committed Nov 12, 2023
1 parent 6ee6475 commit 1fd24fc
Show file tree
Hide file tree
Showing 2 changed files with 672 additions and 6 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ The stubs should be automatically picked up by your editor or typechecker, but n


## 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.

There are some caveats. The primary one is that type inference does _not_ work for the `get_type`
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.

```python
Expand All @@ -35,25 +36,24 @@ campaign_operation: CampaignOperation = client.get_type('CampaignOperation')
from google.ads.googleads.v15 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 types are too lenient compared to what's allowed at runtime. `GoogleAdsClient.enums` is typed as `Any` and the first argument to protobuf message constructors accepts any mapping.
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.

```python
# Replace this:
AdGroupAd({
"status": "ENABLED",
"ad_strength": client.enums.AdStrengtEnum.GOOD,
"ad": {"type": 2},
})
# With this:
from google.ads.googleads.v15 import AdGroupAdStatusEnum, AdStrengtEnum, AdTypeEnum, Ad
AdGroupAd(
status=AdGroupAdStatusEnum.AdGroupAdStatus.ENABLED,
ad_strength=AdStrengtEnum.AdStrengt.GOOD,
ad=Ad(type=AdTypeEnum.AdType.TEXT_AD),
)
```
Loading

0 comments on commit 1fd24fc

Please sign in to comment.