-
Notifications
You must be signed in to change notification settings - Fork 38
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
Pydantic V2 #313
Comments
@sinisaos Thanks for looking into it. It's a shame about the breaking changes to the schema - but we should be able to work around it. I wonder if the schema has some kind of version number in it. |
@dantownsend When the time comes, we'll probably be able to fix everything. There is now an elif isinstance(column, Text):
field = pydantic.Field(format="text-area", extra=extra, **params) but for example I don't know if this is a good solution, but this way I was able to fix the export function convertFormValue(params: {
key: string
value: any
schema: Schema
}): any {
let { key, value, schema } = params
if (value == "null") {
value = null
} else if (schema.properties[key]["anyOf"][0].type == "array") {
value = JSON.parse(String(value))
} else if (schema?.properties[key]["anyOf"][0].format == "uuid" && value == "") {
value = null
} else if (schema?.properties[key]["anyOf"][0].format == "email" && value == "") {
value = null
} else if (schema?.properties[key]["anyOf"][0].format == "date-time" && value == "") {
value = null
} else if (schema?.properties[key]["anyOf"][0].type == "integer" && value == "") {
value = null
} else if (schema?.properties[key]["anyOf"][0].type == "number" && value == "") {
value = null
} else if (
schema?.properties[key].extra.foreign_key == true &&
value == ""
) {
value = null
}
return value
} |
@dantownsend |
OK, so the schema for timedelta now says:
And before it was:
I wonder why they decided to change it? I wonder if there's any significance in JSON schema? Pydantic currently serialises a timedelta into a number: https://demo1.piccolo-orm.com/api/tables/movie/1/?__readable=true {
"id": 1,
"name": "Star Wars: Episode IV - A New Hope",
"rating": 93.3,
"duration": 7500.0, // <- THIS
// ...
} I wonder what the new string representation looks like - do you know? |
Here's a issue for that.
This is new representation {
"id": 1,
"name": "Star Wars: Episode IV - A New Hope",
"rating": 93.3,
"duration": "PT7500S", // <- THIS
// ...
} |
@dantownsend |
Thanks for that. It's worth taking a look at alternatives to moment.js: https://momentjs.com/docs/#/-project-status/ Even though it's still commonly used, it's not recommended for new projects. One of the reasons is it's not 'tree shakeable' so is quite a large dependency. |
@dantownsend // convert ISO 8601 duration string (e.g. `P17DT14706S`) to seconds
moment.duration(timeValue).asSeconds() and vice versa // convert seconds to ISO 8601 duration string (e.g. P17DT14706S)
moment.duration(value, 'seconds').toISOString() I will see if there is an equally good alternative. Everything is subject to change and is just a test of how much work there is when we move to |
I wonder how simple that |
@dantownsend Sorry, but I haven't found any equivalent |
@dantownsend Here are the changes: |
@dantownsend I found a library TinyDuration that is really small and should meet our parsing and conversion needs. I just need to try it. |
@sinisaos Cool, thanks a lot for that. That library looks promising. So the format is |
@dantownsend Sorry, but I haven't been able to get |
@sinisaos What's the problem with parsing seconds then? I had a quick play around, and >>> tinyduration.parse('PT6S')
{seconds: 6}
>>> tinyduration.serialize({'seconds': 6})
'PT6S' |
@dantownsend Everything works fine with that simple example, but we also have export function readableInterval(timeValue: string) {
let timeRange = 0
let parsedTimeValue = parse(timeValue)
if (parsedTimeValue["years"]) {
timeRange += parsedTimeValue["years"] * 31536000
}
if (parsedTimeValue["days"]) {
timeRange += parsedTimeValue["days"] * 86400
}
if (parsedTimeValue["seconds"]) {
timeRange += parsedTimeValue["seconds"]
}
.... The problem is parsing and serializing later. There are two videos that show exactly what I mean. moment.mp4With tinyduration.mp4If we try to edit the existing record, format |
@sinisaos Thanks for sharing those videos. I wonder why TinyDuration is adding those extra prefixes to the string 🤔 Just use moment.js for now then. I can always take a look at it later on, and switch to TinyDuration if I can figure it out. |
@dantownsend I have completed the changes to make the Piccolo ecosystem work with |
@sinisaos Yeah, I just realised Pydantic v2 has already been released. So it's pretty urgent now. I was hoping to finish the charts first, but this is more important. In the short term we need to version pin Pydantic / FastAPI in the Piccolo, Piccolo API and Piccolo Admin repos ASAP, otherwise they won't work any more. At least there will be a version which still works correctly with Pydantic v1, FastAPI < v0.100.0. Can you help me with this if you get a chance? And then next week we will have to bump the major version on each of the Piccolo repos, as the Pydantic v2 is such a big breaking change. |
@dantownsend For Piccolo, I have already pinned Pydantic to |
@sinisaos No worries, hope you have a great holiday! If you don't mind creating a PR to pin Piccolo API and Piccolo Admin I'll release them tonight. I'll review the Pydantic v2 PRs next week and will release them asap. |
@dantownsend Thanks. I'll check my email when I can and get back to you if I can be of any help. |
I've reviewed all the PRs for this - thanks a lot, I can see it was a lot of work. I'm going to release an alpha version of Piccolo 1.0 to PyPI, otherwise we can't get a PR passing for Piccolo API or Piccolo Admin. The order of the alpha releases will have to be Piccolo -> Piccolo API -> Piccolo Admin. |
@dantownsend Great. That sounds like a good plan. I will follow the progress as much as I can and try to be as helpful as I can since I am without a laptop. |
@sinisaos Sorry for the delay on this. Have been really busy with family stuff - I will get this released this week I promise. I can probably still recover the work from a local branch, if you can't reopen this PR. |
@sinisaos This has now been released - thanks for all your help with this. I've upgraded one of my apps, and it seems OK so far. |
Since the Piccolo ecosystem depends on Pydantic, the new
Pydantic V2
beta is out and libraries like FastAPI are available for testing.V2
brings a lot of braking backwards compatibility changes and I wanted to see how much work we'll have once we switch toV2
. I managed to change the code and tests in Piccolo ORM and Piccolo API and everything works (someDeprecationWarning
are left). The most changes are in Piccolo Admin, as expected, because the schema has changed (anyOf
keyword which we don't use etc.). Now the properties fields are arranged alphabetically (as well as Piccolo Admin form fields which is a bit strange), but I see that this has changed and that everything will be like inV1
(this merged PR). Mostly everything works except:- fixedDurationWidget
(Interval)- fixedFlatPickr
(datetime widget)- arrays works 😄ArrayWidget
(arrays and file-multifile upload)- fixednullable columns
mostly due to changes in schema. I'm sorry for the long comment, but I hope this will be useful and a placeholder for later changes we will need to make when we need to move to
V2
.The text was updated successfully, but these errors were encountered: