Skip to content

Commit

Permalink
PyCharm noinspection comments
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Oct 28, 2024
1 parent 0042877 commit e2e1c13
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion pydantic_ai/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def function_schema(either_function: _retriever.RetrieverEitherFunc[AgentDeps, _
field_info,
decorators,
)
# noinspection PyTypeChecker
td_schema.setdefault('metadata', {})['is_model_like'] = is_model_like(annotation)

if p.kind == Parameter.POSITIONAL_ONLY:
Expand All @@ -130,6 +131,7 @@ def function_schema(either_function: _retriever.RetrieverEitherFunc[AgentDeps, _

schema, single_arg_name = _build_schema(fields, var_kwargs_schema, gen_schema, core_config)
schema = gen_schema.clean_schema(schema)
# noinspection PyUnresolvedReferences
schema_validator = create_schema_validator(
schema,
function,
Expand All @@ -139,7 +141,7 @@ def function_schema(either_function: _retriever.RetrieverEitherFunc[AgentDeps, _
core_config,
config_wrapper.plugin_settings,
)
# PluggableSchemaValidator is api compat with SchemaValidator
# PluggableSchemaValidator is api compatible with SchemaValidator
schema_validator = cast(SchemaValidator, schema_validator)
json_schema = GenerateJsonSchema().generate(schema)

Expand Down
8 changes: 5 additions & 3 deletions pydantic_ai/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def validate(


class ToolRetryError(Exception):
"""Internal exception used to indicate a signal a `ToolRetry` message should be returned to the LLM."""
"""Internal exception used to signal a `ToolRetry` message should be returned to the LLM."""

def __init__(self, tool_retry: messages.RetryPrompt):
self.tool_retry = tool_retry
Expand Down Expand Up @@ -99,10 +99,10 @@ def build(cls, response_type: type[ResultData], name: str, description: str | No
else:
allow_text_result = False

def _build_tool(a: Any, tool_name: str, multiple: bool) -> ResultTool[ResultData]:
def _build_tool(a: Any, tool_name_: str, multiple: bool) -> ResultTool[ResultData]:
return cast(
ResultTool[ResultData],
ResultTool.build(a, tool_name, description, multiple), # pyright: ignore[reportUnknownMemberType]
ResultTool.build(a, tool_name_, description, multiple), # pyright: ignore[reportUnknownMemberType]
)

tools: dict[str, ResultTool[ResultData]] = {}
Expand Down Expand Up @@ -141,11 +141,13 @@ def build(cls, response_type: type[ResultData], name: str, description: str | No
if _utils.is_model_like(response_type):
type_adapter = TypeAdapter(response_type)
outer_typed_dict_key: str | None = None
# noinspection PyArgumentList
json_schema = _utils.check_object_json_schema(type_adapter.json_schema())
else:
response_data_typed_dict = TypedDict('response_data_typed_dict', {'response': response_type}) # noqa
type_adapter = TypeAdapter(response_data_typed_dict)
outer_typed_dict_key = 'response'
# noinspection PyArgumentList
json_schema = _utils.check_object_json_schema(type_adapter.json_schema())
# including `response_data_typed_dict` as a title here doesn't add anything and could confuse the LLM
json_schema.pop('title')
Expand Down
1 change: 1 addition & 0 deletions pydantic_ai/_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Retriever(Generic[AgentDeps, P]):
def __init__(self, function: RetrieverEitherFunc[AgentDeps, P], retries: int):
"""Build a Retriever dataclass from a function."""
self.function = function
# noinspection PyTypeChecker
f = _pydantic.function_schema(function)
raw_function = function.whichever()
self.name = raw_function.__name__
Expand Down
2 changes: 1 addition & 1 deletion pydantic_ai/_system_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from . import _utils
from .shared import AgentDeps, CallContext

# A function that may or maybe not take `CallInfo` as an argument, and may or may not be async.
# A function that may or maybe not take `CallContext` as an argument, and may or may not be async.
# Usage `SystemPromptFunc[AgentDeps]`
SystemPromptFunc = Union[
Callable[[CallContext[AgentDeps]], str],
Expand Down
1 change: 1 addition & 0 deletions pydantic_ai/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

async def run_in_executor(func: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
if kwargs:
# noinspection PyTypeChecker
return await asyncio.get_running_loop().run_in_executor(None, partial(func, *args, **kwargs))
else:
return await asyncio.get_running_loop().run_in_executor(None, func, *args) # type: ignore
Expand Down
2 changes: 2 additions & 0 deletions pydantic_ai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ async def run(
messages.extend(tool_responses)
except (ValidationError, shared.UnexpectedModelBehaviour) as e:
run_span.set_attribute('messages', messages)
# noinspection PyTypeChecker
raise shared.AgentError(messages, model_) from e

def run_sync(
Expand Down Expand Up @@ -205,6 +206,7 @@ def retriever_decorator(

return retriever_decorator
else:
# noinspection PyTypeChecker
return self._register_retriever(_utils.Either(left=func), retries)

@overload
Expand Down
7 changes: 4 additions & 3 deletions pydantic_ai/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Logic related to making requests to an LLM.
The aim here is to make a common interface
The aim here is to make a common interface for different LLMs, so that the rest of the code can be agnostic to the
specific LLM being used.
"""

from __future__ import annotations as _annotations
Expand Down Expand Up @@ -52,11 +53,10 @@ class AgentModel(ABC):

@abstractmethod
async def request(self, messages: list[Message]) -> tuple[LLMMessage, Cost]:
"""Request a response from the model."""
"""Make a request to the model."""
raise NotImplementedError()

# TODO streamed response
# TODO support for non JSON tool calls


def infer_model(model: Model | KnownModelName) -> Model:
Expand All @@ -70,6 +70,7 @@ def infer_model(model: Model | KnownModelName) -> Model:
elif model.startswith('gemini'):
from .gemini import GeminiModel

# noinspection PyTypeChecker
return GeminiModel(model) # pyright: ignore[reportArgumentType]
else:
from ..shared import UserError
Expand Down
1 change: 1 addition & 0 deletions pydantic_ai/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def _simplify(self, schema: dict[str, Any], allow_ref: bool) -> None:
if ref := schema.pop('$ref', None):
if not allow_ref:
raise shared.UserError('Recursive `$ref`s in JSON Schema are not supported by Gemini')
# noinspection PyTypeChecker
key = re.sub(r'^#/\$defs/', '', ref)
schema_def = self.defs[key]
self._simplify(schema_def, allow_ref=False)
Expand Down

0 comments on commit e2e1c13

Please sign in to comment.