-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Allow validator and serializer functions to have default values #9478
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
Conversation
CodSpeed Performance ReportMerging #9478 will not alter performanceComparing Summary
|
95d40b1 to
41696d9
Compare
| return sum(1 for param in sig.parameters.values() if can_be_positional(param)) | ||
| """Get the number of positional (required) arguments of a signature. | ||
|
|
||
| This function should only be used to inspect signatures of validation and serialization functions. |
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.
I'm not too happy with how this function grew, hence this "warning" in the docstring. At some point, maybe this inspection of validators/serializers signature could be refactored, e.g.:
@dataclass
class FunctionInfo:
func: Callable[..., Any]
@property
def requires_info_arg(self) -> bool: ...
...8bec84c to
607c139
Compare
|
Please review |
|
Could you provide some tests with concrete examples of validators / serializers with default values, so that we have some more documented context? Thanks! |
|
Thanks for adding those extra tests. I suppose I still have a bit of a gap in understanding in why we'd want to support these additional args given that you don't call validators or serializers directly. Could you explain a bit more re the motivation for this change / addition? Thanks! |
While it is really uncommon to use such a function as a validator (or serializer): pydantic/tests/test_validators.py Lines 2889 to 2900 in f7f7abd
It is more common to do: class Model(BaseModel):
x: str
val_x = field_validator('x', mode='before')(str) Or to use Pydantic uses As we can see, def my_validator(value: Any, info: ValidationInfo | None = None): ...If you need the |
pydantic/_internal/_decorators.py
Outdated
| @@ -774,7 +778,25 @@ def get_function_return_type( | |||
|
|
|||
|
|
|||
| def count_positional_params(sig: Signature) -> int: | |||
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.
It seems like the behaviour of this function changed to have the required arguments property, should we rename it to avoid accidents in case anyone is trying to borrow our internals?
|
Overall this seems very reasonable to me, with just a desire to rename the helper function to help avoid confusion. |
|
Function renamed |
|
Once we get tests passing, we should be all good to merge! |
|
Maybe a pull from |
Such functions now require the info argument to be required (i.e. no default value specified in the signature). Unify handling of exceptions when trying to get the function's signature. Refactor tests
ce87dd5 to
109596d
Compare
|
Ah with a closer look, looks like the test failure is related to these changes. |
|
Yep, fixed the recently added test |
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.
Awesome, thanks @Viicos!
Fixes #9463
Selected Reviewer: @sydney-runkle