a
    xˆcŠ%  ã                   @   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 u r˜t	 
|d|¡ d S t|jƒ}t |tttfƒs¶J ‚| 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   ÚappendÚSQLAlchemyAttributer'   r(   r*   Úapply_type_to_mapped_statement)r!   r"   r#   r$   r&   ÚstmtÚleft_hand_explicit_type© r7   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/sqlalchemy/ext/mypy/apply.pyÚapply_mypy_mapped_attr*   sL    

ÿþý
ü

ÿûÿ
ÿr9   )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|v 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u�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u st|tƒ�r^q|||jd j _	d}|du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 r7   )r&   ©Ú.0Úattrr7   r7   r8   Ú
<dictcomp>h   ó    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_metadatar5   Ú	left_nodeÚpython_type_for_typeZleft_node_proper_typer7   r7   r8   Ú re_apply_declarative_assignments_   sz    	ÿþýü
ÿ
ÿþ
üûúùÿø
öõôûÿ

ÿþ
ÿÿrG   )r"   r5   Úlvaluer6   rF   r%   c                 C   sb   |j }t|tƒsJ ‚|dur.|  t|g¡|_n"d|_|  t|du 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   rD   r    r1   Zis_inferred_defr   Zexpr_to_mapped_constructorr@   )r"   r5   rH   r6   rF   rE   r7   r7   r8   r4   °   s    ÿþr4   c              	   C   s&  t  | |¡}|du rdS t  |¡}d|jvrè|sèdd„ |D ƒ}|jdd… D ]@}d|jvr\qLt  ||¡}|du rrqL|D ]}| |j|j	¡ qvqLg }	| 
¡ D ]:\}
}|du r´ttjƒ}|	 tt|
|ƒ|t|ƒtd�¡ qšt|| d|	tƒ ƒ d	|jv�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 r7   )r&   r1   r:   r7   r7   r8   r=   õ   r>   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_formr2   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)   r7   r7   r8   Úadd_additional_orm_attributesæ   sF    


üÿ	ÿÿrV   )r"   r!   Úqualified_nameÚattrnamer%   c                 C   sr   |   |¡}|r,t|jtƒsJ ‚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   r   r   r   rR   r   rB   Z	_fullnamer*   r1   r   r
   rM   )r"   r!   rW   rX   ÚsymÚtype_Úvarr7   r7   r8   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    r3   r9   rG   r4   rV   ÚstrrS   r7   r7   r7   r8   Ú<module>   st   
û6üRú7ü6û