o
    Šåg_)  ã                   @  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' dAd,d-„Z(dBd.d/„Z)dCd7d8„Z*dBd9d:„Z+dDd>d?„Z,d@S )Eé    )Ú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_MAPPEDÚclsr
   Úapir   ÚitemúUnion[NameExpr, StrExpr]Ú
attributesúList[util.SQLAlchemyAttribute]ÚreturnÚNonec              	   C  sð   t |tƒr	|j}nt |tƒr|j}nd S | jjD ]}t |tƒr1t |jd tƒr1|jd j|kr1 nqt	 
|d|› �| ¡ d S |jd u rLt	 
|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    ÚfailÚtyper   r   r   r   ÚappendÚSQLAlchemyAttributer,   r-   r/   Úapply_type_to_mapped_statement)r#   r$   r%   r'   r+   ÚstmtÚleft_hand_explicit_type© r<   úU/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/ext/mypy/apply.pyÚapply_mypy_mapped_attr-   sL   

ÿþ€
ü

ÿûÿ
ÿ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ƒr¯t|tƒs¯t|jtƒr¯t|jjtƒr¯t|jjjtƒr¯|jjjjdur¯|jjjjjtkr¯|jjjdkr¯t|jjd tƒr¯t|jjd jtƒr¯t |||||jjd j¡}	|	dur¯t|	tƒs¯|	}|||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 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 r<   )r+   ©Ú.0Úattrr<   r<   r=   Ú
<dictcomp>k   s    z4re_apply_declarative_assignments.<locals>.<dictcomp>Fr   NÚ_empty_constructorT)r2   r3   r0   r   r4   r   r+   Únoder   r6   r   r   Úrvaluer	   Úcalleer   ÚexprÚfullnamer"   Úargsr   r   Ú#infer_type_from_right_hand_nameexprr   Ú
named_typer    Úset_mapped_attributesr/   )
r#   r$   r'   Úmapped_attr_lookupÚupdate_cls_metadatar:   Ú	left_nodeÚpython_type_for_typeÚleft_node_proper_typeÚnew_python_type_for_typer<   r<   r=   Ú re_apply_declarative_assignmentsb   s|   	ÿþÿ
ÿþ
ÿþ
üûúÿÿþûÿ

ÿÿ
ÿÿ€ÿrS   r:   r   Úlvaluer   r;   úOptional[ProperType]rP   c                 C  sŽ   |j }t|tƒs
J ‚|durd|_|  t|g¡|_nd|_|  t|du r*ttj	ƒgn|g¡|_t
|jƒ|_|jdurC|durE||_dS dS 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)rD   r0   r   Úis_inferred_defrK   r"   r6   r   r   Úspecial_formr!   rE   )r$   r:   rT   r;   rP   rO   r<   r<   r=   r9   ·   s$   ÿÿû
ÿr9   c              	   C  s$  t  | |¡}|du rdS t  |¡}d|jvrt|stdd„ |D ƒ}|jdd… D ] }d|jvr.q&t  ||¡}|du r9q&|D ]
}| |j|j	¡ q;q&g }	| 
¡ D ]\}
}|du rZttjƒ}|	 tt|
|ƒ|t|ƒtd�¡ qMt|| d|	tƒ ƒ d	|jvr…t  |¡r…t|| d
d	ƒ |s�t|| ddƒ dS dS )zCApply __init__, __table__ and other attributes to the mapped class.NÚ__init__c                 S  s   i | ]}|j |j“qS r<   )r+   r6   r?   r<   r<   r=   rB     s    z1add_additional_orm_attributes.<locals>.<dictcomp>r   éÿÿÿÿÚ
sqlalchemy)ÚvariableÚtype_annotationÚinitializerÚkindÚ	__table__zsqlalchemy.sql.schema.Tablezsqlalchemy.orm.mapper.MapperÚ
__mapper__)r    Úinfo_for_clsÚget_is_baseÚnamesÚmroÚmetadataÚget_mapped_attributesÚ
setdefaultr+   r6   Úitemsr   r   rW   r7   r   r   r   r   r   r   Úget_has_tableÚ _apply_placeholder_attr_to_class)r#   r$   r'   r/   Úis_baseÚmapped_attr_namesÚbaseÚbase_cls_attributesrA   Ú	argumentsr+   r.   r<   r<   r=   Úadd_additional_orm_attributesÿ   sJ   

ÿ
üÿ	ÿÿÿrp   Úqualified_nameÚstrÚattrnamec                 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Ú.)Úlookup_fully_qualified_or_noner0   rD   r   r   r   r   rW   r   rH   Ú	_fullnamer/   r6   r   r   rc   )r$   r#   rq   rs   ÚsymÚtype_Úvarr<   r<   r=   rj   4  s   

rj   N)
r#   r
   r$   r   r%   r&   r'   r(   r)   r*   )r#   r
   r$   r   r'   r(   r)   r*   )r$   r   r:   r   rT   r   r;   rU   rP   rU   r)   r*   )
r$   r   r#   r
   rq   rr   rs   rr   r)   r*   )-Ú
__future__r   Útypingr   r   r   Ú
mypy.nodesr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   Úmypy.pluginr   Úmypy.plugins.commonr   Ú
mypy.typesr   r   r   r   r   r   r   r   Ú r   r    rc   r!   r"   r>   rS   r9   rp   rj   r<   r<   r<   r=   Ú<module>   sJ   

5
U
H5