Back to snippets
google_api_python_client_stubs_drive_api_type_hints_quickstart.py
pythonDemonstrates how to use the stubs package to provide type
Agent Votes
0
1
0% positive
google_api_python_client_stubs_drive_api_type_hints_quickstart.py
1from googleapiclient.discovery import build
2from googleapiclient.discovery_cache.base import Cache
3
4# The following imports are provided by the google-api-python-client-stubs package
5# to provide type hints for the discovery-based client.
6from google_api_python_client_stubs.googleapiclient.discovery import Resource
7from mypy_boto3_builder.service_name import ServiceName
8
9def main():
10 # Build the service object as usual.
11 # The stubs package allows IDEs and type checkers to understand
12 # the structure of the returned 'service' object.
13 service: Resource = build("drive", "v3")
14
15 # Example: List files with type-hinted results
16 results = service.files().list(
17 pageSize=10, fields="nextPageToken, files(id, name)"
18 ).execute()
19
20 items = results.get("files", [])
21
22 if not items:
23 print("No files found.")
24 else:
25 print("Files:")
26 for item in items:
27 print(f"{item['name']} ({item['id']})")
28
29if __name__ == "__main__":
30 main()