Skip to content

Fix pyright type errors#137

Merged
benjibc merged 1 commit intomainfrom
cursor/fix-pyright-type-errors-aab5
Aug 30, 2025
Merged

Fix pyright type errors#137
benjibc merged 1 commit intomainfrom
cursor/fix-pyright-type-errors-aab5

Conversation

@benjibc
Copy link
Copy Markdown
Contributor

@benjibc benjibc commented Aug 30, 2025


name: Pull Request
about: Propose changes to the codebase
title: "Fix: Address initial Pyright type errors in eval_protocol"
labels: ''
assignees: ''


Description

This PR addresses and resolves several initial Pyright type errors within the eval_protocol package, reducing the overall type error count by more than five. The changes focus on improving type accuracy and handling optional values and external imports.

Key changes include:

  • eval_protocol/playback_policy.py: Removed openai.types.CompletionUsage dependency, updated the return signature of generate_tool_calls to use Optional[Dict[str, int]] for usage and Optional[str] for correlation ID, and removed a duplicate method definition.
  • eval_protocol/utils/static_policy.py: Updated StaticPolicy and RandomPolicy to align with the new PlaybackPolicyBase return signature for generate_tool_calls and made info parameters Optional[Dict[str, Any]].
  • eval_protocol/rewards/accuracy_length.py: Improved robustness by coercing response.content to str, handling optional ground_truth values, and adding a cast for accuracy_reward to resolve decorator-related type inference issues.
  • eval_protocol/server.py: Added a safe cast for legacy tuple return types from reward functions and introduced type: ignore hints for missing external imports (uvicorn, fastapi, pydantic) to suppress errors related to uninstalled development dependencies.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Refactoring/Code cleanup
  • Build/CI/CD related changes
  • Other (please describe):

How Has This Been Tested?

The changes were verified by running the basedpyright type checker:

  • Individually on each modified file (eval_protocol/playback_policy.py, eval_protocol/utils/static_policy.py, eval_protocol/rewards/accuracy_length.py, eval_protocol/server.py).
  • On the entire eval_protocol package to confirm a reduction in overall type errors.

Test Configuration:

  • Toolchain: basedpyright (via pre-commit hook)

Checklist:

  • My code follows the style guidelines of this project (ran black ., isort ., flake8 .)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works (type checks)
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Screenshots (if applicable)

N/A

Additional context

This PR addresses the initial set of type errors as requested. Further work could include:

  • Addressing additional Union[str | List[ChatCompletionContentPartTextParam]] string-coercion issues in other reward files (e.g., length.py, math.py, language_consistency.py).
  • Optionally ignoring or vendor-gating external import checks in adapter/agent paths where runtime dependencies are not available locally.

Open in Cursor Open in Web

…e files

Co-authored-by: bchen <bchen@fireworks.ai>
@cursor
Copy link
Copy Markdown

cursor bot commented Aug 30, 2025

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@benjibc benjibc marked this pull request as ready for review August 30, 2025 12:39
@benjibc benjibc requested a review from Copilot August 30, 2025 12:39
Copy link
Copy Markdown

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

This PR addresses and resolves several Pyright type errors within the eval_protocol package to improve type accuracy and handle optional values and external imports. The changes focus on removing problematic import dependencies, updating return type signatures, and improving type handling for optional values.

  • Removed dependency on openai.types.CompletionUsage and updated method signatures to use native Python types
  • Improved type safety by handling optional values and adding proper type coercion
  • Added type ignore comments for external dependencies that may not be available during development

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
eval_protocol/playback_policy.py Removed OpenAI dependency, updated return signature to use Optional types, removed duplicate method
eval_protocol/utils/static_policy.py Updated method signatures to align with base class changes and handle optional parameters
eval_protocol/rewards/accuracy_length.py Added robust string coercion for response content and type casting for accuracy reward
eval_protocol/server.py Added type ignore comments for external imports and safe casting for legacy tuple returns

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


# Import the base policy and types for proper recording functionality
from openai.types import CompletionUsage
from typing import Optional as _Optional
Copy link

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

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

This import is unused and creates an unnecessary alias. The regular Optional import on line 19 is sufficient for the usage in this file.

Suggested change
from typing import Optional as _Optional

Copilot uses AI. Check for mistakes.
elif isinstance(response.content, list) and response.content:
# Join text parts if provided as structured content
try:
text = " ".join(part.text for part in response.content) # type: ignore[union-attr]
Copy link

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

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

The type ignore comment suggests uncertainty about the structure. Consider adding a more specific type check or explicit attribute validation before accessing .text to make the code more robust.

Suggested change
text = " ".join(part.text for part in response.content) # type: ignore[union-attr]
text = " ".join(
getattr(part, "text", "") for part in response.content if hasattr(part, "text")
)

Copilot uses AI. Check for mistakes.
ground_truth=ground_truth, # Pass the ground_truth list
# Ensure ground_truth is a list if provided; default to [] for compatibility
gt_for_accuracy = ground_truth if ground_truth is not None else []
accuracy_eval_result = cast(Any, accuracy_reward)(
Copy link

Copilot AI Aug 30, 2025

Choose a reason for hiding this comment

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

Casting to Any defeats the purpose of type checking. Consider using a more specific type or ensuring the decorator preserves the original function signature instead of using cast(Any, ...).

Suggested change
accuracy_eval_result = cast(Any, accuracy_reward)(
accuracy_eval_result = accuracy_reward(

Copilot uses AI. Check for mistakes.
@benjibc benjibc merged commit 1054bf6 into main Aug 30, 2025
7 checks passed
@benjibc benjibc deleted the cursor/fix-pyright-type-errors-aab5 branch August 30, 2025 12:45
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