-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Use get_type_hints() in function_schema()
#3693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Viicos
wants to merge
1
commit into
main
Choose a base branch
from
vp/fix-function-schema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| from collections.abc import Awaitable, Callable | ||
| from dataclasses import dataclass, field | ||
| from functools import partial | ||
| from inspect import Parameter, signature | ||
| from typing import TYPE_CHECKING, Any, Concatenate, cast, get_origin | ||
|
|
||
|
|
@@ -17,7 +18,7 @@ | |
| from pydantic.json_schema import GenerateJsonSchema | ||
| from pydantic.plugin._schema_validator import create_schema_validator | ||
| from pydantic_core import SchemaValidator, core_schema | ||
| from typing_extensions import ParamSpec, TypeIs, TypeVar | ||
| from typing_extensions import ParamSpec, TypeIs, TypeVar, get_type_hints | ||
|
|
||
| from ._griffe import doc_descriptions | ||
| from ._run_context import RunContext | ||
|
|
@@ -90,9 +91,6 @@ def function_schema( # noqa: C901 | |
| Returns: | ||
| A `FunctionSchema` instance. | ||
| """ | ||
| if takes_ctx is None: | ||
| takes_ctx = _takes_ctx(function) | ||
|
|
||
| config = ConfigDict(title=function.__name__, use_attribute_docstrings=True) | ||
| config_wrapper = ConfigWrapper(config) | ||
| gen_schema = _generate_schema.GenerateSchema(config_wrapper) | ||
|
|
@@ -103,30 +101,24 @@ def function_schema( # noqa: C901 | |
| except ValueError as e: | ||
| errors.append(str(e)) | ||
| sig = signature(lambda: None) | ||
| original_func = function.func if isinstance(function, partial) else function | ||
| function = cast(Callable[..., Any], function) # cope with pyright changing the type from the isinstance() check. | ||
|
|
||
| type_hints = _typing_extra.get_function_type_hints(function) | ||
| type_hints = get_type_hints(original_func) | ||
|
|
||
| var_kwargs_schema: core_schema.CoreSchema | None = None | ||
| fields: dict[str, core_schema.TypedDictField] = {} | ||
| positional_fields: list[str] = [] | ||
| var_positional_field: str | None = None | ||
| decorators = _decorators.DecoratorInfos() | ||
|
|
||
| description, field_descriptions = doc_descriptions(function, sig, docstring_format=docstring_format) | ||
|
|
||
| if require_parameter_descriptions: | ||
| if takes_ctx: | ||
| parameters_without_ctx = set( | ||
| name for name in sig.parameters if not _is_call_ctx(sig.parameters[name].annotation) | ||
| ) | ||
| missing_params = parameters_without_ctx - set(field_descriptions) | ||
| else: | ||
| missing_params = set(sig.parameters) - set(field_descriptions) | ||
|
|
||
| if missing_params: | ||
| errors.append(f'Missing parameter descriptions for {", ".join(missing_params)}') | ||
| description, field_descriptions = doc_descriptions(original_func, sig, docstring_format=docstring_format) | ||
| missing_param_descriptions: set[str] = set() | ||
|
|
||
| for index, (name, p) in enumerate(sig.parameters.items()): | ||
| if index == 0 and takes_ctx is None: | ||
| takes_ctx = p.annotation is not sig.empty and _is_call_ctx(type_hints[name]) | ||
|
|
||
| if p.annotation is sig.empty: | ||
| if takes_ctx and index == 0: | ||
| # should be the `context` argument, skip | ||
|
|
@@ -148,6 +140,10 @@ def function_schema( # noqa: C901 | |
| continue | ||
|
|
||
| field_name = p.name | ||
|
|
||
| if require_parameter_descriptions and field_name not in field_descriptions: | ||
| missing_param_descriptions.add(field_name) | ||
|
|
||
| if p.kind == Parameter.VAR_KEYWORD: | ||
| var_kwargs_schema = gen_schema.generate_schema(annotation) | ||
| else: | ||
|
|
@@ -178,6 +174,9 @@ def function_schema( # noqa: C901 | |
| elif p.kind == Parameter.VAR_POSITIONAL: | ||
| var_positional_field = field_name | ||
|
|
||
| if missing_param_descriptions: | ||
| errors.append(f'Missing parameter descriptions for {", ".join(missing_param_descriptions)}') | ||
|
|
||
| if errors: | ||
| from .exceptions import UserError | ||
|
|
||
|
|
@@ -219,7 +218,7 @@ def function_schema( # noqa: C901 | |
| single_arg_name=single_arg_name, | ||
| positional_fields=positional_fields, | ||
| var_positional_field=var_positional_field, | ||
| takes_ctx=takes_ctx, | ||
| takes_ctx=bool(takes_ctx), | ||
| is_async=is_async_callable(function), | ||
| function=function, | ||
| ) | ||
|
|
@@ -234,7 +233,7 @@ def function_schema( # noqa: C901 | |
| TargetCallable = WithCtx[P, R] | WithoutCtx[P, R] | ||
|
|
||
|
|
||
| def _takes_ctx(callable_obj: TargetCallable[P, R]) -> TypeIs[WithCtx[P, R]]: | ||
| def _takes_ctx(callable_obj: TargetCallable[P, R]) -> TypeIs[WithCtx[P, R]]: # pyright: ignore[reportUnusedFunction] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still used in one other place in Pydantic AI, will see how this can be cleaned up in a future PR. |
||
| """Check if a callable takes a `RunContext` first argument. | ||
|
|
||
| Args: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using
_takes_ctx(), do the logic here. This avoids computing the type hints twice.