Skip to content

[google-cloud-ndb] Add set_if_not_exists to global caches - #16568

Merged
srittau merged 2 commits into
python:mainfrom
Blacksujit:ndb-global-cache-set-if-not-exists
Oct 20, 2026
Merged

srittau merged 2 commits into
python:mainfrom
Blacksujit:ndb-global-cache-set-if-not-exists

Conversation

@Blacksujit

Copy link
Copy Markdown
Contributor

GlobalCache.set_if_not_exists is an abstract method at runtime, but it was missing from the stub, so type checkers did not flag custom GlobalCache subclasses that forget to implement it (instantiating one fails with TypeError at runtime).

This adds the abstract method and the implementations on RedisCache, MemcacheCache and _InProcessGlobalCache, typed from the docstring: a mapping of keys to serialized entities in, a dict of key to "was set" flags out.

Upstream (v2.4.0, unchanged in 2.4.2): https://github.com/googleapis/python-ndb/blob/v2.4.0/google/cloud/ndb/global_cache.py#L117-L132

AI agent used: Claude Code (claude-opus-5-5).

@github-actions

This comment has been minimized.

@abc.abstractmethod
def set(self, items, expires=None): ...
@abc.abstractmethod
def set_if_not_exists(self, items: Mapping[bytes, bytes | None], expires: float | None = None) -> dict[bytes, bool]: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapping should be avoided, since it's not a protocol, but a weird ABC. This could use SupportsItems (from _typeshed).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, it now uses SupportsItems[bytes, bytes | None]. All four implementations only call items.items().

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit f5af067 into python:main Oct 20, 2026
74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants