Get the indexable variable-constant comparison in `exprnode`. A tuple of (variable, operation, constant) is returned if `exprnode` is a variable-constant (or constant-variable) comparison, and the variable is in `indexedcols`. A normal variable can also be used instead of a constan
(
exprnode: ne.expressions.ExpressionNode,
indexedcols: frozenset[str],
)
| 85 | |
| 86 | @_check_indexable_cmp |
| 87 | def _get_indexable_cmp( |
| 88 | exprnode: ne.expressions.ExpressionNode, |
| 89 | indexedcols: frozenset[str], |
| 90 | ) -> tuple[Any, str, Any] | tuple[None, None, None]: |
| 91 | """Get the indexable variable-constant comparison in `exprnode`. |
| 92 | |
| 93 | A tuple of (variable, operation, constant) is returned if |
| 94 | `exprnode` is a variable-constant (or constant-variable) |
| 95 | comparison, and the variable is in `indexedcols`. A normal |
| 96 | variable can also be used instead of a constant: a tuple with its |
| 97 | name will appear instead of its value. |
| 98 | |
| 99 | Otherwise, the values in the tuple are ``None``. |
| 100 | """ |
| 101 | not_indexable = (None, None, None) |
| 102 | turncmp = { |
| 103 | "lt": "gt", |
| 104 | "le": "ge", |
| 105 | "eq": "eq", |
| 106 | "ge": "le", |
| 107 | "gt": "lt", |
| 108 | } |
| 109 | |
| 110 | def get_cmp( |
| 111 | var: ne.expressions.ExpressionNode, |
| 112 | const: ne.expressions.ExpressionNode, |
| 113 | op: str, |
| 114 | ) -> tuple[Any, str, Any] | None: |
| 115 | var_value, const_value = var.value, const.value |
| 116 | if ( |
| 117 | var.astType == "variable" |
| 118 | and var_value in indexedcols |
| 119 | and const.astType in ["constant", "variable"] |
| 120 | ): |
| 121 | if const.astType == "variable": |
| 122 | const_value = (const_value,) |
| 123 | return (var_value, op, const_value) |
| 124 | return None |
| 125 | |
| 126 | def is_indexed_boolean(node: ne.expressions.ExpressionNode) -> bool: |
| 127 | return ( |
| 128 | node.astType == "variable" |
| 129 | and node.astKind == "bool" |
| 130 | and node.value in indexedcols |
| 131 | ) |
| 132 | |
| 133 | # Boolean variables are indexable by themselves. |
| 134 | if is_indexed_boolean(exprnode): |
| 135 | return (exprnode.value, "eq", True) |
| 136 | # And so are negations of boolean variables. |
| 137 | if exprnode.astType == "op" and exprnode.value == "invert": |
| 138 | child = exprnode.children[0] |
| 139 | if is_indexed_boolean(child): |
| 140 | return (child.value, "eq", False) |
| 141 | # A negation of an expression will be returned as ``~child``. |
| 142 | # The indexability of the negated expression will be decided later on. |
| 143 | if child.astKind == "bool": |
| 144 | return (child, "invert", None) |
no test coverage detected