a
    xˆc2   ã                	   @   s   d dl 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( ed#eeef d$�Z)G d%d&„ d&ƒZ*d'd(„ Z+ee,edd)œd*d+„Z-ee,ee d,œd-d.„Z.ee,ee d,œd/d0„Z/edd1œd2d3„Z0edd1œd4d5„Z1ee2d1œd6d7„Z3ee2d1œd8d9„Z4edd1œd:d;„Z5ee2d1œd<d=„Z6eeeee*  d>œd?d@„Z7eee* ddAœdBdC„Z8ee,eddDœdEdF„Z9eeef e,e,e,ddGœdHdI„Z:edJdKœee,deeeef  dLœdMdN„ƒZ;eee,ee
e) dJf ee) dLœdOdN„ƒZ;ddKœee,eee
e dJf  ee dLœdPdN„Z;ee ee dQœdRdS„Z<ee	e	dTœdUdV„Z=eeee dWœdXdY„Z>eedZœd[d\„Z?dS )]é    N)ÚAny)ÚIterable)ÚIterator)ÚList)ÚOptional)Úoverload)ÚTuple)ÚType)ÚTypeVar)ÚUnion)ÚARG_POS)ÚCallExpr)ÚClassDef)ÚCLASSDEF_NO_INFO)ÚContext)Ú
Expression)ÚIfStmt)ÚJsonDict)Ú
MemberExpr)ÚNameExpr)Ú	Statement)ÚSymbolTableNode)ÚTypeInfo)ÚClassDefContext)ÚDynamicClassDefContext)ÚSemanticAnalyzerPluginInterface)Údeserialize_and_fixup_type)Úmap_type_from_supertype)ÚInstance)ÚNoneType)ÚTypeVarType)ÚUnboundType)Ú	UnionTypeÚ	_TArgType)Úboundc                   @   s^   e Zd Zeeeee eddœdd„Ze	dœdd„Z
eddœd	d
„Zeee	ed dœdd„ƒZdS )ÚSQLAlchemyAttributeN)ÚnameÚlineÚcolumnÚtypÚinfoÚreturnc                 C   s"   || _ || _|| _|| _|| _d S ©N)r&   r'   r(   Útyper*   )Úselfr&   r'   r(   r)   r*   © r/   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/sqlalchemy/ext/mypy/util.pyÚ__init__+   s
    zSQLAlchemyAttribute.__init__)r+   c                 C   s$   | j s
J ‚| j| j| j| j  ¡ dœS )N)r&   r'   r(   r-   )r-   r&   r'   r(   Ú	serialize)r.   r/   r/   r0   r2   9   s    
üzSQLAlchemyAttribute.serialize)Úsub_typer+   c                 C   s&   t | jtƒsdS t| j|| jƒ| _dS )zxExpands type vars in the context of a subtype when an attribute is
        inherited from a generic super type.
        N)Ú
isinstancer-   r    r   r*   )r.   r3   r/   r/   r0   Úexpand_typevar_from_subtypeB   s    z/SQLAlchemyAttribute.expand_typevar_from_subtype)r*   ÚdataÚapir+   c                 C   s,   |  ¡ }t| d¡|ƒ}| f ||dœ|¤ŽS )Nr-   )r)   r*   )Úcopyr   Úpop)Úclsr*   r6   r7   r)   r/   r/   r0   ÚdeserializeK   s    zSQLAlchemyAttribute.deserialize)Ú__name__Ú
__module__Ú__qualname__ÚstrÚintr   r	   r   r1   r   r2   r5   Úclassmethodr   r;   r/   r/   r/   r0   r%   *   s   ù		ûr%   c                 C   s   t t d| ¡ƒS )Nz	^__.+?__$)ÚboolÚreÚmatch)r&   r/   r/   r0   Úname_is_dunderW   s    rE   )r*   Úkeyr6   r+   c                 C   s   || j  di ¡|< d S ©NZ
sqlalchemy©ÚmetadataÚ
setdefault)r*   rF   r6   r/   r/   r0   Ú_set_info_metadata[   s    rK   )r*   rF   r+   c                 C   s   | j  di ¡ |d ¡S rG   )rI   Úget)r*   rF   r/   r/   r0   Ú_get_info_metadata_   s    rM   c                 C   s0   | j r,| j D ]}t||ƒ}|d ur|  S qd S r,   )ÚmrorM   )r*   rF   ÚbaserI   r/   r/   r0   Ú_get_info_mro_metadatac   s    


rP   )r*   r+   c                 C   s   | j  di ¡ d S rG   rH   ©r*   r/   r/   r0   Úestablish_as_sqlalchemyl   s    rR   c                 C   s   t | ddƒ d S ©NÚis_baseT©rK   rQ   r/   r/   r0   Úset_is_basep   s    rV   c                 C   s   t | dƒ}|du S rS   ©rM   ©r*   rT   r/   r/   r0   Úget_is_baset   s    
rY   c                 C   s   t | dƒ}|du S rS   )rP   rX   r/   r/   r0   Úhas_declarative_basey   s    
rZ   c                 C   s   t | ddƒ d S ©NZ	has_tableTrU   rQ   r/   r/   r0   Úset_has_table~   s    r\   c                 C   s   t | dƒ}|du S r[   rW   rX   r/   r/   r0   Úget_has_table‚   s    
r]   )r*   r7   r+   c                 C   sJ   t | dƒ}|d u rd S g }|D ]&}t | ||¡}| | ¡ | |¡ q|S )NÚmapped_attributes)rM   r%   r;   r5   Úappend)r*   r7   r^   Ú
attributesr6   Úattrr/   r/   r0   Úget_mapped_attributes‡   s    ÿ
rb   )r*   r`   r+   c                 C   s   t | ddd„ |D ƒƒ d S )Nr^   c                 S   s   g | ]}|  ¡ ‘qS r/   )r2   )Ú.0Ú	attributer/   r/   r0   Ú
<listcomp>    ó    z)set_mapped_attributes.<locals>.<listcomp>rU   )r*   r`   r/   r/   r0   Úset_mapped_attributesš   s
    ýrg   )r7   ÚmsgÚctxr+   c                 C   s   d| }|   ||¡S )Nz[SQLAlchemy Mypy plugin] %s)Úfail)r7   rh   ri   r/   r/   r0   rj   ¤   s    rj   )ri   ÚmoduleÚsymbol_nameÚasnamer+   c                 C   s8   | j j| j j j}||vr4| j j| j| }|||< d S r,   )r7   ÚmodulesZ
cur_mod_idÚnames)ri   rk   rl   rm   Úmodule_globalsZ
lookup_symr/   r/   r0   Ú
add_global©   s    ÿrq   .)Ú
expr_types)Úcallexprr&   rr   r+   c                C   s   d S r,   r/   ©rs   r&   rr   r/   r/   r0   Úget_callexpr_kwarg¹   s    ru   c                C   s   d S r,   r/   rt   r/   r/   r0   ru   À   s    c                C   sR   z| j  |¡}W n ty$   Y d S 0 | j| }t||d ur@|nttfƒrN|S d S r,   )Ú	arg_namesÚindexÚ
ValueErrorÚargsr4   r   r   )rs   r&   rr   Zarg_idxÚkwargr/   r/   r0   ru   Ê   s    
ÿ)Ústmtsr+   c                 c   sX   | D ]N}t |tƒrLt |jd tƒrL|jd jdkrL|jd jD ]
}|V  q>q|V  qd S )Nr   ztyping.TYPE_CHECKING)r4   r   Úexprr   ÚfullnameÚbody)r{   ÚstmtZsubstmtr/   r/   r0   Úflatten_typecheckingÞ   s    ÿþý
r€   )r7   r)   r+   c                    s’   t |tƒs|S |jdkr>tˆ t‡ fdd„|jD ƒtƒ g ƒƒS ˆ  |j|¡}|durŠt |tƒrŠt |j	t
ƒrŠ|j	}t|‡ fdd„|jD ƒƒS |S dS )zÔTake the UnboundType that we seem to get as the ret_type from a FuncDef
    and convert it into an Instance/TypeInfo kind of structure that seems
    to work as the left-hand type of an AssignmentStatement.

    r   c                    s   g | ]}t ˆ |ƒ‘qS r/   )Úunbound_to_instance)rc   Ztyp_arg©r7   r/   r0   re      rf   z'unbound_to_instance.<locals>.<listcomp>Nc                    s$   g | ]}t |tƒrtˆ |ƒn|‘qS r/   )r4   r!   r�   )rc   Úargr‚   r/   r0   re     s   þÿ)r4   r!   r&   r�   r"   ry   r   Úlookup_qualifiedr   Únoder   r   )r7   r)   r…   Z
bound_typer/   r‚   r0   r�   ë   s4    	

ÿÿþÿþ
ý
üþ
r�   )r:   r7   r+   c                 C   sD   | j tu r>| | j| ¡}|d u r$d S |r4t|jtƒs8J ‚|jS | j S r,   )r*   r   r„   r&   r4   r…   r   )r:   r7   Úsymr/   r/   r0   Úinfo_for_cls  s    
r‡   )r|   r+   c                 C   s,   t dƒ}d|_t|dƒ}t|| gtgdgƒS )NZ__sa_Mappedz sqlalchemy.orm.attributes.MappedZ_empty_constructorZarg1)r   r}   r   r   r   )r|   Zcolumn_descriptorZmember_exprr/   r/   r0   Úexpr_to_mapped_constructor(  s    
ürˆ   )@rC   Útypingr   r   r   r   r   r   r   r	   Z
TypingTyper
   r   Z
mypy.nodesr   r   r   r   r   r   r   r   r   r   r   r   r   Zmypy.pluginr   r   r   Zmypy.plugins.commonr   Zmypy.typeopsr   Z
mypy.typesr   r   r    r!   r"   r#   r%   rE   r?   rK   rM   rP   rR   rV   rB   rY   rZ   r\   r]   rb   rg   rj   rq   ru   r€   r�   r‡   rˆ   r/   r/   r/   r0   Ú<module>   s¬   -	
þþ

ûÿþûüûþ1þ