Skip to content

Fix BaseType annotation parsing #8318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged

Conversation

okhat
Copy link
Collaborator

@okhat okhat commented Jun 3, 2025

Summary

  • handle weird typing objects in BaseType.extract_custom_type_from_annotation

Testing

  • ruff format dspy/adapters/types/base_type.py
  • ruff check dspy/adapters/types/base_type.py
  • pytest -q (fails: ModuleNotFoundError: No module named 'datamodel_code_generator')

https://chatgpt.com/codex/tasks/task_e_683f2b47c75c8329b9ec5a7f441c7e2a

@okhat okhat added the codex label Jun 3, 2025 — with ChatGPT Connector
@okhat okhat merged commit cc78b46 into main Jun 3, 2025
3 checks passed
@okhat okhat requested a review from Copilot June 3, 2025 17:26
Copy link
Contributor

@Copilot 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 adds defensive handling for non-class typing constructs in BaseType.extract_custom_type_from_annotation to avoid TypeError from issubclass.

  • Imports inspect and uses inspect.isclass instead of isinstance
  • Wraps issubclass in a try/except TypeError to return early on invalid types
Comments suppressed due to low confidence (1)

dspy/adapters/types/base_type.py:56

  • Add a unit test case for an annotation that triggers this TypeError path (e.g., typing.Any or a TypeAlias) to verify the guard behaves as expected.
return []

# `issubclass` can raise `TypeError` if the argument is not actually
# a class (even if `inspect.isclass` thought otherwise). In these
# cases we just ignore the annotation.
return []
Copy link
Preview

Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

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

Returning [] here will skip nested annotation parsing entirely when a TypeError is raised. Consider using pass instead so that downstream logic (e.g., handling get_origin and its args) can still detect custom types in generic constructs.

Suggested change
return []
pass

Copilot uses AI. Check for mistakes.

# Direct match
if isinstance(annotation, type) and issubclass(annotation, cls):
return [annotation]
# Direct match. Some typing constructs (like `typing.Any`, `TypeAlias`,
Copy link
Preview

Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

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

[nitpick] Update the method docstring to mention this new defensive guard against invalid class-like annotations for clarity and maintainability.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant