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
v2. Accessing a function's 'fields' and 'values' when using validate_call
(possible bug)
#6345
Comments
@lig, any chance you could have a quick look at this and advise if there's a bug here or whether it's no longer possible to access a validated function's 'fields' and 'values' in v2? Thank you. |
@maread99 The following code works import pydantic
from pydantic_core import core_schema
from typing import Any
class ACustomType:
@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: pydantic.GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.general_after_validator_function(
cls._validate,
core_schema.any_schema(),
)
@classmethod
def _validate(cls, v, info: core_schema.ValidationInfo):
print(info)
if v == 1:
msg = f"`{cls.__qualname__}` cannot be 1"
raise ValueError(msg)
return v
class ACls:
def get_a_value():
return 3
@pydantic.validate_call
def add_one(self, a_validated_value: ACustomType):
return a_validated_value + 1
inst = ACls()
inst.add_one(1) However, I think that it would be better to define a model and a field of your custom type. It's not clear to me at the moment what you're trying to achieve. |
@lig is right, you can't access You shouldn't need to include the field name in the error message - it's included anyway when the In you're case you'll get a |
Thank you for the replies.
@lig, all I'd like to know is how to use from pydantic import BeforeValidator, validate_call
from typing import Annotated
def validator_a(a) -> str:
# validates something
return a
def validator_b(b) -> str:
# validates something according to the value received to func's 'a' parameter
# i.e, how can I access in this scope the object returned by `validator_a`
return b
@validate_call
def func(
a: Annotated[str, BeforeValidator(validator_a)],
b: Annotated[str, BeforeValidator(validator_b)],
):
return a + b This example uses the Annotated approach while the earlier one explored custom types. I'm not so concerned which approach to use as simply being able to do this in some way. Possibly related, this part of the docs notes that: These names (together with "args" and "kwargs") may or may not (depending on the function's signature) appear as fields on the internal Pydantic model accessible via .model ...but from where might .model be accessible? Thanks again. |
Hi. v2 migration issue. I use pydantic to validate function inputs and I've hit a bit of wall trying to access the 'fields' and 'values' of the function being validated.
In v1 this can be done...
such that:
Having gone through the docs, this is how I would have thought the same could be done in v2:
However...
Is it still possible to access the function's 'fields' and 'values' in v2? If so, does this error have its roots in a bug or am I just not doing it right?
(the actual custom types I'm struggling with are here)
Thanks for the excellent library!
sys info from
python -c "import pydantic.version; print(pydantic.version.version_info())"
pydantic version: 2.0
pydantic-core version: 2.0.1 release build profile
install path: \venv\Lib\site-packages\pydantic
python version: 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
platform: Windows-10-10.0.22621-SP0
optional deps. installed: ['typing-extensions']
Selected Assignee: @lig
The text was updated successfully, but these errors were encountered: