o
    wi2                     @   s~  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 ,d
Z-g dZ.dZ/e0e1Z2ej3e4d< de5de	e% fddZ6G dd dZ7G dd de%Z8G dd de9Z:e+dddZ;de'dede5ddfddZ<dS )     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>(((?!\.).)*$)))
createZfind_uniquedeleteupdateZ
find_firstZ	find_manyZupsertZupdate_manyZdelete_manycountzprisma-mypylogversionreturnc                 C   s   t S N)PrismaPlugin)r%    r)   B/home/app/Keep/.python/lib/python3.10/site-packages/prisma/mypy.pypluginE   s   r+   c                   @   s,   e Zd ZU dZeed< deddfddZdS )PrismaPluginConfig)warn_parsing_errorsr-   optionsr&   Nc                 C   sL   |j d u rd S t }||j  | jD ]}|jt|dd}t| || qd S )NT)fallback)config_filer	   read	__slots__
getbooleanCONFIGFILE_KEYsetattr)selfr.   Zplugin_configkeyZsettingr)   r)   r*   __init__M   s   

zPrismaPluginConfig.__init__)__name__
__module____qualname__r2   bool__annotations__r   r8   r)   r)   r)   r*   r,   I   s   
 r,   c                	       s  e Zd ZU eed< deddf fddZedede	e
egef  fdd	Zd
edefddZd
edefddZdedeeef defddZdeeeef dedeeef defddZded
ede	e fddZdedefddZdedefddZdedefdd Zdedefd!d"Zd#e defd$d%Z!d&ed'e"defd(d)Z#d*ededefd+d,Z$d-edeeef fd.d/Z%d#e&deeef fd0d1Z'd>d#e(d3edeeef fd4d5Z)d-edefd6d7Z*d-e defd8d9Z+de,defd:d;Z-dedefd<d=Z.  Z/S )?r(   configr.   r&   Nc                    s   t || _t | d S r'   )r,   r>   superr8   )r6   r.   	__class__r)   r*   r8   [   s   
zPrismaPlugin.__init__fullnamec                 C   s*   t |}|s	d S |dtv r| jS d S )Nname)CLIENT_ACTION_CHILDmatchgroupACTIONShandle_action_invocation)r6   rB   rE   r)   r)   r*   get_method_hook_   s   
zPrismaPlugin.get_method_hookctxc                 C   s
   |  |S r'   )_handle_include)r6   rJ   r)   r)   r*   rH   j   s   
z%PrismaPlugin.handle_action_invocationc              
   C   sX  |  d|}|du r|jS t|jts|jS | |j}|r%|jjd }n|j}| |}|r:tt|}|j	d }n|}t|tsD|jS z| 
|}| ||}W n5 ty }	 z)tdt|	j|	 | jjryt|	trp|	j}
n|}
t|j|
d |jW  Y d}	~	S d}	~	ww |rtt|}| ||}n|}|r|jj\}}}|jj|||gdS |S )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_namedZdefault_return_type
isinstancer   is_coroutine_typerN   is_optional_typer   r   itemsparse_expression_to_dictmodify_model_from_include	Exceptionr$   debugtyper9   r>   r-   UnparsedExpressioncontexterror_unable_to_parseapicopy_modified_optional_typeZcopy_modified)r6   rJ   Zinclude_exprZis_coroutineZ
actual_retZis_optionalZ
model_typerL   Z	new_modelexcZerr_ctxZmodified_retZarg1Zarg2_r)   r)   r*   rK   o   sZ   





zPrismaPlugin._handle_includemodeldatac                 C   s@   |j j }|j j D ]\}}| |||||< q| ||S r'   )rX   namescopyrS   maybe_modify_included_fieldcopy_modified_instance)r6   r`   ra   rb   r7   noder)   r)   r*   rU      s   z&PrismaPlugin.modify_model_from_includer7   rf   c                 C   s.  | |}|du s|d u r|S t|ttfrt|t|jtr>|jjd u s>t|jjtr>| 	|jjr>| 
|jjjd sFtd| |S | }t|j|_t|jtsYJ |jjjd |j_t|trd|v rt|jjtrt|jjjd tr| |jjjd |d }|g|jjjR |jj_|S )NFr   z Not modifying included field: %srL   )getrP   r   r   rY   rf   r   rX   r   is_optional_union_typeis_list_typerS   r$   rW   rc   dictr   rN   rU   )r6   r7   rf   ra   valuenewr`   r)   r)   r*   rd      sB   

z(PrismaPlugin.maybe_modify_included_fieldrC   c                 C   s   t |jD ]\}}t |D ]\}}||kr |j| |     S qqt |jD ]\}}||kr=|j| r=|j| d   S q'dS )z&Return the expression for an argument.r   N)	enumerate	arg_namesrN   Zcallee_arg_names)r6   rC   rJ   irb   jarg_namer)   r)   r*   rO      s   zPrismaPlugin.get_arg_namedtypc                 C   s   t |to	| |S r'   )rP   r   rh   r6   rr   r)   r)   r*   rR      s   zPrismaPlugin.is_optional_typec                 C   s   t |jdkot|jd tS )NrM      )lenrS   rP   r   rs   r)   r)   r*   rh     s   z#PrismaPlugin.is_optional_union_typec                 C   s   t |jjdkS )Nztyping.Coroutine)r<   rX   rB   rs   r)   r)   r*   rQ     s   zPrismaPlugin.is_coroutine_typec                 C   s   t |to
|jjdkS )Nzbuiltins.list)rP   r   rX   rB   rs   r)   r)   r*   ri   	  s   zPrismaPlugin.is_list_typeexprc                 C   s@   t |jdkpt t|jto|jjo|jjd jj dS )Nzbuiltins.dictr   rj   )	r<   rB   rP   rf   r   basesrX   lowerendswith)r6   rv   r)   r)   r*   is_dict_call_type  s   zPrismaPlugin.is_dict_call_typeinstancerb   c                 C   sP   t  |}t||jj|jj|_|jg|jj|j_|jj|j_|jj|j_|S r'   )rc   r   rX   Zdefnmodule_namemrorw   Zmetaclass_type)r6   r{   rb   rl   r)   r)   r*   re     s   
z#PrismaPlugin.copy_modified_instanceoriginalc                 C   s$   t  |}|j  |_||jd< |S )Nr   )rc   rS   )r6   r~   rr   rl   r)   r)   r*   r]     s   

z(PrismaPlugin.copy_modified_optional_type
expressionc                 C   s>   t |tr
| |S t |tr| |S tdt|j d)Nz Cannot parse expression of type=z to a dictionary.)rP   r   _dictexpr_to_dictr   _callexpr_to_dict	TypeErrorrX   r9   r6   r   r)   r)   r*   rT   %  s
   



z%PrismaPlugin.parse_expression_to_dictc                 C   s>   i }|j D ]\}}|d u rq| |}| |}|||< q|S r'   )rS   _resolve_expression)r6   rv   parsedZkey_expr
value_exprr7   rk   r)   r)   r*   r   .  s   


zPrismaPlugin._dictexpr_to_dictTstrictc                 C   s   t |jtstdt|j d|r#| |js#td|jj di }t|j|j	D ]\}}|d u r5q,| 
|}|||< q,|S )Nz2Expected CallExpr.callee to be a NameExpr but got z	 instead.z,Expected builtins.dict to be called but got z instead)rP   Zcalleer   r   rX   rz   rB   ziprn   rN   r   )r6   rv   r   r   rq   r   rk   r)   r)   r*   r   ;  s   

zPrismaPlugin._callexpr_to_dictc                 C   sV   t |tttfr|jS t |tr| |S t |tr| |S t |t	r)| 
|S |S r'   )rP   r   r   r   rk   r   _resolve_name_expressionr   r   r   r   r   r)   r)   r*   r   L  s   





z PrismaPlugin._resolve_expressionc                 C   s   t |jtr| |jS |S r'   )rP   rf   r   _resolve_var_noder   r)   r)   r*   r   [  s   z%PrismaPlugin._resolve_name_expressionc                 C   s(   |j r|jS |jdr| |jS |S )Nz	builtins.)is_finalZfinal_valuerB   
startswith_resolve_builtin)r6   rf   r)   r)   r*   r   a  s
   zPrismaPlugin._resolve_var_nodec                 C   s   t j|ddd   tS )N.rt   )operator
attrgettersplitbuiltins)r6   rB   r)   r)   r*   r   j  s   zPrismaPlugin._resolve_builtin)T)0r9   r:   r;   r,   r=   r   r8   r
   strr   r   r   r   rI   rH   rK   r   r   r   rU   r   r   r   r   rd   rO   r<   rR   r   rh   rQ   ri   r   rz   r   re   r]   rT   r   r   r   r   r   r   r   r   r   __classcell__r)   r)   r@   r*   r(   X   s@   
 "
L

-	 	r(   c                       s@   e Zd ZU eeef ed< deeef ddf fddZ  ZS )rY   rZ   r&   Nc                    s$   || _ t dt|j d d S )NzTried to access a (z!) expression that was not parsed.)rZ   r?   r8   rX   r9   )r6   rZ   r@   r)   r*   r8   q  s   zUnparsedExpression.__init__)	r9   r:   r;   r   r   r   r=   r8   r   r)   r)   r@   r*   rY   n  s   
 &rY   zprisma-parsingzUnable to parseZPrismar\   rZ   detailc                 C   s4   d}d| d}|d| d7 }| j ||td d S )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)ZfailERROR_PARSING)r\   rZ   r   linkZfull_messager)   r)   r*   r[   y  s   r[   )=rerc   loggingr   r   typingr   r   r   Z
TypingTyper   r   r   r   configparserr	   Ztyping_extensionsr
   Z
mypy.nodesr   r   r   r   r   r   r   r   r   r   r   r   r   Z
mypy.typesr   r   r   Zmypy.pluginr   r   r   Zmypy.optionsr   Zmypy.errorcodesr   compilerD   rG   r4   	getLoggerr9   r$   Loggerr=   r   r+   r,   r(   rV   rY   r   r[   r)   r)   r)   r*   <module>   s2   
 $	<
	  