From e2e1c1333c0c5fb37a8a4ffb0fa8fc67268d90a0 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Mon, 28 Oct 2024 10:20:21 +0000 Subject: [PATCH] PyCharm noinspection comments --- pydantic_ai/_pydantic.py | 4 +++- pydantic_ai/_result.py | 8 +++++--- pydantic_ai/_retriever.py | 1 + pydantic_ai/_system_prompt.py | 2 +- pydantic_ai/_utils.py | 1 + pydantic_ai/agent.py | 2 ++ pydantic_ai/models/__init__.py | 7 ++++--- pydantic_ai/models/gemini.py | 1 + 8 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pydantic_ai/_pydantic.py b/pydantic_ai/_pydantic.py index 995dfe2dc..68d8fad63 100644 --- a/pydantic_ai/_pydantic.py +++ b/pydantic_ai/_pydantic.py @@ -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: @@ -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, @@ -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) diff --git a/pydantic_ai/_result.py b/pydantic_ai/_result.py index c48771c78..a6eed34d1 100644 --- a/pydantic_ai/_result.py +++ b/pydantic_ai/_result.py @@ -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 @@ -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]] = {} @@ -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') diff --git a/pydantic_ai/_retriever.py b/pydantic_ai/_retriever.py index 5ba3faef8..c79c6a6eb 100644 --- a/pydantic_ai/_retriever.py +++ b/pydantic_ai/_retriever.py @@ -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__ diff --git a/pydantic_ai/_system_prompt.py b/pydantic_ai/_system_prompt.py index 148a7e1e6..05b48e4f2 100644 --- a/pydantic_ai/_system_prompt.py +++ b/pydantic_ai/_system_prompt.py @@ -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], diff --git a/pydantic_ai/_utils.py b/pydantic_ai/_utils.py index 736c4f3ec..e4b126923 100644 --- a/pydantic_ai/_utils.py +++ b/pydantic_ai/_utils.py @@ -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 diff --git a/pydantic_ai/agent.py b/pydantic_ai/agent.py index 9a1f43e1b..55850f7eb 100644 --- a/pydantic_ai/agent.py +++ b/pydantic_ai/agent.py @@ -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( @@ -205,6 +206,7 @@ def retriever_decorator( return retriever_decorator else: + # noinspection PyTypeChecker return self._register_retriever(_utils.Either(left=func), retries) @overload diff --git a/pydantic_ai/models/__init__.py b/pydantic_ai/models/__init__.py index 88e0b464b..81ae41033 100644 --- a/pydantic_ai/models/__init__.py +++ b/pydantic_ai/models/__init__.py @@ -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 @@ -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: @@ -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 diff --git a/pydantic_ai/models/gemini.py b/pydantic_ai/models/gemini.py index 76044bbcb..48413756e 100644 --- a/pydantic_ai/models/gemini.py +++ b/pydantic_ai/models/gemini.py @@ -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)