o
    Šåg§K  ã                   @  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" dGd(d)„Z#dHd*d+„Z$dHd,d-„Z%dGd.d/„Z&dHd0d1„Z'	2dIdJd5d6„Z(	2	2dKdLd;d<„Z)dMd>d?„Z*dNd@dA„Z+dOdEdF„Z,d2S )Pé    )Úannotations)ÚOptional)ÚSequence)Úmap_instance_to_supertype)ÚAssignmentStmt)ÚCallExpr)Ú
Expression)ÚFuncDef)Ú
LambdaExpr)Ú
MemberExpr)ÚNameExpr)ÚRefExpr)ÚStrExpr)ÚTypeInfo)ÚVar)ÚSemanticAnalyzerPluginInterface)Ú
is_subtype)ÚAnyType)ÚCallableType)Úget_proper_type)ÚInstance)ÚNoneType)Ú
ProperType)Ú	TypeOfAny)Ú	UnionTypeé   )Únames)ÚutilÚapir   Ústmtr   Únoder   Úleft_hand_explicit_typeúOptional[ProperType]Úinfer_from_right_sider   Úreturnc                 C  sÂ   t  |¡}|d u rd S |t ju rt| ||||ƒ}|S |t ju r(t| |||ƒ}|S |t ju r6t| |||ƒ}|S |t ju rDt	| |||ƒ}|S |t j
u rQt| ||ƒ}|S |t ju r_t| |||ƒ}|S d S ©N)r   Útype_id_for_calleeÚMAPPEDÚ_infer_type_from_mappedÚCOLUMNÚ_infer_type_from_decl_columnÚRELATIONSHIPÚ_infer_type_from_relationshipÚCOLUMN_PROPERTYÚ%_infer_type_from_decl_column_propertyÚSYNONYM_PROPERTYÚ#infer_type_from_left_hand_type_onlyÚCOMPOSITE_PROPERTYÚ(_infer_type_from_decl_composite_property)r   r   r    r!   r#   Útype_idÚpython_type_for_type© r5   úU/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/ext/mypy/infer.pyÚ#infer_type_from_right_hand_nameexpr(   sD   


ÿ
éÿ
íÿ
ñÿ
õÿ

ùÿþr7   c                 C  sZ  t |jtƒsJ ‚|jjd }d}t |tƒr#t |jtƒr#|j}t|g ƒ}t 	|jd¡}t 	|jd¡}d}	|durQ|  
|¡du rQ|du rQd}	|durP|  tj|g¡}n©|du s\|  
|¡du rÆ|durÆd}	t |tƒrj|j}t |tƒr�t |jtƒr�|dur€t|j|gƒ}nyt |tƒr»t |jtƒr»|jjdur»|durºt |jjtƒrºt|jjjƒ}
t |
tƒrºt|
jƒ}t |tƒrºt|j|gƒ}n?t | d|j¡ d}n4|duré|  
|¡du ré|durÝt | d|j¡ |durèt|tƒ gƒ}n|du rúd	}t | | |j¡|¡ |du �rt| ||ƒS |du�r+|	�r$t |tƒ�sJ ‚t |tƒ�sJ ‚t| |||ƒS t| |||ƒS |S )
a¦  Infer the type of mapping from a relationship.

    E.g.::

        @reg.mapped
        class MyClass:
            # ...

            addresses = relationship(Address, uselist=True)

            order: Mapped["Order"] = relationship("Order")

    Will resolve in mypy as::

        @reg.mapped
        class MyClass:
            # ...

            addresses: Mapped[List[Address]]

            order: Mapped["Order"]

    r   NÚuselistÚcollection_classFTz>Expected Python collection type for collection_class parameterzOSending uselist=False and collection_class at the same time does not make sensezäCan't infer scalar or collection for ORM mapped expression assigned to attribute '{}' if both 'uselist' and 'collection_class' arguments are absent from the relationship(); please specify a type annotation on the left hand side.)Ú
isinstanceÚrvaluer   Úargsr   r    r   r   r   Úget_callexpr_kwargÚ
parse_boolÚ
named_typer   ÚNAMED_TYPE_BUILTINS_LISTÚcalleer	   Útyper   r   Úret_typeÚfailr   r   ÚformatÚnamer0   Ú3_infer_collection_type_from_left_and_inferred_rightÚ(_infer_type_from_left_and_inferred_right)r   r   r    r!   Útarget_cls_argr4   Úrelated_object_typeÚuselist_argÚcollection_cls_argÚtype_is_a_collectionÚrtÚcallable_ret_typeÚmsgr5   r5   r6   r,   P   s¶   ÿ
ÿÿ€
ÿÿ€ÿ
þ


þ€üüÿ€ÿ
ÿ
ÿür,   c                 C  sz   t |jtƒsJ ‚|jjd }d}t |tƒr$t |jtƒr$|j}t|g ƒ}nd}|du r0t| ||ƒS |dur;t	| |||ƒS |S )z+Infer the type of mapping from a Composite.r   N)
r:   r;   r   r<   r   r    r   r   r0   rH   )r   r   r    r!   rI   r4   rJ   r5   r5   r6   r2   ö   s$   ÿÿÿr2   c                 C  s&   t |jtƒsJ ‚t |¡}t| ||ƒS )zVInfer the type of mapping from a right side expression
    that returns Mapped.


    )r:   r;   r   r   Útype_for_calleer0   )r   r   r    r!   r#   Úthe_mapped_typer5   r5   r6   r(     s
   
ÿr(   c                 C  s”   t |jtƒsJ ‚|jjr+|jjd }t |tƒr+t |j¡}|tju r+t| ||||d�S t |jtƒrDt |jj¡}|tj	u rDt| |||ƒS t
| ||ƒS )zœInfer the type of mapping from a ColumnProperty.

    This includes mappings against ``column_property()`` as well as the
    ``deferred()`` function.

    r   )Úright_hand_expression)r:   r;   r   r<   r   r&   rA   r)   r*   ÚQUERY_EXPRESSIONr0   )r   r   r    r!   Úfirst_prop_argr3   r5   r5   r6   r.   6  s2   

û
üÿr.   NrS   úOptional[CallExpr]c           	      C  s  t |tƒsJ ‚d}|du rt |jtƒsdS |j}|jdd… D ]9}t |tƒr5t |jtƒr4|j}|j} n%qt |ttfƒrIt |j	t
ƒrH|}d} nqt |tfƒrPqt |tfƒrWqJ ‚|du r_dS t |j	t
ƒr‡t |j	jtj¡r‡t| |j	|ƒ}|dur€t| |||ƒS t|tƒ gƒS t| ||ƒS )aá  Infer the type of mapping from a Column.

    E.g.::

        @reg.mapped
        class MyClass:
            # ...

            a = Column(Integer)

            b = Column("b", String)

            c: Mapped[int] = Column(Integer)

            d: bool = Column(Boolean)

    Will resolve in MyPy as::

        @reg.mapped
        class MyClass:
            # ...

            a: Mapped[int]

            b: Mapped[str]

            c: Mapped[int]

            d: Mapped[bool]

    Nr   é   r5   )r:   r   r;   r   r<   rA   r   r   r   r    r   r   r
   r   Ú
mro_has_idÚmroÚ
TYPEENGINEÚ#extract_python_type_from_typeenginerH   r   r   r0   )	r   r   r    r!   rS   rA   Ú
column_argÚ	type_argsr4   r5   r5   r6   r*   g  sP   &
ü
ÿÿÿÿr*   r   r4   Úorig_left_hand_typeÚorig_python_type_for_typec                 C  sh   |du r|}|du r|}t ||ƒs2|  tj|g¡}d}t | | |jt || j	¡t || j	¡¡|¡ |S )z�Validate type when a left hand annotation is present and we also
    could infer the right hand side::

        attrname: SomeType = Column(SomeDBType)

    NzRLeft hand assignment '{}: {}' not compatible with ORM mapped expression of type {})
r   r?   r   ÚNAMED_TYPE_SQLA_MAPPEDr   rD   rE   rF   Úformat_typeÚoptions)r   r    r!   r4   r^   r_   Úeffective_typerP   r5   r5   r6   rH   Ê  s(   
ÿÿýù
rH   r   c                 C  sl   |}|}|j rt|j d ƒ}t|j d ƒ}n|}|}t|ttfƒs#J ‚t|ttfƒs,J ‚t| |||||d�S )Nr   )r^   r_   )r<   r   r:   r   r   rH   )r   r    r!   r4   r^   r_   Úleft_hand_argÚpython_type_argr5   r5   r6   rG   ô  s"   úrG   c                 C  s<   |du rd}t  | | |j¡|¡ |  tjttj	ƒg¡S |S )z‘Determine the type based on explicit annotation only.

    if no annotation were present, note that we need one there to know
    the type.

    Nz”Can't infer type from ORM mapped expression assigned to attribute '{}'; please specify a Python type or Mapped[<python type>] on the left hand side.)
r   rD   rE   rF   r?   r   r`   r   r   Úspecial_form)r   r    r!   rP   r5   r5   r6   r0     s   ÿÿr0   r   r]   úSequence[Expression]c                 C  s¼   |j dkr2|r2|d }t|tƒr+t|jtƒr+|jjD ]}|j dkr)t|jg ƒ  S qn|  tj	g ¡S | 
d¡s=J d| ƒ‚|  d¡}|d urLt|jtƒsNJ ‚tt|g ƒ|jƒ}t|jd ƒS )Nzsqlalchemy.sql.sqltypes.Enumr   z	enum.Enumz"sqlalchemy.sql.type_api.TypeEnginez+could not extract Python type from node: %séÿÿÿÿ)Úfullnamer:   r   r    r   rY   r   r?   r   ÚNAMED_TYPE_BUILTINS_STRÚhas_baseÚlookup_fully_qualified_or_noner   r   r<   )r   r    r]   Ú	first_argÚbase_Útype_engine_symÚtype_enginer5   r5   r6   r[   .  s2   ÿ
ÿÿÿÿ
ÿþr[   )r   r   r   r   r    r   r!   r"   r#   r   r$   r"   )
r   r   r   r   r    r   r!   r"   r$   r"   r%   )r   r   r   r   r    r   r!   r"   rS   rV   r$   r"   )NN)r   r   r    r   r!   r   r4   r   r^   r"   r_   r"   r$   r"   )
r   r   r    r   r!   r   r4   r   r$   r"   )r   r   r    r   r!   r"   r$   r"   )r   r   r    r   r]   rg   r$   r   )-Ú
__future__r   Útypingr   r   Úmypy.maptyper   Ú
mypy.nodesr   r   r   r	   r
   r   r   r   r   r   r   Úmypy.pluginr   Úmypy.subtypesr   Ú
mypy.typesr   r   r   r   r   r   r   r   Ú r   r   r7   r,   r2   r(   r.   r*   rH   rG   r0   r[   r5   r5   r5   r6   Ú<module>   sR   

( 
'
 
 6ûhú
*
