o
    =a%                     @   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e	d ed!eeef d"ee"j# d#d$f
d%d&Z$de	d ed"ee"j# d#d$fd'd(Z%d ed)ed*ed+ee d,ee d#d$fd-d.Z&de	d ed"ee"j# d#d$fd/d0Z'd ede	d1e(d2e(d#d$f
d3d4Z)d$S )5    )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clsapiitem
attributesreturnNc              	   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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   O/var/www/html/gps/gps/lib/python3.10/site-packages/sqlalchemy/ext/mypy/apply.pyapply_mypy_mapped_attr)   sN   





r9   c           	   	   C   s  dd |D }d}| j jD ]}t|trt|jd tr|jd j|v rt|jd jtr|jd j}||jd j j	}t
|j	}t|trt|tst|jtrt|jjtrt|jjjtr|jjjjdur|jjjjjdkr|jjjdkrt|jjd trt|jjd jtrt|||||jjd j}|du st|trq|||jd j _	d}|dur|d	|g|_	q|rt| 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 r7   )r%   .0attrr7   r7   r8   
<dictcomp>g   s    z4re_apply_declarative_assignments.<locals>.<dictcomp>Fr   Nz sqlalchemy.orm.attributes.Mapped_empty_constructorT__sa_Mapped)r,   r-   r*   r   r.   r   r%   noder   r1   r   r   rvaluer   calleer   exprfullnameargsr   r   #infer_type_from_right_hand_nameexpr
named_typer   set_mapped_attributesr)   )	r    r!   r#   mapped_attr_lookupupdate_cls_metadatar5   	left_nodepython_type_for_typeleft_node_proper_typer7   r7   r8    re_apply_declarative_assignments^   s   	






rN   r5   lvaluer6   rL   c                 C   sb   |j }t|ts
J |dur| d|g|_nd|_| d|du r#g 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>

    Nr?   F)	r@   r*   r   rG   r1   is_inferred_defr   expr_to_mapped_constructorrA   )r!   r5   rO   r6   rL   rK   r7   r7   r8   r4      s   r4   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rt |rt|| d
d	 |st|| 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 r7   )r%   r1   r:   r7   r7   r8   r=      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%   r1   itemsr   r   special_formr2   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_attributesr<   	argumentsr%   r(   r7   r7   r8   add_additional_orm_attributes   sJ   


	rk   qualified_nameattrnamec                 C   sb   |  |}|rt|jtsJ t|jg }nttj}t|}|j	|_	||_
tt||j	j|< d S )N)lookup_fully_qualified_or_noner*   r@   r   r   r   r   rc   r   r)   r1   r   r
   r]   )r!   r    rl   rm   symtype_varr7   r7   r8   re     s   

re   )*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   r3   r9   rN   r4   rk   strre   r7   r7   r7   r8   <module>   s   

5
Q
6
5