Skip to content

Conversation

@samdolan
Copy link

Description

This PR improves the error guidance for invalid integer indexing on the lazy QuerySet.

  • Adds a dedicated ParamsError branch in QuerySet.__getitem__ for integer keys.
  • Updates the error message to explain lazy evaluation and actionable alternatives:
    await queryset.first(), await queryset.offset(n).first(), or await queryset.all() and index the returned list.
  • Adds test_integer_indexing_not_supported in tests/test_queryset.py to cover this behavior.

No functional change to valid slicing (queryset[start:stop]).

Motivation and Context

QuerySet supports 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.py
    • Result: 64 passed, 8 skipped
  • uv run --frozen --group dev ruff check tests/test_queryset.py tortoise/queryset.py
    • Result: All checks passed

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@samdolan
Copy link
Author

This came up in an interview and the output confused us both a bit. Thought I would pay it forward and have a more discrete error message there.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 11, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing samdolan:fix/queryset-integer-index-guidance (01f2d83) with develop (fe73270)

Open in CodSpeed

@abondar
Copy link
Member

abondar commented Feb 11, 2026

Please fix lint

@coveralls
Copy link

Pull Request Test Coverage Report for Build 21883272953

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.005%) to 82.483%

Files with Coverage Reduction New Missed Lines %
tortoise/queryset.py 1 96.07%
Totals Coverage Status
Change from base Build 21856593682: 0.005%
Covered Lines: 10422
Relevant Lines: 12169

💛 - Coveralls

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.

3 participants