a
    •˜ëj_)  ã                   @  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$ dd l%m&Z& dd!l%m'Z' d"d#d$d%d&d'œd(d)„Z(d"d#d%d&d*œd+d,„Z)d#d-d.d/d/d&d0œd1d2„Z*d"d#d%d&d*œd3d4„Z+d#d"d5d5d&d6œd7d8„Z,d9S ):é    )Úannotations)Ú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)Úexpr_to_mapped_constructor)ÚNAMED_TYPE_SQLA_MAPPEDr
   r   zUnion[NameExpr, StrExpr]zList[util.SQLAlchemyAttribute]ÚNone)Ú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Útyper   r   r   r   ÚappendZSQLAlchemyAttributer*   r+   r-   Úapply_type_to_mapped_statement)r$   r%   r&   r'   r)   ÚstmtÚleft_hand_explicit_type© r8   úX/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/sqlalchemy/ext/mypy/apply.pyÚapply_mypy_mapped_attr-   sL    

ÿþý
ü

ÿûÿ
ÿr:   )r$   r%   r'   r(   c           
   	   C  sÌ  dd„ |D ƒ}d}| j jD �]–}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ƒ�rxt|tƒ�sxt|jtƒ�rxt|jjtƒ�rxt|jjjtƒ�rx|jjjjdu�rx|jjjjjtk�rx|jjjdk�rxt|jjd tƒ�rxt|jjd jtƒ�rxt |||||jjd j¡}	|	du�rxt|	tƒ�sx|	}|||jd j _	d}t|j	tƒ�r”|j	j	jtkr|du�s¢J ‚| 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>k   ó    z4re_apply_declarative_assignments.<locals>.<dictcomp>Fr   NZ_empty_constructorT)r0   r1   r.   r   r2   r   r)   Únoder   r3   r   r   Úrvaluer	   Zcalleer   ÚexprÚfullnamer"   Úargsr   r   Z#infer_type_from_right_hand_nameexprr   Ú
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_typeZnew_python_type_for_typer8   r8   r9   Ú re_apply_declarative_assignmentsb   s~    	ÿþýü
ÿ
ÿþ
üûúùÿø
öõôûÿ
ÿÿ
ÿþÿrH   r   r   zOptional[ProperType])r%   r6   Úlvaluer7   rG   r(   c                 C  s†   |j }t|tƒsJ ‚|dur4d|_|  t|g¡|_n*d|_|  t|du rTttj	ƒgn|g¡|_t
|jƒ|_|jdur‚|dur‚||_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::

    ... format: off

        class User(Base):
            # ...

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

    ... format: on

    NF)r@   r.   r   Zis_inferred_defrE   r"   r3   r   r   Úspecial_formr!   rA   )r%   r6   rI   r7   rG   rF   r8   r8   r9   r5   ·   s"    ÿÿûr5   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 r8   )r)   r3   r;   r8   r8   r9   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)   r3   Úitemsr   r   rJ   r4   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ÿ   sF    


üÿ	ÿÿrW   Ústr)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   rJ   r   rC   Z	_fullnamer-   r3   r   r   rO   )r%   r$   rY   rZ   ÚsymÚtype_Úvarr8   r8   r9   rT   4  s    

rT   N)-Ú
__future__r   Ú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    rO   r!   r"   r:   rH   r5   rW   rT   r8   r8   r8   r9   Ú<module>   sH   5UH5