MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / UnaryExpression

Class UnaryExpression

lib/sqlalchemy/sql/elements.py:3888–4036  ·  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

3886
3887
3888class UnaryExpression(ColumnElement[_T]):
3889 """Define a 'unary' expression.
3890
3891 A unary expression has a single column expression
3892 and an operator. The operator can be placed on the left
3893 (where it is called the 'operator') or right (where it is called the
3894 'modifier') of the column expression.
3895
3896 :class:`.UnaryExpression` is the basis for several unary operators
3897 including those used by :func:`.desc`, :func:`.asc`, :func:`.distinct`,
3898 :func:`.nulls_first` and :func:`.nulls_last`.
3899
3900 """
3901
3902 __visit_name__ = "unary"
3903
3904 _traverse_internals: _TraverseInternalsType = [
3905 ("element", InternalTraversal.dp_clauseelement),
3906 ("operator", InternalTraversal.dp_operator),
3907 ("modifier", InternalTraversal.dp_operator),
3908 ]
3909
3910 element: ColumnElement[Any]
3911 operator: Optional[OperatorType]
3912 modifier: Optional[OperatorType]
3913
3914 def __init__(
3915 self,
3916 element: ColumnElement[Any],
3917 *,
3918 operator: Optional[OperatorType] = None,
3919 modifier: Optional[OperatorType] = None,
3920 type_: Optional[_TypeEngineArgument[_T]] = None,
3921 wraps_column_expression: bool = False, # legacy, not used as of 2.0.42
3922 ):
3923 self.operator = operator
3924 self.modifier = modifier
3925 self._propagate_attrs = element._propagate_attrs
3926 self.element = element.self_group(
3927 against=self.operator or self.modifier
3928 )
3929
3930 # if type is None, we get NULLTYPE, which is our _T. But I don't
3931 # know how to get the overloads to express that correctly
3932 self.type = type_api.to_instance(type_) # type: ignore
3933
3934 def _wraps_unnamed_column(self):
3935 ungrouped = self.element._ungroup()
3936 return (
3937 not isinstance(ungrouped, NamedColumn)
3938 or ungrouped._non_anon_label is None
3939 )
3940
3941 @classmethod
3942 def _create_nulls_first(
3943 cls,
3944 column: _ColumnExpressionArgument[_T],
3945 ) -> 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 9

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