Create distinct types for Settings / SettingsPatch#465
Merged
Conversation
Codecov Report
@@ Coverage Diff @@
## main #465 +/- ##
==========================================
- Coverage 80.50% 80.48% -0.02%
==========================================
Files 187 188 +1
Lines 11006 11087 +81
Branches 1074 1076 +2
==========================================
+ Hits 8860 8923 +63
- Misses 2132 2150 +18
Partials 14 14
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
danvk
commented
Aug 31, 2023
| return filepath | ||
|
|
||
|
|
||
| def default_settings() -> FlatSettingsSchema: |
Collaborator
Author
There was a problem hiding this comment.
This is a way to define default settings without adding default values to the type.
| openai_temperature: float | ||
|
|
||
|
|
||
| @make_optional() |
Collaborator
Author
There was a problem hiding this comment.
This is how you make an optional model with the decorator
| status: ResponseStatus; | ||
| }; | ||
| /** FlatSettingsSchema */ | ||
| FlatSettingsSchema: { |
Collaborator
Author
There was a problem hiding this comment.
The new types are much nicer -- all fields required!
| } | ||
|
|
||
| // TODO: remove this after we separate the request/response types in the settings API | ||
| type DeepRequired<T> = T extends object ? { [k in keyof T]-?: DeepRequired<T[k]> } : T; |
| let settings: SettingsSchema; | ||
| let settings: FlatSettingsSchema; | ||
| try { | ||
| settings = (await apiGetJson('/api/settings/')) as SettingsSchema; |
Collaborator
Author
There was a problem hiding this comment.
no more type assertion! 🎉
cguedes
approved these changes
Sep 1, 2023
cguedes
left a comment
Collaborator
There was a problem hiding this comment.
Only added a note about the migrate_settings utility
| "react-popper": "^2.3.0", | ||
| "react-resizable-panels": "^0.0.51", | ||
| "tailwindcss": "^3.3.2", | ||
| "tauri-settings": "^0.3.2", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This solves the problem with optional fields in
SettingsSchemain TypeScript.In general I think we should avoid specifying default values in Pydantic models that are part of the HTTP API because the semantics are unclear. If you have something like:
Then if the users hits the PUT endpoint with
{"a": "archer"}, presumably their intention is to just set "a" to "archer" and not also set "b" to "bob".Highlights:
FlatSettingsSchemaand it has noOptionalfields or fields with default values.Partial<T>in TypeScript. I used this to define aFlatSettingsSchemaPatchmodel. This might be a useful pattern going forward. But if the update type is something more complicated (say you want to drop anidfield that can't be updated) then maybe just writing out the two models explicitly is the way to go.settings.jsonformat has changed, I wrote a migration function.getDotNotationfromtauri-settingsisn't useful anymore and we can replace it with something much simpler. In fact, we can get rid oftauri-settingsaltogether! 🎉