Skip to content

Commit

Permalink
Iterable Workaround (#737)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Liu <jxnl@users.noreply.github.com>
  • Loading branch information
ivanleomk and jxnl committed Jun 11, 2024
1 parent 6914092 commit b53afab
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4,922 deletions.
15 changes: 14 additions & 1 deletion instructor/dsl/simple_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,27 @@ def __class_getitem__(cls, response_model: type[BaseModel]) -> type[BaseModel]:
)


def validateIsSubClass(response_model: type):
"""
Temporary guard against issues with generics in Python 3.9
"""
import sys

if sys.version_info < (3, 10):
if len(typing.get_args(response_model)) == 0:
return False
return issubclass(typing.get_args(response_model)[0], BaseModel)
return issubclass(response_model, BaseModel)


def is_simple_type(
response_model: type[BaseModel] | str | int | float | bool | typing.Any,
) -> bool:
# ! we're getting mixes between classes and instances due to how we handle some
# ! response model types, we should fix this in later PRs

try:
if isclass(response_model) and issubclass(response_model, BaseModel):
if isclass(response_model) and validateIsSubClass(response_model):
return False
except TypeError:
# ! In versions < 3.11, typing.Iterable is not a class, so we can't use isclass
Expand Down
Loading

0 comments on commit b53afab

Please sign in to comment.