
    ,i2                        U d dl Z d dlZd dlZd dlZd dlZd dlmZmZ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mZmZmZmZmZmZmZmZmZmZ d dl mZm!Z!m"Z"m#Z# d dl$m%Z%m&Z&m'Z' d dl(m)Z) d d	l*m+Z+  e jX                  d
      Z-g dZ.dZ/ ej`                  e1      Z2ejf                  e4d<   de5de	e%   fdZ6 G d d      Z7 G d de%      Z8 G d de9      Z: e+ddd      Z;de'dede5ddfdZ<y)    N)AnyDictTypeUnionCallableOptionalcast)ConfigParser)override)VarNodeContextIntExprStrExprCallExprDictExprNameExprTypeInfo	BytesExpr
ExpressionSymbolTableSymbolTableNode)r   InstanceNoneType	UnionType)PluginMethodContextCheckerPluginInterface)Options)	ErrorCodez5prisma\.actions\.(.*)Actions\.(?P<name>(((?!\.).)*$)))
createfind_uniquedeleteupdate
find_first	find_manyupsertupdate_manydelete_manycountzprisma-mypylogversionreturnc                     t         S N)PrismaPlugin)r,   s    P/var/www/html/langgraph-service/venv/lib/python3.12/site-packages/prisma/mypy.pypluginr2   E   s        c                   ,    e Zd ZU dZeed<   deddfdZy)PrismaPluginConfig)warn_parsing_errorsr6   optionsr-   Nc                     |j                   y t               }|j                  |j                          | j                  D ]'  }|j	                  t
        |d      }t        | ||       ) y )NT)fallback)config_filer
   read	__slots__
getbooleanCONFIGFILE_KEYsetattr)selfr7   plugin_configkeysettings        r1   __init__zPrismaPluginConfig.__init__M   sb    &$7../>> 	(C#..~sT.RGD#w'	(r3   )__name__
__module____qualname__r<   bool__annotations__r   rD    r3   r1   r5   r5   I   s     (I( (D (r3   r5   c            	           e Zd ZU eed<   deddf fdZedede	e
egef      fd       Zdedefd	Zdedefd
Zdedeeef   defdZdeeeef   dedeeef   defdZdedede	e   fdZdedefdZdedefdZdedefdZdedefdZde defdZ!dede"defdZ#dededefdZ$dedeeef   fd Z%de&deeef   fd!Z'd(de(d"edeeef   fd#Z)dedefd$Z*de defd%Z+de,defd&Z-dedefd'Z. xZ/S ))r0   configr7   r-   Nc                 D    t        |      | _        t        |   |       y r/   )r5   rL   superrD   )r@   r7   	__class__s     r1   rD   zPrismaPlugin.__init__[   s    (1!r3   fullnamec                 z    t         j                  |      }|sy |j                  d      t        v r| j                  S y )Nname)CLIENT_ACTION_CHILDmatchgroupACTIONShandle_action_invocation)r@   rP   rT   s      r1   get_method_hookzPrismaPlugin.get_method_hook_   s8    #))(3;;v')000r3   ctxc                 $    | j                  |      S r/   )_handle_include)r@   rY   s     r1   rW   z%PrismaPlugin.handle_action_invocationj   s     ##C((r3   c                    | j                  d|      }||j                  S t        |j                  t              s|j                  S | j	                  |j                        }|r|j                  j
                  d   }n|j                  }| j                  |      }|r t        t        |      }|j                  d   }n|}t        |t              s|j                  S 	 | j                  |      }| j                  ||      }|r#t        t        |      }| j/                  ||      }n|}|r9|j                  j
                  \  }}}|j                  j1                  |||g      S |S # t        $ r}	t        j                  dt        |	      j                   |	       | j"                  j$                  r6t        |	t&              r|	j(                  }
n|}
t+        |j,                  |
d       |j                  cY d}	~	S d}	~	ww xY w)a,  Recursively remove Optional from a relational field of a model
        if it was explicitly included.

        An argument could be made that this is over-engineered
        and while I do agree to an extent, the benefit of this
        method over just setting the default value to an empty list
        is that access to a relational field without explicitly
        including it will raise an error when type checking, e.g

        user = await client.user.find_unique(where={'id': user_id})
        print('
'.join(p.title for p in user.posts))
        includeN   r   z/Ignoring %s exception while parsing include: %szthe "include" argument)args)get_arg_nameddefault_return_type
isinstancer   is_coroutine_typer_   is_optional_typer	   r   itemsparse_expression_to_dictmodify_model_from_include	Exceptionr+   debugtyperE   rL   r6   UnparsedExpressioncontexterror_unable_to_parseapicopy_modified_optional_typecopy_modified)r@   rY   include_expris_coroutine
actual_retis_optional
model_typer]   	new_modelexcerr_ctxmodified_retarg1arg2_s                  r1   r[   zPrismaPlugin._handle_includeo   s    )))S9***#118<***--c.E.EF0055a8J00J++J7i4J#))!,J#J*h/***	+33LAG66z7KI2 i4J;;J	RL$L3388MD$**88tT<>X8YYE  	+IIAS	"" {{.. c#56!kkG*G%GG, ***-	+s   #E# #	G8,BG3-G83G8modeldatac                     |j                   j                  j                         }|j                   j                  j                         D ]  \  }}| j	                  |||      ||<    | j                  ||      S r/   )rj   namescopyre   maybe_modify_included_fieldcopy_modified_instance)r@   r}   r~   r   rB   nodes         r1   rg   z&PrismaPlugin.modify_model_from_include   so    

  %%'))//1 	KIC99#tTJE#J	K **5%88r3   rB   r   c                    |j                  |      }|du s||S t        |t        t        f      rt	        |      t        |j
                  t              r|j
                  j                  {t        |j
                  j                  t              rW| j                  |j
                  j                        r2| j                  |j
                  j                  j                  d         st        j                  d|       |S |j                         }t        j                  |j
                        |_        t        |j
                  t              sJ |j
                  j                  j                  d   |j
                  _        t        |t              rd|v rt        |j
                  j                  t               rt        |j
                  j                  j"                  d   t               rs| j%                  |j
                  j                  j"                  d   |d         }|g|j
                  j                  j"                  |j
                  j                  _        |S )NFr   z Not modifying included field: %sr]   )getrb   r   r   rk   r   r   rj   r   is_optional_union_typeis_list_typere   r+   ri   r   dictr   r_   rg   )r@   rB   r   r~   valuenewr}   s          r1   r   z(PrismaPlugin.maybe_modify_included_field   s    E>U]Kej$/0$U++
 499c*yy~~%diinni8..tyy~~>$$TYY^^%9%9!%<=II2 K iik99SXX&#((C(((		,,Q/ ud#U"388==(3388==--a0(;22388==3E3Ea3H%PYJZ[E"'!=#((--*<*<!=CHHMM
r3   rR   c                 &   t        |j                        D ]3  \  }}t        |      D ]   \  }}||k(  s|j                  |   |   c c S  5 t        |j                        D ].  \  }}||k(  s|j                  |   s|j                  |   d   c S  y)z&Return the expression for an argument.r   N)	enumerate	arg_namesr_   callee_arg_names)r@   rR   rY   ir   jarg_names          r1   r`   zPrismaPlugin.get_arg_named   s     "#--0 	*HAu(/ *8t#88A;q>)*	* %S%9%9: 	&KAx4CHHQKxx{1~%	& r3   typc                 H    t        |t              xr | j                  |      S r/   )rb   r   r   r@   r   s     r1   rd   zPrismaPlugin.is_optional_type   s    #y)Nd.I.I#.NNr3   c                 p    t        |j                        dk(  xr t        |j                  d   t              S )Nr^      )lenre   rb   r   r   s     r1   r   z#PrismaPlugin.is_optional_union_type  s)    399~"Iz#))A,'IIr3   c                 F    t        |j                  j                  dk(        S )Nztyping.Coroutine)rH   rj   rP   r   s     r1   rc   zPrismaPlugin.is_coroutine_type  s    CHH%%);;<<r3   c                 X    t        |t              xr |j                  j                  dk(  S )Nzbuiltins.list)rb   r   rj   rP   r   s     r1   r   zPrismaPlugin.is_list_type	  s#    #x(QSXX->->/-QQr3   exprc                 D   t        |j                  dk(        xs t        t        |j                  t              xrb |j                  j
                  xrJ |j                  j
                  d   j                  j                  j                         j                  d            S )Nzbuiltins.dictr   r   )	rH   rP   rb   r   r   basesrj   lowerendswith)r@   r   s     r1   is_dict_call_typezPrismaPlugin.is_dict_call_type  s{    
 DMM_45 
tyy(+ J		J		"''00668AA&I:
 	
r3   instancer   c                    t        j                   |      }t        ||j                  j                  |j                  j                        |_        |j                  g|j                  j
                  |j                  _        |j                  j                  |j                  _        |j                  j                  |j                  _        |S r/   )r   r   rj   defnmodule_namemror   metaclass_type)r@   r   r   r   s       r1   r   z#PrismaPlugin.copy_modified_instance  s    ii!E388==#((2F2FG58==#4#45!,,"*--">">
r3   originalc                     t        j                   |      }|j                  j                         |_        ||j                  d<   |S )Nr   )r   re   )r@   r   r   r   s       r1   ro   z(PrismaPlugin.copy_modified_optional_type  s4    ii!IINN$			!
r3   
expressionc                     t        |t              r| j                  |      S t        |t              r| j	                  |      S t        dt        |      j                   d      )Nz Cannot parse expression of type=z to a dictionary.)rb   r   _dictexpr_to_dictr   _callexpr_to_dict	TypeErrorrj   rE   r@   r   s     r1   rf   z%PrismaPlugin.parse_expression_to_dict%  sZ    j(+))*55j(+))*55:4
;K;T;T:UUfghhr3   c                     i }|j                   D ]/  \  }}|	| j                  |      }| j                  |      }|||<   1 |S r/   )re   _resolve_expression)r@   r   parsedkey_expr
value_exprrB   r   s          r1   r   zPrismaPlugin._dictexpr_to_dict.  sX    $(JJ 	  Hj**84C,,Z8EF3K	  r3   strictc                    t        |j                  t              s"t        dt	        |j                         d      |r>| j                  |j                        s#t        d|j                  j                   d      i }t        |j                  |j                        D ]  \  }}|	| j                  |      }|||<     |S )Nz2Expected CallExpr.callee to be a NameExpr but got z	 instead.z,Expected builtins.dict to be called but got z instead)rb   calleer   r   rj   r   rP   zipr   r_   r   )r@   r   r   r   r   r   r   s          r1   r   zPrismaPlugin._callexpr_to_dict;  s    $++x0PQUVZVaVaQbPcclmnn$00=J4;;K_K_J``hijj$'		$B 	% Hj,,Z8E$F8	% r3   c                    t        |t        t        t        f      r|j                  S t        |t
              r| j                  |      S t        |t              r| j                  |      S t        |t              r| j                  |      S |S r/   )rb   r   r   r   r   r   _resolve_name_expressionr   r   r   r   r   s     r1   r   z PrismaPlugin._resolve_expressionL  sv    j7Iw"?@###j(+00<<j(+))*55j(+))*55r3   c                 p    t        |j                  t              r| j                  |j                        S |S r/   )rb   r   r   _resolve_var_noder   s     r1   r   z%PrismaPlugin._resolve_name_expression[  s+    joos+))*//::r3   c                     |j                   r|j                  S |j                  j                  d      r| j	                  |j                        S |S )Nz	builtins.)is_finalfinal_valuerP   
startswith_resolve_builtin)r@   r   s     r1   r   zPrismaPlugin._resolve_var_nodea  sA    ==###==##K0((77r3   c                 ^     t        j                  |j                  d      dd   t              S )N.r   )operator
attrgettersplitbuiltins)r@   rP   s     r1   r   zPrismaPlugin._resolve_builtinj  s*    <x""HNN3$7$;<XFFr3   )T)0rE   rF   rG   r5   rI   r   rD   r   strr   r   r   r   rX   rW   r[   r   r   r   rg   r   r   r   r   r   r`   rH   rd   r   r   rc   r   r   r   r   r   ro   rf   r   r   r   r   r   r   r   r   r   __classcell__rO   s   @r1   r0   r0   X   sD   " "D "  =/SWBW9X0Y  )M )d )
J= JT JX9x 9tCH~ 9RZ 9+3
D()+ + 38n	+
 
+Z# M hz>R OD OT OJ) J J
=X =$ =R R R	
h 	
4 	
x  PX I D Y i: i$sCx. ih 4S> h  SRUX "j S 8  c c G G Gr3   r0   c                   H     e Zd ZU eeef   ed<   deeef   ddf fdZ xZS )rk   rl   r-   Nc                 `    || _         t        | 	  dt        |      j                   d       y )NzTried to access a (z!) expression that was not parsed.)rl   rN   rD   rj   rE   )r@   rl   rO   s     r1   rD   zUnparsedExpression.__init__q  s-    .tG}/E/E.FFghir3   )	rE   rF   rG   r   r   r   rI   rD   r   r   s   @r1   rk   rk   n  s9    :t#$$jj$&6 7 jD j jr3   rk   zprisma-parsingzUnable to parsePrismarn   rl   detailc                 V    d}d| d}|d| dz  }| j                  ||t               y )NzChttps://github.com/RobertCraigie/prisma-client-py/issues/new/choosez,The prisma mypy plugin was unable to parse: 
z&Please consider reporting this bug at z so we can try to fix it!)code)failERROR_PARSING)rn   rl   r   linkfull_messages        r1   rm   rm   y  s<    PDA&LL<TFB[\\LHH\7H7r3   )=rer   loggingr   r   typingr   r   r   
TypingTyper   r   r   r	   configparserr
   typing_extensionsr   
mypy.nodesr   r   r   r   r   r   r   r   r   r   r   r   r   
mypy.typesr   r   r   mypy.pluginr   r   r   mypy.optionsr   mypy.errorcodesr    compilerS   rV   r>   	getLoggerrE   r+   LoggerrI   r   r2   r5   r0   rh   rk   r   rm   rJ   r3   r1   <module>r      s   	       & &     F E   % !bjj!YZ  'g''1W^^ 1C Jv. ( (SG6 SGlj j *,=xH85 8 8QT 8Y] 8r3   