feat(orm): type QueryBuilder.get() as Collection[TModel] - #210
Merged
Conversation
Make the ORM Collection generic (Collection[T]) with TYPE_CHECKING-only element-access stubs (first/__iter__/__getitem__), and annotate QueryBuilder.get()/get_models() to return Collection[TModel]. So `User.where(...).get()` now types as Collection[User] instead of Any, and element access (iteration, indexing, first()) yields User. - load() gets one `# type: ignore` (runs only on model collections, but the ORM Collection is legitimately generic over non-model values too). - chunk_by_id uses results[-1] (equivalent; non-emptiness already guaranteed) so the last row is cleanly typed. Stubs are typing-only; runtime behaviour is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
load()'s `else: add_relation` branch was the only uncovered line in Collection.py — no built-in relationship reaches it, since every get_related() returns a Collection for a collection input. Add two tests using a stub relationship whose get_related() returns a non-Collection value, covering both the truthy-mapped and falsy-mapped (stored as None) cases. Collection.py is now at 100%. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
What
Make
QueryBuilder.get()return a typedCollection[TModel]instead of an untyped value, soawait User.where(...).get()types asCollection[User](previouslyAny).Changes
Collectionis now generic (Collection[T]) withTYPE_CHECKING-only element-access stubs (first,__iter__,__getitem__). Runtime behaviour is unchanged — the base class supplies all implementations.QueryBuilder.get()/get_models()annotated to returnCollection[TModel].As a result, element access flows through:
results→Collection[User]results[0]→Userfor u in results→Userresults.first()→User | NoneCollateral (typing surfaced these)
Collection.load()gets one# type: ignore— it only runs on model collections, but the ORMCollectionis legitimately generic over non-model values too (e.g.Collection(relation._get_value(...))), soTcan't be bounded toModel.chunk_by_idswitchedresults.last().get_attributes()→results[-1]...— equivalent (non-emptiness is already guaranteed by thecount_results != 0guard above it) and cleanly typed.Scope
Kept intentionally small: the element stubs are typing-only, and the base
Collectionand the ~35 chainable builder methods were not touched. A chain likeorder_by().limit().get()still yieldsCollection[Unknown](those intermediate methods drop the type param); the user-facingModel.where(...).get()path is fully typed.Verification
tests/masoniteorm/.Collection.py0 errors; the remaining builder errors are all pre-existing (tuplebindings,Nonestr defaults) and unrelated.🤖 Generated with Claude Code