WIP: Use pydantic for config panels and app install #1603
Closed
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.
TLDR:
Use pydantic for config panel and app install form parsing/validation.
PR is not yet ready for review, but i would like to know if it's worth continuing or not ;)
I guess it's a bit too much changes...
While working on the POC migration to FastAPI/typer I started working with pydantic (the parser/validator behind some features of FastAPI) and found it pretty cool. I thought we could give it a try for handling config panels and other dynamic forms like app install and maybe more.
bool
, it can receive a string like"yes"
and still manage to auto parse it asTrue
) which is useful for our python/bash context.So here's a WIP trying to integrate it to this complex datastructure which features a toml syntax to build custom multi-level and conditional forms that integrate inputs of many types, display/readonly components and has to be displayed as a CLI input sequence and thru API web forms.
The proposed implementation is roughly:
config.toml
declarations with pydantic models to instantiate aconfig: ConfigPanel
config
, build a dynamic pydantic model that will act assettings: Form
hydrated with currentsettings.yml
config
to iterate over what to ask/display and parse/validate user inputs thrusettings
assignements.settings
while still checking conditions fromvisible|enabled
props.settings
as an env (only strings) and pass it to bash magic helpers.settings
as yaml with "real" types, use touchedsettings
(only the ones that changed) and update what's needed.The code is split into two files:
form.py
: Custom type to handle bash list,Option
s models for every implemented display/inputs components, basic form factory, CLI displayer/prompter, API filler, evaluation functions. This can be used directly byapp_install()
for install form.configpanel.py
: which is more or less the config panel implementation built over base stuff inform.py
. (ConfigPanel
containingPanel
s containingSection
s containingOption
s) and other specific stuff. (Haven't removed the actual implementation to be able to compare to it easily)Small new thing i have in mind:
select
andtags
can receive anitem_type = "string|color|number|date|time|email|path|url"
default tostring
that will parse/validate every items in the list with the correct type, so you can ask for a list of emails and be sure all are valid emails without reinventing regex to match already defined types. (could be extended to allowfile|app|domain|user|group
)PR STATUS
Not yet ready for review i guess, need to update tests and test it moar + the todo.
This does not yet brings that much new stuff but open the door for more type safe code, i think pydantic could be used in other part of the core code and we could rely on it for most of the validation/parsing we need and use it to define our datastructures. I tried to test it on config panel because i thought it was the most complex but idk.
Also if we definitly plan to move to FastAPI, i guess it will be an easier migration.
TODO
None
for bool (not set)?OperationLogger
required
options are not very clear to me in case on config panel, does config panel required options have to be set at install?Config.get()
will yell at settings validation since it may not be defined.Config.set()
are not validated at instantiation to allow the prompt/fill a chance to define a value, but this is not tottaly satisfying since it mess up a bit with "detecting what changed" since originally the form is unparsed/unvalidated.Possible improvments
""
fromNone
and allow multiple values in CLI with a custom type:"null|nil|none|_none"
)?min_length|max_length
of constrAppManifest
pydantic model integrating the app install dynamic form.app_config_get(full=True)
could return a JSON Schema directly instead of our custom toml specs in the web admin (need to update the current web admin config panel/custom forms interpreter to this more generic JSON schema approach)settings
model when CLI prompting/ API filling instead of relying onconfig
(repr of config toml stuff) with nested stuff and extra information on each field.SettingConfigPanel
andDomainConfigPanel
as pydantic models directly and get rid of their.toml
.How to test
# install pydantic pip install pydantic
Also install python-email-validator to use pydantic EmailStr type. This is extra dep so maybe we can use a simpler custom regex validator but this lib handle international stuff (but i'm not sure we do).