[google-cloud-ndb] Model.get_by_id and get_or_insert should return Self - #16277
Open
miguelsmm wants to merge 1 commit into
Open
[google-cloud-ndb] Model.get_by_id and get_or_insert should return Self#16277miguelsmm wants to merge 1 commit into
miguelsmm wants to merge 1 commit into
Conversation
Contributor
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Model.get_by_idandModel.get_or_insertare declared as returning the base class, so every attribute declared on anndb.Modelsubclass is reported as missing on correct code.With
types-google-cloud-ndb2.4.0.20260724 on Python 3.12:After this change, on the same reproduction:
Writing
Challenge.get_by_id(...)in place ofcls.get_by_id(...)produces the same error before the change.Why Self is the right annotation
Line references below are
google-cloud-ndb2.5.1.get_or_insertconstructs the entity itself.Model._get_or_insert, aliased atmodel.py:6075, callsentity = _cls(**kwargs)on the insert path (line 6168) andotherwise fetches by a key derived from
_cls.get_by_idis one step removed.Model._get_by_id, aliased atmodels shorthand for ``Key(cls, id, ....).get()`` (line 5859) and delegates to_get_by_id_async, which buildsKey(cls._get_kind(), id)and fet What comes back is an instance of the model class registered for thatkind. For an ordinaryModelsubclass the kind is the class name and the registry maps it back to the same class, so the result iscls`.Neither
Modelnor its stub defines__getattr__, so nothing softens the missing attributes today.PolyModel
Selfis not exact forpolymodel.PolyModel.PolyModel._get_kinddot of the hierarchy (polymodel.py:212), andPolyModelinherits both methods unchanged in the runtime and inpolymodel.pyi. So withAnimal -> Feline -> Cat, all three share the kindAnimal, andCat.get_by_id(...)can return aFeline.I think
Selfis still the better annotation.Modelis imprecise for every subclass, which is the common case, whileSelfis imprecise only for aPolyModelsubclass called on a non-root class. If you would rather pin that case override the two methods to returnPolyModel, and I am happy to addthat here.Scope
The
cls: type[Model]annotations have to go forSelfto bind, but only on these two methods. The_asyncvariants keep theirs: they returntasklets_module.Future, which carries no element type. The remainingters sit ongql,queryandallocate_ids, which do not returnentities.
PyCharm bundles this stub, so the false positive also reaches users who never installed
types-google-cloud-ndb.