-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
DynamoDB condition operator arity not handled correctly, produces semi-cryptic error #4113
Comments
Hi @crh23, thanks for reaching out. You should be able to chain these conditions using logical operators. The |
Chaining does work, since using The main issues are the lack of documentation (#4117) and the lack of validation (this issue). It would be reasonable to expect the example I gave to work, but:
For context, this was raised by an actual user who I helped to fix their code - their code was similar to my given example, and they didn't understand why it wasn't working. (this would also be addressed with proper type signatures for boto3, but that's a somewhat larger fix :P ) |
Describe the bug
The binary condition operators in
boto3.dynamodb.conditions
, such asAnd
andOr
, don't correctly handle being given three or more arguments. This is not caught client-side, and causes a pretty cryptic error.Expected Behavior
Either the operators are handled correctly (ideally with type hinting), or a more relevant error is raised
Current Behavior
Errors are like
the generated request body from the below SSCCE is
which is indeed invalid as indicated in the error message.
Reproduction Steps
Example I would expect to work but doesn't:
Current workaround:
Possible Solution
Explanation:
The conditions all inherit from ConditionBase, which has properties
expression_operator
(the character in the DynamoDB condition expression syntax used to indicate the relevant operator),values
(the operands), andexpression_format
(a string with python format fields{operator}
and some of{0}
,{1}
,{2}
).The
expression_operator
andexpression_format
are defined by the operator, and the values are set on construction. For example,And
:boto3/boto3/dynamodb/conditions.py
Lines 226 to 228 in 096e458
Note that
ConditionBase
's constructor takes an arbitrary number of values.The number of values passed to the condition operator is never validated, which is the root cause of this error. The core of the expression builder is
boto3/boto3/dynamodb/conditions.py
Lines 359 to 383 in 096e458
Note that every value passed (the number of which is never checked) gets a placeholder value, but only as many as there are number format fields in
expression_format
will actually be included in the final condition expression (since Python'sstr.format()
silently ignores extraneous arguments).Possible fix:
Add a property
arity
ornumber_of_values
toConditionBase
, and have the constructor validate that a valid number has been received. Optionally, for quality of life also expand theAnd
andOr
constructs to take an arbitrary number of arguments (either by transparently expanding to"{0} AND {1} AND {2} AND {3}"
or"((({0} AND {1}) AND {2}) AND {3})"
)Additional Information/Context
No response
SDK version used
1.34.97
Environment details (OS name and version, etc.)
Windows 10
The text was updated successfully, but these errors were encountered: