o
    Šågè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" 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( 	%dCdDd.d/„Z)dEd7d8„Z*dFd;d<„Z+dGd>d?„Z,dHd@dA„Z-dBS )Ié    )Úannotations)ÚList)ÚOptional)ÚUnion)ÚAssignmentStmt)ÚCallExpr)ÚClassDef)Ú	Decorator)Ú
LambdaExpr)ÚListExpr)Ú
MemberExpr)ÚNameExpr)ÚPlaceholderNode)ÚRefExpr)ÚStrExpr)Ú
SymbolNode)ÚSymbolTableNode)ÚTempNode)ÚTypeInfo)ÚVar)ÚSemanticAnalyzerPluginInterface)ÚAnyType)ÚCallableType)Úget_proper_type)ÚInstance)ÚNoneType)Ú
ProperType)ÚType)Ú	TypeOfAny)ÚUnboundType)Ú	UnionTypeé   )Úapply)Úinfer)Únames)ÚutilFÚclsr   Úapir   Úis_mixin_scanÚboolÚreturnú(Optional[List[util.SQLAlchemyAttribute]]c                 C  s   t  | |¡}|d u rd S | j d¡rd S t  ||¡}t  |¡ |d ur.|s,t | ||¡ |S g }| jj	sG|j
 ¡ D ]\}}t| ||||ƒ q9n#t  | jj	¡D ]}t|tƒr]t| |||ƒ qNt|tƒrit| |||ƒ qNt| |ƒ |sxt | ||¡ t  ||¡ |S )NÚbuiltins)r%   Úinfo_for_clsÚfullnameÚ
startswithÚget_mapped_attributesÚestablish_as_sqlalchemyr"   Ú re_apply_declarative_assignmentsÚdefsÚbodyr$   ÚitemsÚ_scan_symbol_table_entryÚflatten_typecheckingÚ
isinstancer   Ú!_scan_declarative_assignment_stmtr	   Ú _scan_declarative_decorator_stmtÚ_scan_for_mapped_basesÚadd_additional_orm_attributesÚset_mapped_attributes)r&   r'   r(   ÚinfoÚmapped_attributesÚsym_nameÚsymÚstmt© rC   úZ/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/ext/mypy/decl_class.pyÚ,scan_declarative_assignments_and_apply_types0   sD   
ÿ

ÿÿ
ÿ
ÿ€
rE   ÚnameÚstrÚvaluer   Ú
attributesúList[util.SQLAlchemyAttribute]ÚNonec              	   C  s|  t |jƒ}t|tƒsdS d}t |j¡}d}|tjtjtjtj	tj
tjhv r4|jr1t |jd ƒ}nWd}nT|tju rˆ|js?d}nIt |jd ƒ}	t|	tƒrN|	j}	t|	ttfƒrˆ| |	j|	¡}
|
durˆt|
jtƒrˆt |
jtj¡r|tt ||
jg ¡tƒ gƒ}nt |d |
jj¡|¡ |r�d}t || || j¡| ¡ ttjƒ}|dur¼|jdus¨J ‚|  tj!||jj"|jj#|| j$d�¡ dS dS )zaExtract mapping information from a SymbolTableNode that's in the
    type.names dictionary.

    NFr   Tú4Column type should be a TypeEngine subclass not '{}'zØCan't infer type from attribute {} on class {}. please specify a return type from this function that is one of: Mapped[<python type>], relationship[<target class>], Column[<TypeEngine>], MapperProperty[<python type>]©rF   ÚlineÚcolumnÚtypr>   )%r   Útyper8   r   r$   Útype_id_for_named_nodeÚMAPPEDÚRELATIONSHIPÚCOMPOSITE_PROPERTYÚMAPPER_PROPERTYÚSYNONYM_PROPERTYÚCOLUMN_PROPERTYÚargsÚCOLUMNr   r   Úlookup_qualifiedrF   ÚnodeÚhas_base_type_idÚ
TYPEENGINEr    r#   Ú#extract_python_type_from_typeenginer   r%   ÚfailÚformatr.   r   r   Úspecial_formÚappendÚSQLAlchemyAttributerN   rO   r>   )r&   r'   rF   rH   rI   Ú
value_typeÚleft_hand_explicit_typeÚtype_idÚerrÚtypeengine_argrA   ÚmsgrC   rC   rD   r6   r   sv   

ú
ÿ
ÿüÿ	
üÿ
ûÿþr6   rB   r	   c              	   C  s`  |j D ]}t|tttfƒrt |¡tju r nqdS | jj	 
|¡}d}t |j¡rMttjƒ}t|jjƒ}|j|_t|gt|ƒƒ}	|jj|	_|	| jj	|< dS t|jjtƒrÈ|jjj}
t|
tƒrft |
| |¡}ndS |tjtjtjtjtjtj hv rƒ|
j!rƒt"|
j!d ƒ}nE|tj#u rÈ|
j!rÈ|
j!d }t|tƒrÈ| $|j|¡}|durÈt|jt%ƒrÈt &|jtj'¡r¼t(t) *||jg ¡t+ƒ gƒ}nt ,|d -|jj.¡|
¡ |du rßd}t ,|| -|jj¡|¡ ttjƒ}t|jjƒ}|j|_t|tƒröt"t /||¡ƒ}| 0tj1|g¡|j_t 2t3|jj4|jj	ƒ¡}t|g|ƒ}	|jj|	_| 5tj6|j|j7|j8|| j9d�¡ |	| jj	|< dS )a°  Extract mapping information from a @declared_attr in a declarative
    class.

    E.g.::

        @reg.mapped
        class MyClass:
            # ...

            @declared_attr
            def updated_at(cls) -> Column[DateTime]:
                return Column(DateTime)

    Will resolve in mypy as::

        @reg.mapped
        class MyClass:
            # ...

            updated_at: Mapped[Optional[datetime.datetime]]

    Nr   rL   zàCan't infer type from @declared_attr on function '{}';  please specify a return type from this function that is one of: Mapped[<python type>], relationship[<target class>], Column[<TypeEngine>], MapperProperty[<python type>]rM   ):Ú
decoratorsr8   r   r   r   r$   rR   ÚDECLARED_ATTRr3   r4   Úindexr%   Úname_is_dunderrF   r   r   rb   Úvarr\   r   r   rQ   Úfuncr   Úret_typer   Útype_id_for_unbound_typerS   rT   rU   rV   rW   rX   rY   r   rZ   r[   r   r]   r^   r    r#   r_   r   r`   ra   r.   Úunbound_to_instanceÚ
named_typeÚNAMED_TYPE_SQLA_MAPPEDÚexpr_to_mapped_constructorr
   Ú	argumentsrc   rd   rN   rO   r>   )r&   r'   rB   rI   ÚdecÚ	dec_indexrf   Úany_Ú	left_nodeÚnew_stmtÚ	func_typerg   ri   rA   rj   ÚrvaluerC   rC   rD   r:   Ë   s¤   
ÿ€



úÿ	÷

ÿüÿ	
üÿ


ÿÿÿ
ûÿ	r:   r   c              	   C  sp  |j d }t|tƒsdS | jj |j¡}|dusJ ‚|j}t|tƒr$dS ||ju s+J ‚t|t	ƒs2J ‚|jdkrG| 
|j¡du rEt | j¡ dS |jdkrSt | j¡ n1|j d¡r[dS |jdkr„t|jtƒsnt |d|¡ n|jjD ]}t|ttfƒrƒt | |||¡ qrd}d}	|js�|jdu rÃt|jtƒrÂ|j}	|jjd	krÂ| d	| ¡}
|
durÂ|
jdurÂt |
j¡tju rÂt|jjd ƒ}	|j}n!t|jƒ}t|tƒràt |j¡tju ràt|jd ƒ}	|}n|}	d}t|jtƒrñ|durñ|	}n$t|jt ƒ�rt|jj!t"ƒ�rt# $||||	|jj!¡}|du �rdS ndS |du�sJ ‚| %tj&|j|j'|j(|| jd
�¡ t )||||	|¡ dS )zZExtract mapping information from an assignment statement in a
    declarative class.

    r   NÚ__abstract__TÚ__tablename__Ú__Ú_mypy_mapped_attrsz+_mypy_mapped_attrs is expected to be a listÚMappedrM   )*Úlvaluesr8   r   r>   r$   ÚgetrF   r\   r   r   Ú
parse_boolr~   r%   Úset_is_baseÚset_has_tabler/   r   r`   r5   r   r"   Úapply_mypy_mapped_attrÚis_inferredrQ   r   r[   rR   rS   r   rY   r   r   r   Úcalleer   r#   Ú#infer_type_from_right_hand_nameexprrc   rd   rN   rO   Úapply_type_to_mapped_statement)r&   r'   rB   rI   ÚlvaluerA   r\   ÚitemÚleft_hand_mapped_typerf   Ú
mapped_symÚ	node_typeÚpython_type_for_typerC   rC   rD   r9   g  sž   






€

ÿ
ÿ€
ÿÿÿ
ÿûÿ
ûr9   c                 C  sN   t  | |¡}|du rdS |jdd… D ]}|j d¡rqt|j|dd� qdS )zèGiven a class, iterate through its superclass hierarchy to find
    all other classes that are considered as ORM-significant.

    Locates non-mapped mixins and scans them for mapped attributes to be
    applied to subclasses.

    Nr!   éÿÿÿÿr,   T)r(   )r%   r-   Úmror.   r/   rE   Údefn)r&   r'   r>   Ú	base_inforC   rC   rD   r;   é  s   ÿùr;   N)F)r&   r   r'   r   r(   r)   r*   r+   )r&   r   r'   r   rF   rG   rH   r   rI   rJ   r*   rK   )
r&   r   r'   r   rB   r	   rI   rJ   r*   rK   )
r&   r   r'   r   rB   r   rI   rJ   r*   rK   )r&   r   r'   r   r*   rK   ).Ú
__future__r   Útypingr   r   r   Ú
mypy.nodesr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Úmypy.pluginr   Ú
mypy.typesr   r   r   r   r   r   r   r   r   r    Ú r"   r#   r$   r%   rE   r6   r:   r9   r;   rC   rC   rC   rD   Ú<module>   sV   ý
B
Y 
 