Skip to content

chore: add generator return type hints to query/llm/text_utils.py#2329

Open
ilias-laoukili wants to merge 2 commits intomicrosoft:mainfrom
ilias-laoukili:chore/type-hints-text-utils
Open

chore: add generator return type hints to query/llm/text_utils.py#2329
ilias-laoukili wants to merge 2 commits intomicrosoft:mainfrom
ilias-laoukili:chore/type-hints-text-utils

Conversation

@ilias-laoukili
Copy link
Copy Markdown

@ilias-laoukili ilias-laoukili commented Apr 21, 2026

Description

Add missing return type hints to two generator functions in packages/graphrag/graphrag/query/llm/text_utils.py.

Related Issues

Closes #2330

Proposed Changes

  • batched(iterable: Iterator, n: int) → adds -> Iterator[tuple]
  • chunk_text(text: str, max_tokens: int, tokenizer: Tokenizer | None = None) → adds -> Iterator[str]

Iterator was already imported from collections.abc; no new imports required.

Checklist

  • I have tested these changes locally.
  • I have reviewed the code changes.
  • I have updated the documentation (if necessary).
  • I have added appropriate unit tests (if applicable).

Additional Notes

Verified with mypy packages/graphrag/graphrag/query/llm/text_utils.py --ignore-missing-imports — no issues. The pre-existing import-not-found errors in the baseline are caused by uninstalled monorepo sub-packages and are unrelated to this change.

@ilias-laoukili ilias-laoukili requested a review from a team as a code owner April 21, 2026 01:48
Copilot AI review requested due to automatic review settings April 21, 2026 01:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds explicit return type hints for generator utilities in graphrag/query/llm/text_utils.py to improve static type checking in the LLM/RAG pipeline.

Changes:

  • Annotate batched(...) to explicitly return an iterator of batches.
  • Annotate chunk_text(...) to explicitly return an iterator of text chunks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +20 to 22
def batched(iterable: Iterator, n: int) -> Iterator[tuple]:
"""
Batch data into tuples of length n. The last batch may be shorter.
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

batched is annotated as returning Iterator[tuple], but tuple is a generic type and leaving it un-parameterized loses most of the downstream type-safety (and may trigger "missing type parameters" warnings in stricter mypy/pyright configs). Since this helper preserves element type, consider making it generic (e.g., T = TypeVar('T')) and typing the signature as iterable: Iterable[T] (or Iterator[T]) with a return of Iterator[tuple[T, ...]] to accurately reflect variable-length batches while keeping element types.

Copilot uses AI. Check for mistakes.
@ilias-laoukili
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

Missing return type hints on generator functions in query/llm/text_utils.py

2 participants