Skip to content
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

New JSON validation mode for callables #11240

Open
3 of 13 tasks
Viicos opened this issue Jan 8, 2025 · 1 comment · May be fixed by pydantic/pydantic-core#1641
Open
3 of 13 tasks

New JSON validation mode for callables #11240

Viicos opened this issue Jan 8, 2025 · 1 comment · May be fixed by pydantic/pydantic-core#1641
Assignees
Milestone

Comments

@Viicos
Copy link
Member

Viicos commented Jan 8, 2025

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

When using a TypeAdapter on a callable (or using the @validate_call()) decorator, validation from JSON is limited:

from pydantic import TypeAdapter
from pydantic_core import ArgsKwargs

def f(a: int, /, *, b: int):
    ...

ta = TypeAdapter(f)

# From Python, you use `ArgsKwargs`:
ta.validate_python(ArgsKwargs((1,), {'b': 1})

# No way to validate from JSON
ta.validate_json('{"a": 1, "b": 2}')  # error

The idea would be to introduce a flag on the arguments_schema(), so that we can validate like this:

def f(a: int, /, *, b: int):
    ...

...
ta.validate_json('{"a": 1, "b": 2}')

def f(*args: int, **kwargs: str):
    ...

...
ta.validate_json('{"args": [1, 2, 3], "kwargs": {"a": "string"}}')

This should work as parameter names should be unique. Note that while something like this could also be done for kwargs:

def f(*args: int, **kwargs: str):
    ...

...
ta.validate_json('{"args": [1, 2, 3], "a": "string"')

This won't play well if you have a parameter named a.

Affected Components

@samuelcolvin
Copy link
Member

@Viicos looks good, that covers my explanation.

The rationale for this is to use arguments_schema in pydantic_ai, instead of my current hack.

Hence (unless you find a bug in my code), the new logic should match this.

@Viicos Viicos added this to the v2.11 milestone Feb 13, 2025
@Viicos Viicos linked a pull request Feb 20, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants