
    O3j_                    *   U d dl mZ d dlmZmZ d dlmZmZmZm	Z	 d dl
mZ d dlmZmZmZ er>d dlmZmZmZ d dlmZmZ d dlmZmZ d d	lmZmZmZmZmZ d d
l m!Z! d dl"m#Z# d dl$m%Z%m&Z&m'Z' d+dZ(d,dZ)	 	 	 	 d-dZ*d.dZ+	 	 	 	 	 	 	 	 d/dZ, G d de      Z-d0dZ. G d de      Z/ G d d      Z0 G d d      Z1e-jd                  e1jf                  e-jh                  e1jj                  e-jl                  e1jn                  e-jp                  e1jj                  e-jr                  e1jt                  e-jv                  e1jx                  e-jz                  e1j|                  e-j~                  e1j                  e-j                  e1jj                  i	ZBdeCd<   e-jd                  e1j                  e-jl                  e1j                  e-j                  e1j                  e-j                  e1j                  e-j                  e1j                  e-j                  e1j                  e-j                  e1j                  e-j                  e1j                  iZRdeCd<   	 	 	 	 	 	 	 	 	 	 d1dZS	 	 	 	 	 	 d2d ZTd!d"d#d$	 	 	 	 	 	 	 	 	 d3d%ZU	 	 	 	 	 	 	 	 	 	 d4d&ZVd5d'ZWd6d(ZX	 	 	 	 	 	 	 	 d7d)ZY	 	 	 	 	 	 d8d*ZZy")9    )annotations)Enumauto)TYPE_CHECKINGAnyLiteralcast)is_numpy_array_1d)InvalidIntoExprErrorInvalidOperationErrorMultiOutputExpressionError)CallableIteratorSequence)NeverTypeIs)CompliantExprCompliantFrameT)
AliasNamesCompliantExprAnyCompliantFrameAnyCompliantNamespaceAny	EvalNamesExprSeries)IntoExprNonNestedLiteral_1DArrayc                &    ddl m} t        | |      S )'Check whether `obj` is a Narwhals Expr.r   r   )narwhals.exprr   
isinstance)objr   s     I/DATA/.local/lib/python3.12/site-packages/narwhals/_expression_parsing.pyis_exprr'   #   s    "c4      c                &    ddl m} t        | |      S )r"   r   r   )narwhals.seriesr   r$   )r%   r   s     r&   	is_seriesr+   *   s    &c6""r(   c                      d fd}|S )Nc                2    d   j                  |       d d S Nr      )_evaluate_output_names)dfexprss    r&   evaluate_output_namesz<combine_evaluate_output_names.<locals>.evaluate_output_names6   s    Qx..r22A66r(   )r1   r   returnSequence[str] )r2   r3   s   ` r&   combine_evaluate_output_namesr7   1   s    
7 ! r(   c                 4      d   j                   y d fd}|S )Nr   c                2    d   j                  |       d d S r.   _alias_output_names)namesr2   s    r&   alias_output_namesz6combine_alias_output_names.<locals>.alias_output_namesB   s    Qx++E22A66r(   )r<   r5   r4   r5   r:   )r2   r=   s   ` r&   combine_alias_output_namesr>   <   s%     Qx##+7 r(   c                   | j                  |      }| j                  x} ||      n|}|rW| j                  j                  j	                         r3t        t        ||d      D cg c]  \  }}||vr||f c}}ddi\  }}||fS c c}}w )NT)strictr@   )r0   r;   	_metadataexpansion_kindis_multi_unnamedzip)exprr1   excludeoutput_namesalias_fnaliasesxaliass           r&   !evaluate_output_names_and_aliasesrL   H   s     ..r2L 000H= 	 
 4>>00AAC # !$L'$ G GHAuG# E
 G!
 !
g   s   &B
c                  X   e Zd ZdZ e       Z	  e       Z	  e       Z	  e       Z	  e       Z		  e       Z
	  e       Z	  e       Z	  e       Z	  e       Z	  e       Z	  e       Z	  e       Z	  e       Z	  e       Z	  e       Z	 edd       Zedd       Zedd       Zedd       Zy)ExprKindz6Describe which kind of expression we are dealing with.c                    | t         j                  t         j                  t         j                  t         j                  hv S N)rN   ORDERABLE_WINDOWWINDOWORDERABLE_AGGREGATIONORDERABLE_FILTRATIONselfs    r&   is_orderablezExprKind.is_orderable   s8     %%OO**))	
 
 	
r(   c           
        | t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  t         j                  t         j                  h	v S rP   )
rN   ALLCOLELEMENTWISEEXCLUDELITERALNTHSELECTORSERIES	WHEN_THENrU   s    r&   is_elementwisezExprKind.is_elementwise   s_     LLLL  LLOO

 

 
	
r(   c                b    | j                   xs" | t        j                  t        j                  hv S rP   )rb   rN   rR   rQ   rU   s    r&   preserves_lengthzExprKind.preserves_length   s'    ""Zdx@Y@Y.Z&ZZr(   c                d    | t         j                  t         j                  t         j                  hv S rP   )rN   AGGREGATIONr]   rS   rU   s    r&   is_scalar_likezExprKind.is_scalar_like   s/      **
 
 	
r(   Nr4   bool)__name__
__module____qualname____doc__r   r]   rf   rS   r[   rQ   rR   
FILTRATIONrT   OVERrZ   r^   r\   rY   r_   ra   r`   propertyrW   rb   rd   rg   r6   r(   r&   rN   rN   ]   s    @fG&KY FX&KgvHVFNJG6=6D5
&C(
&C(fG,
&C(vH>IPVF3
 
 
 
 [ [ 
 
r(   rN   c                .    | j                   j                  S rP   )rA   rg   )r%   s    r&   rg   rg      s    =='''r(   c                  X    e Zd ZdZ e       Z	  e       Z	  e       Z	 ddZddZ	ddZ
y)ExpansionKindz8Describe what kind of expansion the expression performs.c                &    | t         j                  u S rP   rs   MULTI_UNNAMEDrU   s    r&   rC   zExpansionKind.is_multi_unnamed   s    }2222r(   c                F    | t         j                  t         j                  hv S rP   )rs   MULTI_NAMEDrv   rU   s    r&   is_multi_outputzExpansionKind.is_multi_output   s    11=3N3NOOOr(   c                    | t         j                  u r"|t         j                  u rt         j                  S d|  d| d}t        |      )Nz+Unsupported ExpansionKind combination, got z and z, please report a bug.)rs   rv   AssertionError)rV   othermsgs      r&   __and__zExpansionKind.__and__   sI    =...5M<W<W3W ...;D6ugMcdS!!r(   Nrh   )r|   rs   r4   z$Literal[ExpansionKind.MULTI_UNNAMED])rj   rk   rl   rm   r   SINGLErx   rv   rC   ry   r~   r6   r(   r&   rs   rs      s3    BVF9&K!FM'3P"r(   rs   c                  x    e Zd ZdZdddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZddZddZ	 	 	 	 	 	 dd	Zdd
Z	ddZ
y)ExprNodea  An operation to create or modify an expression.

    Parameters:
        kind: ExprKind of operation.
        name: Name of function, as defined in the compliant protocols.
        exprs: Expressifiable arguments to function.
        str_as_lit: Whether to interpret strings as literals when they
            are present in `exprs`.
        allow_multi_output: Whether to allow any of `exprs` to be multi-output.
        kwargs: Other (non-expressifiable) arguments to function.
    r6   F)r2   
str_as_litallow_multi_outputc              t    || _         || _        || _        || _        || _        || _        d | _        d | _        y rP   )kindnamer2   kwargsr   r   _is_orderable_cached_is_elementwise_cached)rV   r   r   r2   r   r   r   s          r&   __init__zExprNode.__init__   sA     #		<A
&, *(: 26!37#r(   c                   | j                   dk(  r+dj                  d | j                  d   D              }d| dS g }dj                  d | j                  D              }dj                  d | j                  j	                         D              }| j                  r|j                  |       | j                  r|j                  |       | j                    d	dj                  |       dS )
Ncolz, c              3  2   K   | ]  }t        |        y wrP   str.0rJ   s     r&   	<genexpr>z$ExprNode.__repr__.<locals>.<genexpr>   s     C.Bc!f.B   r<   zcol()c              3  2   K   | ]  }t        |        y wrP   r   r   s     r&   r   z$ExprNode.__repr__.<locals>.<genexpr>   s     9jc!fjr   c              3  0   K   | ]  \  }}| d |   yw)=Nr6   )r   keyvalues      r&   r   z$ExprNode.__repr__.<locals>.<genexpr>   s"     WCVZS%3%q 0CVs   ()r   joinr   r2   itemsappend)rV   r<   arg_str	expr_reprkwargs_reprs        r&   __repr__zExprNode.__repr__   s    99IICdkk'.BCCE%?"II9djj99	iiW4;;CTCTCVWW::NN9%;;NN;'))Adii0133r(   c                    | j                   | j                  | j                  | j                  | j                  | j
                  dS )Nr   r   r2   r   r   r   r   rU   s    r&   as_dictzExprNode.as_dict  s:     IIIIZZkk//"&"9"9
 	
r(   c                     | j                   | j                  | j                  f| j                  | j                  d|S )N)r2   r   )	__class__r   r   r2   r   )rV   r   s     r&   _with_kwargszExprNode._with_kwargs  s<    t~~IItyy
(,

t
RX
 	
r(   c                   g }|j                   d   }|j                   d   }| j                  D ]  }t        |      s|j                  |        |r=t	        d |j
                  D              r!|j                  |j                  |             _|r=t        d |j
                  D              s!|j                  |j                  |             |j                  |        || _        y )Norder_bypartition_byc              3  <   K   | ]  }|j                           y wrP   rW   r   	expr_nodes     r&   r   z9ExprNode._push_down_over_node_in_place.<locals>.<genexpr>"  s      ,:EY	&&(+   c              3  <   K   | ]  }|j                           y wrP   rb   r   s     r&   r   z9ExprNode._push_down_over_node_in_place.<locals>.<genexpr>&  s      4<Gy	((*Kr   )r   r2   r'   r   any_nodes_with_over_nodeall)rV   	over_nodeover_node_without_order_byr2   over_node_order_byover_node_partition_byrE   s          r&   _push_down_over_node_in_placez&ExprNode._push_down_over_node_in_place  s     46 '--j9!*!1!1.!AJJD4=T"# ,:>++, ) T11)<=' 4<@KK4 1 T112LMN T"  
r(   c                    | j                   _| j                  j                  rd| _         | j                   S t        d | j                  D              rd| _         | j                   S d| _         | j                   S )NTc              3  j   K   | ]+  }t        |      rt        d  |j                  D               - yw)c              3  <   K   | ]  }|j                           y wrP   r   r   nodes     r&   r   z2ExprNode.is_orderable.<locals>.<genexpr>.<genexpr>6  s     @KDD%%'Kr   Nr'   r   r   r   rE   s     r&   r   z(ExprNode.is_orderable.<locals>.<genexpr>5  s.      &D4= @DKK@@&   13F)r   r   rW   r   r2   rU   s    r&   rW   zExprNode.is_orderable/  s|    $$, yy%%,0) (((   JJ 
 -1) ((( -2)(((r(   c                    | j                   _| j                  j                  sd| _         | j                   S t        d | j                  D              rd| _         | j                   S d| _         | j                   S )NFc              3  j   K   | ]+  }t        |      rt        d  |j                  D               - yw)c              3  >   K   | ]  }|j                            y wrP   r   r   s     r&   r   z4ExprNode.is_elementwise.<locals>.<genexpr>.<genexpr>F  s     F+$++--+s   Nr   r   s     r&   r   z*ExprNode.is_elementwise.<locals>.<genexpr>E  s.      &D4= F$++FF&r   T)r   r   rb   r   r2   rU   s    r&   rb   zExprNode.is_elementwise?  s|    &&. 99++.3+ ***   JJ 
 /4+ *** /3+***r(   N)r   rN   r   r   r2   z%Sequence[IntoExpr | NonNestedLiteral]r   ri   r   ri   r   r   r4   Noner4   r   )r4   zdict[str, Any])r   r   r4   r   )r   r   r   r   r4   r   rh   )rj   rk   rl   rm   r   r   r   r   r   rW   rb   r6   r(   r&   r   r      s    
$ 8: #(88 8 58 8 !8 8 
8,4	


!?G	0) +r(   r   c                      e Zd ZdZdZdddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"dZd#d	Zd$d
Zd%dZe		 	 	 	 	 	 d&d       Z
	 	 	 	 	 	 	 	 d'dZe		 	 	 	 	 	 d&d       Ze	d(d       Ze	d(d       Ze	d(d       Ze	d(d       Ze	d(d       Ze	d(d       Ze		 	 	 	 	 	 d&d       Zed)d       Zd*dZ	 	 	 	 	 	 d*dZ	 	 	 	 	 	 	 	 d'dZd*dZ	 	 	 	 	 	 d*dZd*dZ	 	 	 	 	 	 d*dZd*dZ	 	 	 	 	 	 d&dZ	 	 	 	 	 	 d*d Zd%d!Z y)+ExprMetadataaI  Expression metadata.

    Parameters:
        expansion_kind: What kind of expansion the expression performs.
        has_windows: Whether it already contains window functions.
        is_elementwise: Whether it can operate row-by-row without context
            of the other rows around it.
        is_literal: Whether it is just a literal wrapped in an expression.
        is_scalar_like: Whether it is a literal or an aggregation.
        n_orderable_ops: The number of order-dependent operations. In the
            lazy case, this number must be `0` by the time the expression
            is evaluated.
        preserves_length: Whether the expression preserves the input length.
        current_node: The current ExprNode in the linked list.
        prev: Reference to the previous ExprMetadata in the linked list (None for root).
    )	current_noderB   has_windowsrb   
is_literalrg   n_orderable_opsrd   prevFr   TN)r   r   rd   rb   rg   r   r   c                   |r|sJ || _         || _        || _        || _        || _        || _        || _        || _        |	| _        y rP   )	rB   r   r   rb   rd   rg   r   r   r   )
rV   rB   r   r   rd   rb   rg   r   r   r   s
             r&   r   zExprMetadata.__init__n  sU     !!>-;!,$3$2&6$2 *&2)-	r(   c               6    d| j                   }t        |      )NzCannot subclass )rj   	TypeError)clsargskwdsr}   s       r&   __init_subclass__zExprMetadata.__init_subclass__  s      01nr(   c                   t        t        t        | j                                           }d| j                   d| j                   d| j
                   d| j                   d| j                   d| j                   d| j                   d| d	S )
Nz ExprMetadata(
  expansion_kind: z,
  has_windows: z,
  n_orderable_ops: z,
  is_elementwise: z,
  preserves_length: z,
  is_scalar_like: z,
  is_literal: z,
  nodes: z,
))
tuplereversediter_nodes_reversedrB   r   r   rb   rd   rg   r   )rV   nodess     r&   r   zExprMetadata.__repr__  s    huT%=%=%?@AB!!%!4!4 5 6"../ 0""&"6"6!7 8!!%!4!4 5 6##'#8#8"9 :!!%!4!4 5 6!__- .w 		
r(   c              #  P   K   | }||j                    |j                  }|yyw)z/Iterate through all nodes from current to root.Nr   r   )rV   currents     r&   r   z ExprMetadata.iter_nodes_reversed  s.     '+!&&&llG !s   !&&c                4    t        |j                     |g| S rP   )KIND_TO_METADATA_CONSTRUCTORr   r   r   compliant_exprss      r&   	from_nodezExprMetadata.from_node  s     ,DII6tNoNNr(   c                8    t        |j                     | ||g| S rP   )KIND_TO_METADATA_UPDATERr   rV   r   compliant_exprcompliant_expr_argss       r&   	with_nodezExprMetadata.with_node  s)     (		2$
)<
 	
r(   c                    t        |d|d dS NTto_single_outputr   r   combine_metadatar   s      r&   from_aggregationzExprMetadata.from_aggregation        t$T
 	
r(   c           	     <     | t         j                  dddd|d       S )NTF)rb   rd   r   rg   r   r   rs   r   r   r   s     r&   from_literalzExprMetadata.from_literal  s*      "
 	
r(   c                4     | t         j                  |d       S Nr   r   r   s     r&   from_serieszExprMetadata.from_series  s    =''dFFr(   c                    t        |j                  d         dk(  r | t        j                  |d       S | j	                  |      S )Nr<   r/   r   lenr   rs   r   from_selector_multi_namedr   s     r&   from_colzExprMetadata.from_col  sH    
 4;;w'(A- $$4dC	
 ..t4	
r(   c                    t        |j                  d         dk(  r | t        j                  |d       S | j	                  |      S )Nindicesr/   r   r   r   s     r&   from_nthzExprMetadata.from_nth  sH     4;;y)*a/ $$4dC	
 ..t4	
r(   c                4     | t         j                  |d       S r   )rs   rx   r   s     r&   r   z&ExprMetadata.from_selector_multi_named  s     =,,4dKKr(   c                4     | t         j                  |d       S r   ru   r   s     r&   from_selector_multi_unnamedz(ExprMetadata.from_selector_multi_unnamed  s     =..TMMr(   c                    t        |d|d dS r   r   r   s      r&   from_elementwisezExprMetadata.from_elementwise  r   r(   c                :    | j                    xr | j                   S rP   )rd   rg   rU   s    r&   is_filtrationzExprMetadata.is_filtration  s    (((D1D1D-DDr(   c                    | j                   rd}t        |      t        | j                  | j                  | j
                  dddd|| 	      S )N4Can't apply aggregations to scalar-like expressions.FTr   r   rd   rb   rg   r   r   r   rg   r   r   rB   r   r   rV   r   _cer}   s       r&   with_aggregationzExprMetadata.with_aggregation  sU    HC',,(( 00" 

 
	
r(   c                    | j                   rd}t        |      t        | j                  | j                  | j
                  dz   dddd|| 	      S )Nr  r/   FTr	  r
  r  s       r&   with_orderable_aggregationz'ExprMetadata.with_orderable_aggregation   s\     HC',,(( 0014" 

 
	
r(   c                8    t        |g|d||j                  dS )NFr   )r   rA   r   s       r&   with_elementwisezExprMetadata.with_elementwise  s2      
 
 #))
 	
r(   c                    | j                   rd}t        |      t        | j                  | j                  | j
                  | j                  ddd|| 	      S )Nz;Can't apply window (e.g. `rank`) to scalar-like expression.Fr	  rg   r   r   rB   r   r   rd   r  s       r&   with_windowzExprMetadata.with_window!  s]    OC',,(( !00!22  
 	
r(   c                    | j                   rd}t        |      t        | j                  | j                  | j
                  dz   | j                  ddd|| 	      S )NzNCan't apply orderable window (e.g. `diff`, `shift`) to scalar-like expression.r/   Fr	  r  r  s       r&   with_orderable_windowz"ExprMetadata.with_orderable_window4  sb     bC',,(( 0014!22  

 
	
r(   c                   | j                   rd}t        |      | j                  s| j                  rd}t        |      | j                  }|s@t        | j                               j                  t        j                  urd}t        |      t        | j                               j                  j                  r
|dkD  r|dz  }t        | j                  d|dddd|| 	      S )	NCannot nest `over` statements.oCannot use `over` on expressions which are elementwise
(e.g. `abs`) or which change length (e.g. `drop_nulls`).aP  Cannot use `order_by` in `over` on expression which isn't orderable.
If your expression is orderable, then make sure that `over(order_by=...)`
comes immediately after the order-dependent expression.

Hint: instead of
  - `(nw.col('price').diff() + 1).over(order_by='date')`
write:
  + `nw.col('price').diff().over(order_by='date') + 1`
r   r/   TFr	  )r   r   rb   r  r   nextop_nodes_reversedr   rN   rR   rW   r   rB   )rV   r   r  r}   r   s        r&   with_ordered_overzExprMetadata.with_ordered_overG  s    2C',,$"4"4K  (,,..T++-.338??JK  (,,&&()..;;RS@Sq O+!  

 
	
r(   c                    | j                   rd}t        |      | j                  s| j                  rd}t        |      t	        | j
                  d| j                  dddd|| 	      S )Nr  r  TFr	  )r   r   rb   r  r   rB   r   r  s       r&   with_partitioned_overz"ExprMetadata.with_partitioned_overn  sz     2C',,$"4"4K  (,, 00!  

 
	
r(   c                    |j                   d   r| j                  ||      S |j                   d   sd}t        |      | j                  ||      S )Nr   r   z?At least one of `partition_by` or `order_by` must be specified.)r   r  r   r  r  s       r&   	with_overzExprMetadata.with_over  sP    ;;z"))$44{{>*SC',,))$44r(   c                    | j                   rd}t        |      t        d |D              }t        d |D              }t	        | j
                  ||dddd|| 	      S )NECan't apply filtration (e.g. `drop_nulls`) to scalar-like expression.c              3  H   K   | ]  }|j                   j                    y wrP   )rA   r   r   s     r&   r   z/ExprMetadata.with_filtration.<locals>.<genexpr>  s      R/Q!8!8/    "c              3  H   K   | ]  }|j                   j                    y wrP   )rA   r   r   s     r&   r   z/ExprMetadata.with_filtration.<locals>.<genexpr>  s     $Z/QQ[[%@%@/r$  Fr	  )rg   r   r   sumr   rB   )rV   r   r   r}   result_has_windowsresult_n_orderable_opss         r&   with_filtrationzExprMetadata.with_filtration  sm     YC',,  R/ RR!$$Z/$Z!Z*2"  

 
	
r(   c                    | j                   rd}t        |      t        | j                  | j                  | j
                  dz   dddd|| 	      S )Nr"  r/   Fr	  r
  r  s       r&   with_orderable_filtrationz&ExprMetadata.with_orderable_filtration  s\     YC',,(( 0014"  

 
	
r(   c              #  v   K   | j                         D ]"  }|j                  j                  d      r| $ y w)N)zname.rK   )r   r   
startswith)rV   r   s     r&   r  zExprMetadata.op_nodes_reversed  s4     ,,.Dyy##$67J	 /s   79)rB   rs   r   ri   r   intrd   ri   rb   ri   rg   ri   r   ri   r   r   r   ExprMetadata | Noner4   r   )r   r   r   r   r4   r   r   )r4   zIterator[ExprNode])r   r   r   r   r4   r   )r   r   r   r   r   r   r4   r   )r   r   r4   r   rh   )r   r   r  r   r4   r   )!rj   rk   rl   rm   	__slots__r   r   r   r   classmethodr   r   r   r   r   r   r   r   r  r  rp   r  r  r  r  r  r  r  r  r   r)  r+  r  r6   r(   r&   r   r   P  s   "
I  " !%#$ $(.%. 	.
 . . . . . . ". 
.2
# OO/?O	O O


 )
 /	

 

 

/?
	
 
 	
 	
 G G 
 
 
 
 L L N N 

/?
	
 
 E E
 

#3
	
&

 )
 /	

 


&

#3
	
&%
N

#3
	
05

0@
	
(

#3
	
$r(   r   z2dict[ExprKind, Callable[[ExprNode], ExprMetadata]]r   z+dict[ExprKind, Callable[..., ExprMetadata]]r   c                   d}d}t         j                  }d}d}|j                  j                  }	d}
t	        |      D ]  \  }}|j
                  }|J |j                  j                         r|j                  }| s|dkD  r||z  n|}||j                  z  }||j                  z  }|	|j                  z  }	|
|j                  z  }
|t        |j                        z  }|t        |j                        z  } |j                  j                  xs |t        |      k(  }|j                  j                  xr
 |dk(  xr | }|r#|dkD  s||z   t        |      k  rd}t!        |      t#        |||||	||
||	      S )aI  Combine metadata from `args`.

    Arguments:
        compliant_exprs: Expression arguments.
        to_single_output: Whether the result is always single-output, regardless
            of the inputs (e.g. `nw.sum_horizontal`).
        current_node: The current node being added.
        prev: ExprMetadata of previous node.
    r   FTr/   zXLength-changing expressions can only be used in isolation, or followed by an aggregationr	  )rs   r   r   rb   	enumeraterA   rB   ry   r   r   r   r.  r  rg   r   rd   r   r   )r   r   r   r   n_filtrationsn_scalar_likeresult_expansion_kindr'  r(  result_is_elementwiseresult_is_literalicemetadatarB   result_is_scalar_likeresult_preserves_lengthr}   s                     r&   r   r     s    MM)00(--<<?+2<<###""224%44N#>?!e)N: & 	h222(":"::!8!88X000X3344X4455 ,$ )--<< _--  	** 	&Q	&%%  		 =O@T Th#C((&.0,,$!
 
r(   c                J    t        d |D              sd|  d}t        |      y )Nc              3  H   K   | ]  }|j                   j                    y wrP   )rA   rd   r   s     r&   r   z4check_expressions_preserve_length.<locals>.<genexpr>&  s     :Tq{{++Tr$  zBExpressions which aggregate or change length cannot be passed to 'z'.)r   r   )function_namer   r}   s      r&   !check_expressions_preserve_lengthrA    s3     :T::RS`Raacd#C(( ;r(   FNT)r   backendallow_literalc               2   ddl m}m}m} t	        | t
              r
|s ||       S t        |       r |d| |      j                         S t        |       r| j                         S t        |       r| S |st        j                  t        |              ||       S )Nr   )r   lit
new_series )rB  )narwhals.functionsr   rE  rF  r$   r   r
   _to_exprr+   r'   r   from_invalid_typetype)argr   rB  rC  r   rE  rF  s          r&   _parse_into_exprrM  +  s     87#sJ3x"c73<<>>~||~s|
"44T#Y??s8Or(   c              '     K   |D ]`  }t        ||| j                        j                  |       }|s1|j                  j                  j                         rd}t        |      | b y w)N)r   rB  z9Multi-output expressions are not allowed in this context.)rM  _implementation_to_compliant_exprrA   rB   ry   r   )nsr   r   r2   rE   retr}   s          r&   evaluate_into_exprsrS  A  sf      Z1C1C


R
  	 "cmm&B&B&R&R&TMC,S11	 s   A'A)c                     t        d | D              }g }| D ]S  }|r>t        |      r3|j                         }|j                  |_        |j                  |       C|j                  |       U |S )Nc              3  4   K   | ]  }t        |         y wrP   )rg   )r   r:  s     r&   r   z&maybe_broadcast_ces.<locals>.<genexpr>R  s     E_rr**_s   )r   rg   	broadcastrA   _opt_metadatar   )r   rV  resultsr   _compliant_exprs        r&   maybe_broadcast_cesrZ  Q  sf    E_EEI&(G)70>0H0H0JO,:,D,DO)NN?+NN>* * Nr(   c           	        | j                   dv r) t        || j                         | j                  d    }g }nd| j                   v r5| j                   j                  d      \  }}t        t        ||      |      }nt        || j                         }t	        t        | j                  || j                  | j                  d }t        d ||i | j                        }t        j                  | g| }||_        |S )N>   r   rF   r<   .rQ  r   r   r   )r   getattrr   splitrZ  rS  r2   r   r   r	   r   r   rW  )r   rQ  r:  cesmodulemethodfuncmds           r&   evaluate_root_nodere  _  s    yy&& $WR#T[[%9:$))!YY__S1NFF72v.7D2tyy)D! ??#'#:#:	
 $dC&?4;;&?@				+s	+BBIr(   c           
        | j                   }t        | gt        |j                  ||j                  |j
                  d ^} } |j                  || g| }d|j                  v r5|j                  j                  d      \  }}t        t        | |      |      }nt        | |j                        }t        d ||i |j                        }||_        |S )Nr]  r\  r   )rA   rZ  rS  r2   r   r   r   r   r_  r^  r	   r   rW  )	r   r   rQ  rd  r   accessorrb  rc  rR  s	            r&   evaluate_noderh  y  s     &//B+>,	ZZ#66	

,(N( 
dN	A-@	AB
dii99??3/&w~x8&A~tyy1
!4)<#L#L
MCCJr(   c                R    t        | d   |      }| dd  D ]  }t        |||      } |S r.   )re  rh  )r   rQ  r:  r   s       r&   evaluate_nodesrj    s7     
E!Hb	)Bab	2tR( Ir(   )r%   r   r4   zTypeIs[Expr])r%   r   r4   zTypeIs[Series[Any]])r2   z#CompliantExpr[CompliantFrameT, Any]r4   zEvalNames[CompliantFrameT])r2   r   r4   zAliasNames | None)rE   r   r1   r   rF   r5   r4   z#tuple[Sequence[str], Sequence[str]])r%   r   r4   ri   )
r   r   r   ri   r   r   r   r/  r4   r   )r   r   r@  r   r4   r   )
rL  &IntoExpr | NonNestedLiteral | _1DArrayr   ri   rB  r   rC  ri   r4   r   )
r2   rk  rQ  r   r   ri   r   ri   r4   zIterator[CompliantExprAny])r   r   r4   zlist[CompliantExprAny])r   r   rQ  r   r4   r   )r   r   r   r   rQ  r   r4   r   )r   zSequence[ExprNode]rQ  r   r4   r   )[
__future__r   enumr   r   typingr   r   r   r	   narwhals.dependenciesr
   narwhals.exceptionsr   r   r   collections.abcr   r   r   typing_extensionsr   r   narwhals._compliantr   r   narwhals._compliant.typingr   r   r   r   r   r#   r   r*   r   narwhals.typingr   r   r    r'   r+   r7   r>   rL   rN   rg   rs   r   r   rf   r   rY   r  r[   r  r\   r`   r   rZ   r   r]   r   r^   r   r_   r   __annotations__r  r  rn   r)  rS   r  rT   r+  ro   r   rQ   r  rR   r  r   r   rA  rM  rS  rZ  re  rh  rj  r6   r(   r&   <module>rw     s  
 #  4 4 3  <</B  #&DD!#!/!!	!
! 1!<I!(!*X
t X
v("D "6v+ v+ri iZ ,77LL,::,77l>>OO\--LL,''l//LL,''|??
T P 
 ,77,7755""L$K$K!!<#I#IMM<))|AAOO\--	I E 	H&HH H 	H
 HV	)	),/	)		) 	/  	
  
,2  	
   4$,4:O0#8r(   