MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / UnaryExpression

Class UnaryExpression

lib/sqlalchemy/sql/elements.py:3604–3751  ·  view source on GitHub ↗

Define a 'unary' expression. A unary expression has a single column expression and an operator. The operator can be placed on the left (where it is called the 'operator') or right (where it is called the 'modifier') of the column expression. :class:`.UnaryExpression` is the ba

Source from the content-addressed store, hash-verified

3602
3603
3604class UnaryExpression(ColumnElement[_T]):
3605 """Define a 'unary' expression.
3606
3607 A unary expression has a single column expression
3608 and an operator. The operator can be placed on the left
3609 (where it is called the 'operator') or right (where it is called the
3610 'modifier') of the column expression.
3611
3612 :class:`.UnaryExpression` is the basis for several unary operators
3613 including those used by :func:`.desc`, :func:`.asc`, :func:`.distinct`,
3614 :func:`.nulls_first` and :func:`.nulls_last`.
3615
3616 """
3617
3618 __visit_name__ = "unary"
3619
3620 _traverse_internals: _TraverseInternalsType = [
3621 ("element", InternalTraversal.dp_clauseelement),
3622 ("operator", InternalTraversal.dp_operator),
3623 ("modifier", InternalTraversal.dp_operator),
3624 ]
3625
3626 element: ColumnElement[Any]
3627 operator: Optional[OperatorType]
3628 modifier: Optional[OperatorType]
3629
3630 def __init__(
3631 self,
3632 element: ColumnElement[Any],
3633 *,
3634 operator: Optional[OperatorType] = None,
3635 modifier: Optional[OperatorType] = None,
3636 type_: Optional[_TypeEngineArgument[_T]] = None,
3637 wraps_column_expression: bool = False, # legacy, not used as of 2.0.42
3638 ):
3639 self.operator = operator
3640 self.modifier = modifier
3641 self._propagate_attrs = element._propagate_attrs
3642 self.element = element.self_group(
3643 against=self.operator or self.modifier
3644 )
3645
3646 # if type is None, we get NULLTYPE, which is our _T. But I don't
3647 # know how to get the overloads to express that correctly
3648 self.type = type_api.to_instance(type_) # type: ignore
3649
3650 def _wraps_unnamed_column(self):
3651 ungrouped = self.element._ungroup()
3652 return (
3653 not isinstance(ungrouped, NamedColumn)
3654 or ungrouped._non_anon_label is None
3655 )
3656
3657 @classmethod
3658 def _create_nulls_first(
3659 cls,
3660 column: _ColumnExpressionArgument[_T],
3661 ) -> UnaryExpression[_T]:

Callers 15

CoreFixturesClass · 0.90
_negateMethod · 0.85
_negateMethod · 0.85
_negateMethod · 0.85
_create_nulls_firstMethod · 0.85
_create_nulls_lastMethod · 0.85
_create_descMethod · 0.85
_create_ascMethod · 0.85
_create_distinctMethod · 0.85
_create_bitwise_notMethod · 0.85
_negateMethod · 0.85
_neg_implFunction · 0.85

Calls

no outgoing calls

Tested by 7

factorialMethod · 0.68
factorial_prefixMethod · 0.68
__invert__Method · 0.68
modulusMethod · 0.68
modulus_prefixMethod · 0.68
test_unary_no_opsMethod · 0.68
test_unary_both_opsMethod · 0.68