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

Support tuples in forms #52

Merged
merged 10 commits into from
Dec 21, 2023
Merged

Conversation

WolfDWyc
Copy link
Contributor

@WolfDWyc WolfDWyc commented Dec 2, 2023

fixes #24

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but you need to fix submission - at the moment data is being converted in unflatten into something like this:

    model_data: {
        'profile_pic': <starlette.datastructures.UploadFile object at 0x1084ac090>,
        'dob': '2023-12-01',
        'size': {
            'width': '123',
            'height': '321',
        },
        'address': {
            0: 'foobar',
            1: 'x',
            2: 'y',
        },

In terms of solving this, I see two routes:

  • unflatten taking the model and working out if the field is a tuple
  • assume that if all keys are completely numeric, then the dict should be converted to a list - this will probably be mucher easier

I think best to wait until we have #66 so you can add tests

@WolfDWyc
Copy link
Contributor Author

WolfDWyc commented Dec 4, 2023

I agree the second solution is simpler, but it appears I have an issue:
If the tuple contains optional values (like the one in the demo), and they aren't passed, we need to know how to fill the list with None values.

This then gets complicated, because we need to check for each value if it's optional - and if so, fill it with a None if it's not provided. Then this gets even more complicated since model fields can't be optional (SizeModel | None has no effect), so they shouldn't actually be filled.

What do you think @samuelcolvin? Do I have to go for the second option? Or can the frontend somehow return null for optional values instead of omitting them?

@samuelcolvin
Copy link
Member

Well, I guess the least bad option is to pass the model to unflatten, then use its JSON Schema to work out how many elements that tuple needs to have and pack the resultant list/tuple with None.

It might be quickest (in terms of writing code at least, if not performance) to call our existing function to flatten the JSON Schema and get a dict of {field_key: tuple_length/None}.

I hope that helps.

@samuelcolvin
Copy link
Member

Or we could just error and say it's not possible unless all fields are required as a first solution.

@WolfDWyc
Copy link
Contributor Author

WolfDWyc commented Dec 5, 2023

I think that initial solution is much better for now. I think most use cases for tuples are gonna be something such as X, Y, Z and won't require optionals.

I implemented this and updated the demo accordingly.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good, but needs tests now we have the machinery in place for tests.

python/fastui/forms.py Outdated Show resolved Hide resolved
python/fastui/forms.py Outdated Show resolved Hide resolved
python/fastui/json_schema.py Outdated Show resolved Hide resolved
python/fastui/json_schema.py Outdated Show resolved Hide resolved
@samuelcolvin samuelcolvin enabled auto-merge (squash) December 21, 2023 14:58
Copy link

codecov bot commented Dec 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4274a8d) 87.71% compared to head (3e7641c) 94.01%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #52      +/-   ##
==========================================
+ Coverage   87.71%   94.01%   +6.29%     
==========================================
  Files          11       11              
  Lines         700      718      +18     
==========================================
+ Hits          614      675      +61     
+ Misses         86       43      -43     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samuelcolvin samuelcolvin merged commit 9cf058e into pydantic:main Dec 21, 2023
16 checks passed
@WolfDWyc
Copy link
Contributor Author

Sorry I haven't been able to finish this myself and you had to step in @samuelcolvin, something popped up. Thank you for completing it!

@samuelcolvin
Copy link
Member

No problem, thanks for your work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tuples in forms
2 participants