Repository navigation
Allow enums and structs to be handled by pydantic - #428
Conversation
Signed-off-by: Nijat Khanbabayev <[email protected]>
Signed-off-by: Nijat Khanbabayev <[email protected]>
Signed-off-by: Nijat Khanbabayev <[email protected]>
Signed-off-by: Nijat Khanbabayev <[email protected]>
ptomecek
left a comment
There was a problem hiding this comment.
Some comments left in the review about testing, might need some small modifications to get this to work. Overall though, I like the approach and also think this adds very powerful functionality to csp around serialization/integration with other tools (like FastAPI).
|
Once we merge this, we should create an issue for adding documentation about this functionality to the wiki page about pydantic compatibility of csp structs. |
…ight type Signed-off-by: Nijat Khanbabayev <[email protected]>
There was a problem hiding this comment.
I noticed that the unit test is often using result.to_json to generate the json from the result, which does not go through the pydantic logic (rather it uses the native csp functionality). To go through the pydantic serialization logic, one should call TypeAdapter(cls).dump_json(), which will pick up any serializers in the annotations (whereas to_json will not).
I think we should be a bit more explicit in the tests. When testing pydantic round-trip of serialization/deserialization, use the TypeAdapter, and then have a separate test which checks that for basic types (and no custom serializers) that TypeAdapter.dump_json and Struct.to_json return the same thing.
69fb38b to
5cf0259
Compare
Added these tests, and actually they found a bug. Separately, Pydantic and csp give different results for datetime serializations to string. This can be bypassed with a custom serializer (which is demonstrated with this test here: https://github.com/Point72/csp/pull/428/files#diff-16e10f816ba9638999d038fb8bd076b4021e901b60e548b1bc392b6191026687R3649 |
5cf0259 to
e5dcc10
Compare
|
Notably, the csp behavior with csp Enums is to serialize them to a string when calling import csp
class MyEnum(csp.Enum):
OPTION1 = csp.Enum.auto()
OPTION2 = csp.Enum.auto()
class DummyS(csp.Struct):
e: MyEnum = MyEnum.OPTION1
val = DummyS()
print(f'{val.to_dict(), val.e = }')We intentionally let the pydantic route maintain the Enum class when converting to a dict (this is covered in the tests) |
e5dcc10 to
61dbb86
Compare
46451e4 to
71844f5
Compare
…Structs Signed-off-by: Nijat Khanbabayev <[email protected]>
71844f5 to
c1acca8
Compare
I think this makes sense, as this is what pydantic would do for regular python enums and string enums (unless converting to json) |
Signed-off-by: Nijat Khanbabayev <[email protected]>
|
As discussed, since csp treats underscore attributes as first class, but pydantic doesnt, we agreed on this behavior:
|
Signed-off-by: Nijat Khanbabayev <[email protected]>
Signed-off-by: Nijat Khanbabayev <[email protected]>
b756673 to
fd203b1
Compare
|
For classes without a pydantic schema, we perform one level of validation, then, we use an 'any_schema' to err on the side of caution to not erroneously throw validation errors. We pass the attribute onto csp to allow it to decide. |
ptomecek
left a comment
There was a problem hiding this comment.
Thank you for all the additional testing, which certainly brought out a number of edge cases we didn't originally think of. With all the additional test cases you've added and downstream testing, I am comfortable with the application of these changes in real-world usage.
FYI the reason we serialize the string is so that the encoding is stable across updates to the enum |
No description provided.