-
Notifications
You must be signed in to change notification settings - Fork 7
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
Drop TaskArguments
models, in favor of validate_arguments
#377
Comments
TaskArguments
models?TaskArguments
models?
Let's discuss this. Are we talking about moving argument defaults from the python function to the pydantic models? In that case, the Python functions would loose a lot of value in respect to being usable standalone and I wouldn't be a fan. How do other libraries using pydantic handle that? |
TL;DR Further review of this issue showed us a slightly different, and promising, way forward (all details below, as part of a verbose log); we are not trying to move from a basic example to something that can actually work. If it does work, then:
There's more than "just" the defaults (although I agree that removing the defaults from the function signature would be the most relevant change). The information that we can encode for each argument includes:
I'm not aware of someone using it the same way as we (plan to) do. This is partly because enforcing type hints at runtime is not a common practice in Python, and partly because we also want to use Pydantic for additional purposes (i.e. generating JSON Schemas). For the moment:
This two-fold purpose leads to the redundancy issue: where is each information declared? In the function signature and/or in the Pydantic model? If we stick with point 1, then in principle we could move to an even better solution, where we simply never declare a Pydantic model. This would be via the
This would immediately close this issue: all information would strictly belong to the function call signature, since there wouldn't even exist a Pydantic model. What is missing, however, is that we still need to generate the JSON Schema, and we'd still need a Pydantic model for that purpose. In fact, I just realized that this model could still be extracted from the Pydantic from devtools import debug
from typing import Optional
from pydantic import validate_arguments
def fun(
x: int,
y: Optional[str],
z: dict = {"a": "b"},
):
pass
debug(validate_arguments(fun).model.schema()) which outputs something similar to what we need {
"title": "Fun",
"type": "object",
"properties": {
"x": {
"title": "X",
"type": "integer"
},
"v__duplicate_kwargs": {
"title": "V Duplicate Kwargs",
"type": "array",
"items": {
"type": "string"
}
},
"y": {
"title": "Y",
"type": "string"
},
"z": {
"title": "Z",
"default": {
"a": "b"
},
"type": "object"
},
"args": {
"title": "Args",
"type": "array",
"items": {}
},
"kwargs": {
"title": "Kwargs",
"type": "object"
}
},
"required": [
"x",
"y"
],
"additionalProperties": false
} Note that apart from understanding and then removing some spurious properties ( A comment about Pydantic versionThe example above is with Pydantic v1, which is in bug-fixes-only state, so that it will keep working. Pydantic v2 will refactor this feature, but without removing it. Thus I'd say that it's something we can safely rely upon. |
* Use PydanticV1 `validate_arguments` instead of creating `TaskArguments` models by hand. * Update tests. * Update `args_schema` script.
As of 87a38b5, we have a first implementation of this new approach - which seems to work. What is left (apart from further tests) is
|
TaskArguments
models?TaskArguments
models, in favor of validate_arguments
Re-opening, since this is not yet complete with #369 (see to-do list in previous comment) |
(closing since what was left is not part of #386) |
This issue is not yet well defined, but we will have to do something in this direction.
The text was updated successfully, but these errors were encountered: