Skip to content

Commit 72300ae

Browse files
authored
Suppress griffe logging related to return type (#606)
1 parent faff8a9 commit 72300ae

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

pydantic_ai_slim/pydantic_ai/_griffe.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations as _annotations
22

3+
import logging
34
import re
5+
from contextlib import contextmanager
46
from inspect import Signature
57
from typing import Any, Callable, Literal, cast
68

@@ -25,7 +27,8 @@ def doc_descriptions(
2527
parent = cast(GriffeObject, sig)
2628

2729
docstring = Docstring(doc, lineno=1, parser=style or _infer_docstring_style(doc), parent=parent)
28-
sections = docstring.parse()
30+
with _disable_griffe_logging():
31+
sections = docstring.parse()
2932

3033
params = {}
3134
if parameters := next((p for p in sections if p.kind == DocstringSectionKind.parameters), None):
@@ -125,3 +128,12 @@ def _infer_docstring_style(doc: str) -> DocstringStyle:
125128
'numpy',
126129
),
127130
]
131+
132+
133+
@contextmanager
134+
def _disable_griffe_logging():
135+
# Hacky, but suggested here: https://github.com/mkdocstrings/griffe/issues/293#issuecomment-2167668117
136+
old_level = logging.root.getEffectiveLevel()
137+
logging.root.setLevel(logging.ERROR)
138+
yield
139+
logging.root.setLevel(old_level)

tests/test_tools.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pydantic_core
66
import pytest
7+
from _pytest.logging import LogCaptureFixture
78
from inline_snapshot import snapshot
89
from pydantic import BaseModel, Field
910
from pydantic_core import PydanticSerializationError
@@ -532,3 +533,36 @@ def ctx_tool(ctx: RunContext[int], x: int) -> int:
532533
""")
533534
result = mod.agent.run_sync('foobar', deps=5)
534535
assert result.data == snapshot('{"ctx_tool":5}')
536+
537+
538+
async def tool_without_return_annotation_in_docstring() -> str: # pragma: no cover
539+
"""A tool that documents what it returns but doesn't have a return annotation in the docstring.
540+
541+
Returns:
542+
A value.
543+
"""
544+
545+
return ''
546+
547+
548+
def test_suppress_griffe_logging(set_event_loop: None, caplog: LogCaptureFixture):
549+
# This would cause griffe to emit a warning log if we didn't suppress the griffe logging.
550+
551+
agent = Agent(FunctionModel(get_json_schema))
552+
agent.tool_plain(tool_without_return_annotation_in_docstring)
553+
554+
result = agent.run_sync('')
555+
json_schema = json.loads(result.data)
556+
assert json_schema == snapshot(
557+
{
558+
'description': "A tool that documents what it returns but doesn't have a "
559+
'return annotation in the docstring.',
560+
'name': 'tool_without_return_annotation_in_docstring',
561+
'outer_typed_dict_key': None,
562+
'parameters_json_schema': {'additionalProperties': False, 'properties': {}, 'type': 'object'},
563+
}
564+
)
565+
566+
# Without suppressing griffe logging, we get:
567+
# assert caplog.messages == snapshot(['<module>:4: No type or annotation for returned value 1'])
568+
assert caplog.messages == snapshot([])

0 commit comments

Comments
 (0)