Skip to content

Add precise type annotations to search helpers - #5140

Open
Tanishq-mellu wants to merge 1 commit into
OWASP:mainfrom
Tanishq-mellu:fix/search-helper-typing
Open

Add precise type annotations to search helpers#5140
Tanishq-mellu wants to merge 1 commit into
OWASP:mainfrom
Tanishq-mellu:fix/search-helper-typing

Conversation

@Tanishq-mellu

@Tanishq-mellu Tanishq-mellu commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds precise type annotations to the Algolia search helpers by specifying string element types for search attribute lists and defining concrete TypedDict contracts for search result hits.

The changes cover all search helpers that directly use algoliasearch_django.raw_search and avoid Any in their public return annotations.

Related issue

Fixes #5080

Changes

  • Type attributes and searchable_attributes as list[str] | None.
  • Add TypedDict definitions for GitHub user and OWASP chapter, committee, issue, and project search hits.
  • Type each search helper with its concrete Algolia search result shape instead of dict[str, Any].
  • Handle nullable user location and company fields in the Slack user search handler.
  • Add regression coverage for nullable user metadata.

Testing

  • pre-commit run ruff-check --files ... — passed
  • pre-commit run ruff-format --files ... — passed
  • pre-commit run mypy --all-files — passed
  • Targeted search helper unit tests — 18 passed
  • git diff --check upstream/main..HEAD — passed

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Backend search helpers now return typed search result payloads with narrower list annotations. Slack user block rendering now defaults missing location and company values to empty strings, with a unit test covering None metadata.

Changes

Search result typing updates

Layer / File(s) Summary
Search return types
backend/src/apps/common/index_types.py, backend/src/apps/github/index/search/user.py, backend/src/apps/owasp/index/search/chapter.py, backend/src/apps/owasp/index/search/committee.py, backend/src/apps/owasp/index/search/issue.py, backend/src/apps/owasp/index/search/project.py
Shared TypedDict search result shapes are added, and the search helpers import them under TYPE_CHECKING while updating return annotations and list parameter types.

Slack user metadata fallback

Layer / File(s) Summary
Metadata defaults and test
backend/src/apps/slack/common/handlers/users.py, backend/tests/unit/apps/slack/common/handlers/users_test.py
location and company now fall back to empty strings before escaping, and a unit test verifies the rendered block text omits those labels when metadata is None.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR addresses the issue by tightening search helper types, adding concrete TypedDict results, and fixing nullable Slack metadata handling.
Out of Scope Changes check ✅ Passed All changes support the stated typing and regression goals, with no unrelated code paths introduced.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title clearly summarizes the main change: tightening type annotations for search helpers.
Description check ✅ Passed The description matches the changeset and accurately describes the typed search helpers, TypedDicts, and Slack handling updates.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 6, 2026
cubic-dev-ai[bot]
cubic-dev-ai Bot previously approved these changes Jul 6, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

Contribution validation failed:

  • commit_sign_off: One or more commits are missing or have an invalid Signed-off-by trailer.

@Tanishq-mellu
Tanishq-mellu force-pushed the fix/search-helper-typing branch from 814a5ee to 7443044 Compare July 6, 2026 08:16
@Tanishq-mellu
Tanishq-mellu marked this pull request as ready for review July 6, 2026 08:36
Comment thread backend/src/apps/owasp/index/search/chapter.py Outdated
@Tanishq-mellu

Copy link
Copy Markdown
Contributor Author

I checked the underlying algoliasearch-django implementation. raw_search() returns SearchResponse.to_dict(), whose upstream annotation is Dict[str, Any], while search_single_index() itself returns the typed SearchResponse model. Would you prefer a shared local TypedDict for the raw search result contract here or should these helpers preserve the upstream dictionary return type ?

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
backend/src/apps/github/index/search/user.py (1)

31-33: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Update the docstring return type to match the annotation.

The Returns section still says dict but the function now returns UserSearchResult. This same inconsistency exists across all search helper files in this PR.

📝 Proposed docstring update
     Returns:
-        dict: Search results containing users and metadata.
+        UserSearchResult: Search results containing users and metadata.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/apps/github/index/search/user.py` around lines 31 - 33, The
docstring return type is inconsistent with the function annotation in the user
search helper. Update the Returns section in the search helper docstrings,
including the one in user-related search functions, to match the actual return
type `UserSearchResult` instead of `dict`; apply the same correction across the
other search helper files in this PR so the documented and annotated return
types stay aligned.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@backend/src/apps/github/index/search/user.py`:
- Around line 31-33: The docstring return type is inconsistent with the function
annotation in the user search helper. Update the Returns section in the search
helper docstrings, including the one in user-related search functions, to match
the actual return type `UserSearchResult` instead of `dict`; apply the same
correction across the other search helper files in this PR so the documented and
annotated return types stay aligned.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: a9f95f17-355d-42bb-b563-4923a6975e55

📥 Commits

Reviewing files that changed from the base of the PR and between 7443044 and 068df81.

📒 Files selected for processing (8)
  • backend/src/apps/common/index_types.py
  • backend/src/apps/github/index/search/user.py
  • backend/src/apps/owasp/index/search/chapter.py
  • backend/src/apps/owasp/index/search/committee.py
  • backend/src/apps/owasp/index/search/issue.py
  • backend/src/apps/owasp/index/search/project.py
  • backend/src/apps/slack/common/handlers/users.py
  • backend/tests/unit/apps/slack/common/handlers/users_test.py

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 8, 2026
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

Contribution validation failed:

  • commit_sign_off: One or more commits are missing or have an invalid Signed-off-by trailer.

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

Contribution validation failed:

  • commit_sign_off: One or more commits are missing or have an invalid Signed-off-by trailer.

@Tanishq-mellu

Copy link
Copy Markdown
Contributor Author

Addressed the Any concern by replacing the generic search result annotation with concrete TypedDict contracts for each search hit shape.
This also surfaced that idx_location and idx_company may be None in the Slack user search consumer, so I handled those nullable values explicitly and added regression coverage for that case.
mypy, Ruff, and the targeted tests pass. Would appreciate a re-review when you have a chance.

@Tanishq-mellu
Tanishq-mellu force-pushed the fix/search-helper-typing branch from a7ec75b to 7a4c575 Compare July 8, 2026 07:29

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
backend/src/apps/github/index/search/user.py (1)

32-32: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Update the docstring return type.

The Returns section still says dict: but the return annotation is now UserSearchResult. This should be updated to match, keeping the docstring accurate for IDE support — a stated goal of this PR.

📝 Proposed fix
     Returns:
-        dict: Search results containing users and metadata.
+        UserSearchResult: Search results containing users and metadata.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/apps/github/index/search/user.py` at line 32, The docstring
return type for UserSearchResult in the user search module is outdated and still
says dict instead of the current annotated type. Update the Returns section in
the docstring for the search user function or method to reference
UserSearchResult so it matches the actual return annotation and keeps IDE/type
hints accurate.
♻️ Duplicate comments (4)
backend/src/apps/owasp/index/search/chapter.py (1)

33-33: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Update the docstring return type.

Same stale dict: docstring issue as user.py — should say ChapterSearchResult.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/apps/owasp/index/search/chapter.py` at line 33, Update the return
type in the docstring for the chapter search function so it uses
ChapterSearchResult instead of dict, matching the actual result type. Locate the
docstring in the search chapter module by the ChapterSearchResult-related search
function and replace the stale dict annotation with the correct type name.
backend/src/apps/owasp/index/search/committee.py (1)

31-31: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Update the docstring return type.

Same stale dict: docstring issue — should say CommitteeSearchResult.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/apps/owasp/index/search/committee.py` at line 31, The docstring
on the committee search result needs its return type updated from the stale dict
annotation to CommitteeSearchResult. Update the return description in the
relevant docstring for the committee search function or method in committee.py
so it matches the actual return type and aligns with the surrounding search
result symbols.
backend/src/apps/owasp/index/search/project.py (1)

33-33: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Update the docstring return type.

Same stale dict: docstring issue — should say ProjectSearchResult.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/apps/owasp/index/search/project.py` at line 33, The docstring for
the project search result type is stale and still says dict instead of the
actual return type. Update the return annotation text in the docstring
associated with the search project result function/class in project.py to use
ProjectSearchResult, and make sure any nearby docstrings in the same search
module use the same concrete type naming.
backend/src/apps/owasp/index/search/issue.py (1)

35-35: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Update the docstring return type.

Same stale dict: docstring issue — should say IssueSearchResult.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/apps/owasp/index/search/issue.py` at line 35, The docstring for
the search result return type is stale and still says dict instead of
IssueSearchResult. Update the return annotation text in the relevant docstring
for the search helper in issue.py so it matches the actual return type, and keep
the surrounding documentation aligned with the IssueSearchResult symbol used by
the search API.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@backend/src/apps/github/index/search/user.py`:
- Line 32: The docstring return type for UserSearchResult in the user search
module is outdated and still says dict instead of the current annotated type.
Update the Returns section in the docstring for the search user function or
method to reference UserSearchResult so it matches the actual return annotation
and keeps IDE/type hints accurate.

---

Duplicate comments:
In `@backend/src/apps/owasp/index/search/chapter.py`:
- Line 33: Update the return type in the docstring for the chapter search
function so it uses ChapterSearchResult instead of dict, matching the actual
result type. Locate the docstring in the search chapter module by the
ChapterSearchResult-related search function and replace the stale dict
annotation with the correct type name.

In `@backend/src/apps/owasp/index/search/committee.py`:
- Line 31: The docstring on the committee search result needs its return type
updated from the stale dict annotation to CommitteeSearchResult. Update the
return description in the relevant docstring for the committee search function
or method in committee.py so it matches the actual return type and aligns with
the surrounding search result symbols.

In `@backend/src/apps/owasp/index/search/issue.py`:
- Line 35: The docstring for the search result return type is stale and still
says dict instead of IssueSearchResult. Update the return annotation text in the
relevant docstring for the search helper in issue.py so it matches the actual
return type, and keep the surrounding documentation aligned with the
IssueSearchResult symbol used by the search API.

In `@backend/src/apps/owasp/index/search/project.py`:
- Line 33: The docstring for the project search result type is stale and still
says dict instead of the actual return type. Update the return annotation text
in the docstring associated with the search project result function/class in
project.py to use ProjectSearchResult, and make sure any nearby docstrings in
the same search module use the same concrete type naming.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d616669c-b515-4ad1-9e14-65c319603819

📥 Commits

Reviewing files that changed from the base of the PR and between 068df81 and 7a4c575.

📒 Files selected for processing (8)
  • backend/src/apps/common/index_types.py
  • backend/src/apps/github/index/search/user.py
  • backend/src/apps/owasp/index/search/chapter.py
  • backend/src/apps/owasp/index/search/committee.py
  • backend/src/apps/owasp/index/search/issue.py
  • backend/src/apps/owasp/index/search/project.py
  • backend/src/apps/slack/common/handlers/users.py
  • backend/tests/unit/apps/slack/common/handlers/users_test.py

@Tanishq-mellu

Copy link
Copy Markdown
Contributor Author

dict[str, Any] result contract with concrete TypedDict search-hit shapes, handled the nullable metadata that stricter typing exposed and added regression coverage.

@Tanishq-mellu
Tanishq-mellu requested a review from arkid15r July 8, 2026 07:39
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

Contribution validation failed:

  • commit_sign_off: One or more commits are missing or have an invalid Signed-off-by trailer.

@Tanishq-mellu
Tanishq-mellu force-pushed the fix/search-helper-typing branch from 7a94331 to 89e8819 Compare July 9, 2026 06:36

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/src/apps/common/index_types.py`:
- Around line 8-64: The `*SearchHit` TypedDicts in `index_types.py` are missing
several fields that the corresponding search helpers return by default, so
update `UserSearchHit`, `ChapterSearchHit`, `CommitteeSearchHit`,
`IssueSearchHit`, and `ProjectSearchHit` to include every default
`attributesToRetrieve` entry. Add the absent fields from `get_users`,
`get_chapters`, `get_committees`, `get_issues`, and `get_projects` (for example
`idx_email`, `idx_created_at`, `idx_updated_at`, `idx_related_urls`,
`idx_comments_count`, `idx_level`, `idx_topics`, etc.), and verify the exact
types for schema-dependent fields like `idx_top_contributors`,
`idx_related_urls`, `idx_hint`, and `idx_labels` against the Algolia index
schema. Keep the TypedDict definitions aligned with the search helper outputs so
future callers can access default fields without mypy errors.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4bfe9903-4ced-4ca9-843c-29e04bc64b6e

📥 Commits

Reviewing files that changed from the base of the PR and between 7a4c575 and 89e8819.

📒 Files selected for processing (8)
  • backend/src/apps/common/index_types.py
  • backend/src/apps/github/index/search/user.py
  • backend/src/apps/owasp/index/search/chapter.py
  • backend/src/apps/owasp/index/search/committee.py
  • backend/src/apps/owasp/index/search/issue.py
  • backend/src/apps/owasp/index/search/project.py
  • backend/src/apps/slack/common/handlers/users.py
  • backend/tests/unit/apps/slack/common/handlers/users_test.py

Comment thread backend/src/apps/common/index_types.py
coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 9, 2026
@github-actions

Copy link
Copy Markdown

Contribution validation failed:

  • commit_sign_off: One or more commits are missing or have an invalid Signed-off-by trailer.

Signed-off-by: Tanishq Meshram <tnshqmeshram@gmail.com>
@github-actions

Copy link
Copy Markdown

Contribution validation failed:

  • commit_sign_off: One or more commits are missing or have an invalid Signed-off-by trailer.

@sonarqubecloud

Copy link
Copy Markdown

@Tanishq-mellu

Copy link
Copy Markdown
Contributor Author

I reworked the search helpers to avoid exposing Any in the public return types by introducing concrete TypedDict definitions for the Algolia search results (index_types.py) and using those as the return types across the search helpers.

Before I iterate further, I'd like to check whether this is the direction you'd prefer. Is using dedicated TypedDict models for these search results acceptable here, or would you rather see a different typing approach (for example, a shared generic type or another pattern already used in the project) ?

I'm happy to refactor it to match the project's preferred typing style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve backend type annotations and re-enable stricter type checking

2 participants