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.

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.

1 participant