Skip to content

Commit

Permalink
fix: improve type hints in partial.py
Browse files Browse the repository at this point in the history
- Add proper type hints for _make_field_optional return type
- Add type casting for generic type handling
- Improve type hints for _process_generic_arg
- Add missing Type import
  • Loading branch information
devin-ai-integration[bot] committed Nov 23, 2024
1 parent 70accd1 commit 73af58b
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions instructor/dsl/partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@

from jiter import from_json
from pydantic import BaseModel, create_model
from typing import Union
import types
import sys
from pydantic.fields import FieldInfo
from typing import (
Any,
Generic,
Union,
TypeVar,
Optional,
get_args,
get_origin,
NoReturn,
Optional,
TypeVar,
Type,

Check failure on line 22 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Ruff (ubuntu-latest)

Ruff (F401)

instructor/dsl/partial.py:22:5: F401 `typing.Type` imported but unused

Check failure on line 22 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Ruff (macos-latest)

Ruff (F401)

instructor/dsl/partial.py:22:5: F401 `typing.Type` imported but unused

Check failure on line 22 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Import "Type" is not accessed (reportUnusedImport)

Check failure on line 22 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.10)

Import "Type" is not accessed (reportUnusedImport)
cast,
)

Check failure on line 24 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Ruff (ubuntu-latest)

Ruff (UP035)

instructor/dsl/partial.py:13:1: UP035 `typing.Type` is deprecated, use `type` instead

Check failure on line 24 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Ruff (macos-latest)

Ruff (UP035)

instructor/dsl/partial.py:13:1: UP035 `typing.Type` is deprecated, use `type` instead
import types
import sys
from pydantic.fields import FieldInfo
from collections.abc import AsyncGenerator, Generator, Iterable
from copy import deepcopy
from functools import cache
Expand All @@ -48,9 +50,9 @@ class PartialLiteralMixin:


def _process_generic_arg(
arg: Any,
arg: type[Any] | Any,
make_fields_optional: bool = False,
) -> Any:
) -> type[Any] | Any:
arg_origin = get_origin(arg)
if arg_origin is not None:
# Handle any nested generic type (Union, List, Dict, etc.)
Expand All @@ -64,23 +66,22 @@ def _process_generic_arg(
)
# Special handling for Union types (types.UnionType isn't subscriptable)
if arg_origin in UNION_ORIGINS:
return Union[modified_nested_args] # type: ignore
return cast(Any, Union[modified_nested_args])

Check failure on line 69 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Union requires two or more type arguments (reportInvalidTypeArguments)

Check failure on line 69 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Expected class but received "tuple[type[Any] | Any, ...]" (reportGeneralTypeIssues)

Check failure on line 69 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.10)

Union requires two or more type arguments (reportInvalidTypeArguments)

Check failure on line 69 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.10)

Expected class but received "tuple[type[Any] | Any, ...]" (reportGeneralTypeIssues)

return arg_origin[modified_nested_args]
return cast(Any, arg_origin[modified_nested_args])

Check failure on line 71 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Unnecessary "cast" call; type is already "Any" (reportUnnecessaryCast)

Check failure on line 71 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.10)

Unnecessary "cast" call; type is already "Any" (reportUnnecessaryCast)
else:
if isinstance(arg, type) and issubclass(arg, BaseModel):
return (
Partial[arg, MakeFieldsOptional] # type: ignore[valid-type]
if make_fields_optional
else Partial[arg]
return cast(
Any,
Partial[arg, MakeFieldsOptional] if make_fields_optional else Partial[arg]

Check failure on line 76 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Too many type arguments provided for "Partial"; expected 1 but received 2 (reportInvalidTypeArguments)

Check failure on line 76 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.10)

Too many type arguments provided for "Partial"; expected 1 but received 2 (reportInvalidTypeArguments)
)
else:
return arg


def _make_field_optional(
field: FieldInfo,
) -> tuple[Any, FieldInfo]:
) -> tuple[type[Any] | type[None] | type[Partial[BaseModel]], FieldInfo]:
tmp_field = deepcopy(field)

annotation = field.annotation
Expand All @@ -96,17 +97,15 @@ def _make_field_optional(
)

# Reconstruct the generic type with modified arguments
tmp_field.annotation = (
Optional[generic_base[modified_args]] if generic_base else None
)
tmp_field.annotation = Optional[generic_base[modified_args]] # type: ignore[valid-type]
tmp_field.default = None
# If the field is a BaseModel, then recursively convert it's
# attributes to optionals.
elif isinstance(annotation, type) and issubclass(annotation, BaseModel):
tmp_field.annotation = Optional[Partial[annotation, MakeFieldsOptional]] # type: ignore[assignment, valid-type]
tmp_field.annotation = Optional[Partial[annotation, MakeFieldsOptional]] # type: ignore[valid-type]
tmp_field.default = {}
else:
tmp_field.annotation = Optional[field.annotation]
tmp_field.annotation = Optional[annotation] # type: ignore[valid-type]
tmp_field.default = None

return tmp_field.annotation, tmp_field

Check failure on line 111 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Type of "annotation" is partially unknown   Type of "annotation" is "Unknown | type[None] | type[Partial[BaseModel]]" (reportUnknownMemberType)

Check failure on line 111 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Return type, "tuple[Unknown | type[None] | type[Partial[BaseModel]], FieldInfo]", is partially unknown (reportUnknownVariableType)

Check failure on line 111 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.10)

Type of "annotation" is partially unknown   Type of "annotation" is "Unknown | type[None] | type[Partial[BaseModel]]" (reportUnknownMemberType)

Check failure on line 111 in instructor/dsl/partial.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.10)

Return type, "tuple[Unknown | type[None] | type[Partial[BaseModel]], FieldInfo]", is partially unknown (reportUnknownVariableType)
Expand Down

0 comments on commit 73af58b

Please sign in to comment.