Distinguish between optional and required fields on csp structs #536
                  
                    
                      p72dennisxu
                    
                  
                
                  started this conversation in
                Ideas
              
            Replies: 1 comment 1 reply
-
| This was internally discussed and agreed upon. Here are some notes that came from the meeting: 
 | 
Beta Was this translation helpful? Give feedback.
                  
                    1 reply
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently any field on a csp struct could be unset, which means that to avoid
AttributeErrors at runtime, the user would need to manually perform thehasattrcheck every time. To improve this, we could add a new metaclass parameterallow_unsetthat defaults toTrue(for backwards compatibility) and when set toFalseenforces the following new semantics:T, then it's required on struct creation (currently it would be optional). If the user does not pass in a value and there's no default value, aTypeErrorwill be raised.Optional[T]orT | None, then it's not required to be set and defaults toNone.For example, under the new rules, the following struct definition
would imply that
MyStruct(x=3)is valid withy=NonebutMyStruct(y="hello")would raise something likeTypeError: MyStruct missing 1 required field: 'x' (int).Beta Was this translation helpful? Give feedback.
All reactions