a
    [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' 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 rt	
|d| d S t|j}t |tttfsJ |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   g/var/www/html/cobodadashboardai.evdpl.com/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sJ |t|g|_	q|rt| 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|sdd |D }|jdd D ]@}d|jvr\qLt ||}|du rrqL|D ]}||j|j	 qvqLg }	|
 D ]:\}
}|du rttj}|	tt|
||t|td qt|| 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