o
    E6dE                     @  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
startswithZget_mapped_attributesZestablish_as_sqlalchemyr"   Z re_apply_declarative_assignmentsdefsbodyr$   items_scan_symbol_table_entryZflatten_typechecking
isinstancer   !_scan_declarative_assignment_stmtr	    _scan_declarative_decorator_stmt_scan_for_mapped_basesZadd_additional_orm_attributesZset_mapped_attributes)r&   r'   r(   infoZmapped_attributesZsym_namesymstmt r;   uC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\sqlalchemy/ext/mypy/decl_class.py,scan_declarative_assignments_and_apply_types0   sD   





r=   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rt|
jtrt|
jtjr|tt||
jg t g}nt|d|
jj| |rd}t|||| j|  ttj}|dur|jdusJ | 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   T4Column type should be a TypeEngine subclass not '{}'zCan'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>]r>   linecolumntypr8   )%r   typer4   r   r$   type_id_for_named_nodeMAPPEDRELATIONSHIPCOMPOSITE_PROPERTYMAPPER_PROPERTYSYNONYM_PROPERTYCOLUMN_PROPERTYargsCOLUMNr   r   lookup_qualifiedr>   nodehas_base_type_id
TYPEENGINEr    r#   #extract_python_type_from_typeenginer   r%   failformatr.   r   r   special_formappendSQLAlchemyAttributerF   rG   r8   )r&   r'   r>   r@   rA   Z
value_typeleft_hand_explicit_typetype_iderrtypeengine_argr9   msgr;   r;   r<   r3   r   sv   



	

r3   r:   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!rt"|
j!d }nE|tj#u r|
j!r|
j!d }t|tr|$|j|}|durt|jt%rt&|jtj'rt(t)*||jg t+ g}nt,|d-|jj.|
 |du rd}t,||-|jj| ttj}t|jj}|j|_t|trt"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   rD   zCan'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>]rE   ):Z
decoratorsr4   r   r   r   r$   rJ   ZDECLARED_ATTRr0   r1   indexr%   Zname_is_dunderr>   r   r   rZ   varrT   r   r   rI   funcr   Zret_typer   Ztype_id_for_unbound_typerK   rL   rM   rN   rO   rP   rQ   r   rR   rS   r   rU   rV   r    r#   rW   r   rX   rY   r.   Zunbound_to_instanceZ
named_typeZNAMED_TYPE_SQLA_MAPPEDZexpr_to_mapped_constructorr
   	argumentsr[   r\   rF   rG   r8   )r&   r'   r:   rA   decZ	dec_indexr]   Zany_Z	left_nodeZnew_stmt	func_typer^   r`   r9   ra   rvaluer;   r;   r<   r6      s   




	

	




	r6   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rt|jtsnt|d| n|jjD ]}t|ttfrt| ||| qrd}d}	|js|jdu rt|jtr|j}	|jjd	kr|d	| }
|
dur|
jdurt|
jtju rt|jjd }	|j}n!t|j}t|trt|jtju rt|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   NZ__abstract__TZ__tablename____Z_mypy_mapped_attrsz+_mypy_mapped_attrs is expected to be a listZMappedrE   )*Zlvaluesr4   r   r8   r$   getr>   rT   r   r   Z
parse_boolrh   r%   Zset_is_baseZset_has_tabler/   r   rX   r2   r   r"   Zapply_mypy_mapped_attrZis_inferredrI   r   rS   rJ   rK   r   rQ   r   r   r   Zcalleer   r#   Z#infer_type_from_right_hand_nameexprr[   r\   rF   rG   Zapply_type_to_mapped_statement)r&   r'   r:   rA   Zlvaluer9   rT   itemZleft_hand_mapped_typer]   Z
mapped_symZ	node_typeZpython_type_for_typer;   r;   r<   r5   g  s   












r5   c                 C  sN   t | |}|du rdS |jdd D ]}|jdrqt|j|dd qdS )zGiven 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/   r=   Zdefn)r&   r'   r8   Z	base_infor;   r;   r<   r7     s   r7   N)F)r&   r   r'   r   r(   r)   r*   r+   )r&   r   r'   r   r>   r?   r@   r   rA   rB   r*   rC   )
r&   r   r'   r   r:   r	   rA   rB   r*   rC   )
r&   r   r'   r   r:   r   rA   rB   r*   rC   )r&   r   r'   r   r*   rC   ).
__future__r   typingr   r   r   Z
mypy.nodesr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Zmypy.pluginr   Z
mypy.typesr   r   r   r   r   r   r   r   r   r     r"   r#   r$   r%   r=   r3   r6   r5   r7   r;   r;   r;   r<   <module>   sV   
B
Y 
 