Typing Stubs for Google Cloud NDB
This package provides static type checking stubs for the `google-cloud-ndb` library, enabling tools like MyPy or PyRight to perform compile-time type validation of NDB code. Maintained as part of the `typeshed` project, it currently targets `google-cloud-ndb==2.4.*`. Updates are automatically released, often daily, to reflect changes in the underlying library.
Warnings
- breaking Migration from the original App Engine NDB (`google.appengine.ext.ndb`) to `google-cloud-ndb` (which these stubs type) requires significant code changes. This library is a standalone client for Cloud Datastore, not a direct drop-in replacement for App Engine apps. Key differences include package name, mandatory `async/await` for most operations, and different client initialization and context management.
- gotcha All NDB database operations (e.g., `put()`, `get()`, `fetch()`) are asynchronous and return 'futures'. Forgetting to `await` these operations will result in the operation not being executed, or returning the future object itself rather than its result.
- gotcha All Datastore interactions must occur within an NDB context. Failing to establish a context (or letting it expire) will raise a `RuntimeError: A context is required for this operation.`
- gotcha The `types-google-cloud-ndb` package provides *only* type annotations. It does not contain any executable code. Importing symbols from `types_google_cloud_ndb` for runtime use will result in `ModuleNotFoundError` or `AttributeError`.
- gotcha While `types-google-cloud-ndb` aims to provide accurate annotations for `google-cloud-ndb==2.4.*`, significant discrepancies between the stub version and the runtime library version can lead to incorrect type checking results or missed errors.
- deprecated Cloud NDB is primarily intended as a migration path for applications moving from App Engine NDB. For *new* Python 3 applications that require Datastore, Google recommends using the native Cloud Datastore client library (`google-cloud-datastore`) instead of Cloud NDB, as it supports newer Firestore in Datastore mode features.
Install
-
pip install types-google-cloud-ndb -
pip install google-cloud-ndb
Imports
- ndb
from google.cloud import ndb
- Client
from google.cloud import ndb client = ndb.Client()
- Model
from google.cloud import ndb class MyModel(ndb.Model): ...
Quickstart
import os
from google.cloud import ndb
import asyncio
# For local development, point to the Datastore emulator
os.environ['DATASTORE_EMULATOR_HOST'] = os.environ.get('DATASTORE_EMULATOR_HOST', 'localhost:8081')
# Ensure your GOOGLE_CLOUD_PROJECT is set, e.g., in your environment or 'your-gcp-project-id'
project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-gcp-project-id')
class User(ndb.Model):
name = ndb.StringProperty()
email = ndb.StringProperty()
joined_date = ndb.DateTimeProperty(auto_now_add=True)
async def main():
client = ndb.Client(project=project_id)
async with client.context():
# Create a user
user = User(name='Alice', email='alice@example.com')
user_key = await user.put()
print(f'Created user with key: {user_key.id()}')
# Fetch the user back by key
fetched_user = await user_key.get_async()
if fetched_user:
print(f'Fetched user: {fetched_user.name} ({fetched_user.email})')
# Query for users
query = User.query(User.name == 'Alice')
users_with_name = await query.fetch_async(limit=1)
if users_with_name:
print(f'Query result: {users_with_name[0].name}')
# Update user
if fetched_user:
fetched_user.email = 'alice.updated@example.com'
await fetched_user.put()
print(f'Updated user: {fetched_user.email}')
# Delete user
await user_key.delete_async()
print(f'Deleted user with key: {user_key.id()}')
if __name__ == '__main__':
asyncio.run(main())