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

Nested models with pydantic 2.0 #36

Open
szabi opened this issue Jul 14, 2023 · 1 comment
Open

Nested models with pydantic 2.0 #36

szabi opened this issue Jul 14, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@szabi
Copy link
Contributor

szabi commented Jul 14, 2023

Describe the bug:

With pydantic 2.0 (using the patched version from #35), I get an error on nested models.

class DisplaySettings(BaseModel):
    multiline: bool
    sort_on: str  # Literal['best_value', 'most_hits']


class InputSettings(BaseModel):
    autocomplete: bool


class OtherSettings(BaseModel):
    ids: list[NonNegativeInt]
    other_ids: list[NonNegativeInt]


class Settings(BaseModel):
    display: DisplaySettings
    input: InputSettings
    ltner: OtherSettings

class User(BaseModel):
    password: SecretStr
    settings: Settings

value = pydantic_form(model=User)

only renders the text_input (for password) visually, and consequently, when pressing "Submit" in the form, will rightly refuse to create a corresponding instance, as there are missing required arguments (settings).

When debugging, the problem happens in ui_rendere.py:117, when getting the _schema_references: I get a warning:

[WARNING] <streamlit.runtime.scriptrunner.script_run_context> Thread 'Thread-73': missing ScriptRunContext

and self._schema_references is set to {}.

This {} is also the reason for the missing input behaviour, as the references cannot be resolved.

See note below for a partial fix!

Expected behaviour:

Nested models working

Steps to reproduce the issue:

See snippet above.

Technical details:

  • Host Machine OS (Windows/Linux/Mac): Windows
  • Browser (Chrome/Firefox/Safari): Firefox
  • Streamlit: 1.24.1
  • Pydantic: 2.0.3

Possible Fix:

It seems that the internal schema of the model classes have changed with 2.0

In ui_renderer.py, line 118 and 1171 in the .get changing "definitions" to "$defs" seems to work in the first order:
Next to password also the settings defined in DisplaySettings and InputSettings are rendered.
BUT there is still a problem with the list fields nested in OtherSettings which needs further investigation.

I'm sorry that the PR #35 was not enough to make the lib fully pydantic 2.0 compliant.

As I said, s/"definitions"/"$defs"/ solves some of the problems, but still not all.

@szabi szabi added the bug Something isn't working label Jul 14, 2023
@szabi
Copy link
Contributor Author

szabi commented Jul 14, 2023

Ok, so the issue wrt. nested models not working with pydatnic 2 is resolved by the "suggested fixes".

However, the "list" not working seems to be an orthogonal problem, not related to pydantic 2 (but maybe changes in streamlit, compared to earlier versions):

List-Editing is solved in streamlit-pydantic among others by adding a button: self._render_list_add_button(key, add_col, data_list) (ui_renderer.py:1000), but that tries to add a streamlit.button(...) to the interface. However, adding a regular button within a form is disallowed by (at least current) streamlit button.py:407-411 raising an exception (which is then silently caught and ignored).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant