U
    ÌZjŠ%  ã                   @   sú  d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d d	lm
Z
 d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ ddl m!Z! ddl m"Z" ddl#m$Z$ e	eeeef ee"j% d d!œd"d#„Z&e	eee"j% d d$œd%d&„Z'eeeee ee d d'œd(d)„Z(e	eee"j% d d$œd*d+„Z)ee	e*e*d d,œd-d.„Z+d S )/é    )ÚList)ÚOptional)ÚUnion)ÚARG_NAMED_OPT)ÚArgument)ÚAssignmentStmt)ÚCallExpr)ÚClassDef)ÚMDEF)Ú
MemberExpr)ÚNameExpr)ÚRefExpr)ÚStrExpr)ÚSymbolTableNode)ÚTempNode)ÚTypeInfo)ÚVar)ÚSemanticAnalyzerPluginInterface)Úadd_method_to_class)ÚAnyType)Úget_proper_type)ÚInstance)ÚNoneTyp)Ú
ProperType)Ú	TypeOfAny)ÚUnboundType)Ú	UnionTypeé   )Úinfer)Úutil)ÚNAMED_TYPE_SQLA_MAPPEDN)ÚclsÚapiÚitemÚ
attributesÚreturnc              	   C   sð   t |tƒr|j}nt |tƒr$|j}nd S | jjD ]2}t |tƒr0t |jd tƒr0|jd j|kr0 q|q0t	 
|d |¡| ¡ d S |jd kr˜t	 
|d|¡ d S t|jƒ}t |tttfƒs¶t‚| t	j||j|j|| jd�¡ t|||jd |d ƒ d S )Nr   zCan't find mapped attribute {}zBStatement linked from _mypy_mapped_attrs has no typing information)ÚnameÚlineÚcolumnÚtypÚinfo)Ú
isinstancer   r&   r   ÚvalueÚdefsÚbodyr   Úlvaluesr   ZfailÚformatÚtyper   r   r   r   ÚAssertionErrorÚappendÚSQLAlchemyAttributer'   r(   r*   Úapply_type_to_mapped_statement)r!   r"   r#   r$   r&   ÚstmtÚleft_hand_explicit_type© r8   ú\/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/sqlalchemy/ext/mypy/apply.pyÚapply_mypy_mapped_attr*   sV    

ÿþý
ü
 ÿûÿ
    ÿr:   )r!   r"   r$   r%   c           	   	   C   s¦  dd„ |D ƒ}d}| j jD �]p}t|tƒrt|jd tƒr|jd j|krt|jd jtƒr|jd j}||jd j j	}t
|j	ƒ}t|tƒ�rtt|tƒ�stt|jtƒ�rtt|jjtƒ�rtt|jjjtƒ�rt|jjjjdk	�rt|jjjjjtk�rt|jjjdk�rtt|jjd tƒ�rtt|jjd jtƒ�rtt |||||jjd j¡}|dkst|tƒ�r^q|||jd j _	d}|dk	r| t|g¡|_	q|�r¢t | j|¡ dS )zlFor multiple class passes, re-apply our left-hand side types as mypy
    seems to reset them in place.

    c                 S   s   i | ]}|j |“qS r8   )r&   ©Ú.0Úattrr8   r8   r9   Ú
<dictcomp>h   s      z4re_apply_declarative_assignments.<locals>.<dictcomp>Fr   NZ_empty_constructorT)r-   r.   r+   r   r/   r   r&   Únoder   r1   r   r   Úrvaluer   Zcalleer   ÚexprÚfullnamer    Úargsr   r   Z#infer_type_from_right_hand_nameexprÚ
named_typer   Zset_mapped_attributesr*   )	r!   r"   r$   Zmapped_attr_lookupZupdate_cls_metadatar6   Ú	left_nodeÚpython_type_for_typeZleft_node_proper_typer8   r8   r9   Ú re_apply_declarative_assignments_   s~    	ÿþýü
ÿ
ÿþ
üûúùÿø
öõôûÿ

 ÿþ
ÿ ÿrG   )r"   r6   Úlvaluer7   rF   r%   c                 C   sb   |j }t|tƒst‚|dk	r.|  t|g¡|_n"d|_|  t|dkrFg n|g¡|_t 	|j
¡|_
dS )aµ  Apply the Mapped[<type>] annotation and right hand object to a
    declarative assignment statement.

    This converts a Python declarative class statement such as::

        class User(Base):
            # ...

            attrname = Column(Integer)

    To one that describes the final Python behavior to Mypy::

        class User(Base):
            # ...

            attrname : Mapped[Optional[int]] = <meaningless temp node>

    NF)r?   r+   r   r2   rD   r    r1   Zis_inferred_defr   Zexpr_to_mapped_constructorr@   )r"   r6   rH   r7   rF   rE   r8   r8   r9   r5   °   s     ÿþr5   c              	   C   s&  t  | |¡}|dkrdS t  |¡}d|jkrè|sèdd„ |D ƒ}|jdd… D ]@}d|jkr\qLt  ||¡}|dkrrqL|D ]}| |j|j	¡ qvqLg }	| 
¡ D ]:\}
}|dkr´ttjƒ}|	 tt|
|ƒ|t|ƒtd�¡ qšt|| d|	tƒ ƒ d	|jk�rt  |¡�rt|| d
d	ƒ |�s"t|| ddƒ dS )zCApply __init__, __table__ and other attributes to the mapped class.NÚ__init__c                 S   s   i | ]}|j |j“qS r8   )r&   r1   r;   r8   r8   r9   r>   õ   s      z1add_additional_orm_attributes.<locals>.<dictcomp>r   éÿÿÿÿZ
sqlalchemy)ÚvariableZtype_annotationZinitializerÚkindZ	__table__zsqlalchemy.sql.schema.Tablezsqlalchemy.orm.mapper.MapperZ
__mapper__)r   Zinfo_for_clsZget_is_baseÚnamesÚmroÚmetadataZget_mapped_attributesÚ
setdefaultr&   r1   Úitemsr   r   Úspecial_formr3   r   r   r   r   r   r   Zget_has_tableÚ _apply_placeholder_attr_to_class)r!   r"   r$   r*   Zis_baseZmapped_attr_namesÚbaseZbase_cls_attributesr=   Ú	argumentsr&   r)   r8   r8   r9   Úadd_additional_orm_attributesæ   sR    


üÿ	   ÿ   ÿrV   )r"   r!   Úqualified_nameÚattrnamer%   c                 C   sr   |   |¡}|r,t|jtƒst‚t|jg ƒ}n
ttjƒ}t	|ƒ}|j
d | |_|j|_||_tt|ƒ|jj|< d S )NÚ.)Zlookup_fully_qualified_or_noner+   r?   r   r2   r   r   r   rR   r   rB   Z	_fullnamer*   r1   r   r
   rM   )r"   r!   rW   rX   ÚsymÚtype_Úvarr8   r8   r9   rS     s    

rS   ),Útypingr   r   r   Z
mypy.nodesr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   Zmypy.pluginr   Zmypy.plugins.commonr   Z
mypy.typesr   r   r   r   r   r   r   r   Ú r   r   rM   r    r4   r:   rG   r5   rV   ÚstrrS   r8   r8   r8   r9   Ú<module>   st   
û6üRú7ü6û