googleads / google-ads-python

Google Ads API Client Library for Python
Apache License 2.0
526 stars 480 forks source link

Make library compatible with type checking and auto-completion #695

Open g3or3 opened 2 years ago

g3or3 commented 2 years ago

I noticed there are types available as part of the google ads client and I was wondering what the intended application is for them?

from google.ads.googleads.v11.services.types

I've tried using them to enforce type checking with mypy, e.g:

from google.ads.googleads.v11.services.types.customizer_attribute_service import CustomizerAttributeOperation

operations: List[CustomizerAttributeOperation] = [1, 2, 'banana'] # no error

However this is not raised as an error with mypy. Besides auto-completion is there something I'm missing as an intended benefit? Also, is there a way to make this work with mypy to enforce type checking?

Thank you!

BenRKarl commented 2 years ago

Hi @g3or3 thanks for the question - those are the types that are required by the adjacent service classes. Most of the operation, request, and response classes are defined there.

It's a bit complicated to ensure that the library is compatible with mypy type checking. All of the API-specific code (i.e. anything under the v* directories here) is generated, so to alter the structure of that portion of the library would require a huge amount of work that would affect other libraries at Google that rely on the code generator.

That said, I'll mark this as a feature request and see if we can make the library structure compatible with type checking/auto-completion in the future.

g3or3 commented 2 years ago

Thank you for the insight! Looking forward to this.

iRod3s commented 1 year ago

This would be much appreciated. The way my team is handing this problem is using https://github.com/henribru/google-ads-stubs to improve our development velocity, but an officially supported solution would be great

henribru commented 1 year ago

@g3or3 The reason Mypy doesn't complain about your code is that this package doesn't declare itself as supporting typechecking using a py.typed file. In the absence of this marker, Mypy treats anything imported from the package as Any, regardless of whether the package has type annotations. As far as I'm aware there's no way to force Mypy to look at the annotations in this case. Anyone who'd rather not use google-ads-stubs might have more luck with Pyright, I think the useLibraryCodeForTypes setting allows this. Though note that only the services in this package have actual type annotations, not the protobuff classes like CustomizerAttributeOperation. You should be able to get some basic typechecking for those using https://pypi.org/project/proto-plus-stubs/ (disclaimer: also my package).

It would be great not having to maintain google-ads-stubs anymore, so if I can help move this along in the future I'd be happy to, e.g. by upstreaming any work.