Skip to content

Conversation

@AdamGlustein
Copy link
Collaborator

@AdamGlustein AdamGlustein commented Oct 10, 2025

Cleanup and a few fixes to #574, thanks @sim15 for the contribution!

Simon Beyzerov and others added 4 commits September 16, 2025 12:03
Signed-off-by: Simon Beyzerov <simon.beyzerov@point72.com>
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
…ance bug

Signed-off-by: Adam Glustein <adam.glustein@point72.com>
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@timkpaine timkpaine added the type: enhancement Issues and PRs related to improvements to existing features label Oct 10, 2025
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@ptomecek
Copy link
Collaborator

ptomecek commented Oct 27, 2025

Previously, when all fields on structs were optional, we hard-coded these two flags for pydantic validation

Can we only apply this logic when allow_unset=True?
I think that when allow_unset is False, pydantic should infer it correctly based on the declared type and the default value, though we should add a unit test.

@AdamGlustein
Copy link
Collaborator Author

Previously, when all fields on structs were optional, we hard-coded these two flags for pydantic validation

Can we only apply this logic when allow_unset=True? I think that when allow_unset is False, pydantic should infer it correctly based on the declared type and the default value, though we should add a unit test.

I tested out this approach and it doesn't validate correctly in the case of a required field with a default value.

class MyStrictStruct(csp.Struct, allow_unset=False):
            req_default_str: str = "default"
E       pydantic_core._pydantic_core.SchemaError: Error building "list" validator:
E         SchemaError: Error building "function-wrap" validator:
E         SchemaError: Error building "typed-dict" validator:
E         SchemaError: Field 'req_default_str': a required field cannot have a default value

@AdamGlustein AdamGlustein requested a review from ptomecek November 3, 2025 20:59
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@ptomecek
Copy link
Collaborator

Previously, when all fields on structs were optional, we hard-coded these two flags for pydantic validation

Can we only apply this logic when allow_unset=True? I think that when allow_unset is False, pydantic should infer it correctly based on the declared type and the default value, though we should add a unit test.

I tested out this approach and it doesn't validate correctly in the case of a required field with a default value.

class MyStrictStruct(csp.Struct, allow_unset=False):
            req_default_str: str = "default"
E       pydantic_core._pydantic_core.SchemaError: Error building "list" validator:
E         SchemaError: Error building "function-wrap" validator:
E         SchemaError: Error building "typed-dict" validator:
E         SchemaError: Field 'req_default_str': a required field cannot have a default value

I think this just means that if there is a default value, we leave it as "optional" (as the user doesn't have to provide it), but if there isn't, then it's not. I guess I was wrong about pydantic inferring it. However, the behavior for non-defaulted fields will be different for the two types of Struct.

@AdamGlustein AdamGlustein marked this pull request as draft November 19, 2025 21:23
…ield is set to None in the struct's bitmask

Signed-off-by: Adam Glustein <adam.glustein@point72.com>
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@AdamGlustein AdamGlustein marked this pull request as ready for review November 21, 2025 17:46
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
Signed-off-by: Adam Glustein <adam.glustein@point72.com>
… test

Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@AdamGlustein
Copy link
Collaborator Author

AdamGlustein commented Dec 30, 2025

Previously, when all fields on structs were optional, we hard-coded these two flags for pydantic validation

Can we only apply this logic when allow_unset=True? I think that when allow_unset is False, pydantic should infer it correctly based on the declared type and the default value, though we should add a unit test.

I tested out this approach and it doesn't validate correctly in the case of a required field with a default value.

class MyStrictStruct(csp.Struct, allow_unset=False):
            req_default_str: str = "default"
E       pydantic_core._pydantic_core.SchemaError: Error building "list" validator:
E         SchemaError: Error building "function-wrap" validator:
E         SchemaError: Error building "typed-dict" validator:
E         SchemaError: Field 'req_default_str': a required field cannot have a default value

I think this just means that if there is a default value, we leave it as "optional" (as the user doesn't have to provide it), but if there isn't, then it's not. I guess I was wrong about pydantic inferring it. However, the behavior for non-defaulted fields will be different for the two types of Struct.

I think this just means that if there is a default value, we leave it as "optional" (as the user doesn't have to provide it)

I am not sure what you mean by this. In the semantics of structs, a: str= 'default' != a: Optional[str] = 'default'. The latter means the field is nullable, the former does not.

@robambalu robambalu self-requested a review December 30, 2025 16:15
robambalu
robambalu previously approved these changes Dec 30, 2025
Copy link
Collaborator

@robambalu robambalu left a comment

Choose a reason for hiding this comment

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

Looks gtg, lets wait on Pascal for that open conversation though

Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@AdamGlustein
Copy link
Collaborator Author

Previously, when all fields on structs were optional, we hard-coded these two flags for pydantic validation

Can we only apply this logic when allow_unset=True? I think that when allow_unset is False, pydantic should infer it correctly based on the declared type and the default value, though we should add a unit test.

I tested out this approach and it doesn't validate correctly in the case of a required field with a default value.

class MyStrictStruct(csp.Struct, allow_unset=False):
            req_default_str: str = "default"
E       pydantic_core._pydantic_core.SchemaError: Error building "list" validator:
E         SchemaError: Error building "function-wrap" validator:
E         SchemaError: Error building "typed-dict" validator:
E         SchemaError: Field 'req_default_str': a required field cannot have a default value

I think this just means that if there is a default value, we leave it as "optional" (as the user doesn't have to provide it), but if there isn't, then it's not. I guess I was wrong about pydantic inferring it. However, the behavior for non-defaulted fields will be different for the two types of Struct.

I think this just means that if there is a default value, we leave it as "optional" (as the user doesn't have to provide it)

I am not sure what you mean by this. In the semantics of structs, a: str= 'default' != a: Optional[str] = 'default'. The latter means the field is nullable, the former does not.

Added logic to require fields without a default value in the case of a strict struct: 80f26d3

Signed-off-by: Adam Glustein <adam.glustein@point72.com>
@ptomecek
Copy link
Collaborator

Looks gtg, lets wait on Pascal for that open conversation though

All good based on 80f26d3 and bd403e5

@AdamGlustein AdamGlustein merged commit ba23862 into main Dec 30, 2025
26 checks passed
@AdamGlustein AdamGlustein deleted the strict-structs branch December 30, 2025 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement Issues and PRs related to improvements to existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants