Improve QuerySet integer indexing error guidance #2094
+21
−0
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.
Description
This PR improves the error guidance for invalid integer indexing on the lazy
QuerySet.ParamsErrorbranch inQuerySet.__getitem__for integer keys.await queryset.first(),await queryset.offset(n).first(), orawait queryset.all()and index the returned list.test_integer_indexing_not_supportedintests/test_queryset.pyto cover this behavior.No functional change to valid slicing (
queryset[start:stop]).Motivation and Context
QuerySetsupports slicing but not list-style integer indexing.Before this change, users got a generic error that didn’t explain the async/lazy usage pattern clearly.
This update makes the failure mode easier to understand and recover from.
How Has This Been Tested?
uv run --frozen --group test --group contrib --extra psycopg pytest -q tests/test_queryset.py64 passed, 8 skippeduv run --frozen --group dev ruff check tests/test_queryset.py tortoise/queryset.pyAll checks passedChecklist: