o
    ãRc˜H  ã                   @   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ededed ee d!ed"ee fd#d$„Z#dededed ee d"ee f
d%d&„Z$dededed ee d"ee f
d'd(„Z%dededed ee d"ee f
d)d*„Z&	+d;dededed ee d,ee	 d"ee fd-d.„Z'	+	+d<deded ed/ed0ee d1ee d"ee fd2d3„Z(deded ed/ed"ee f
d4d5„Z)deded ee d"ee fd6d7„Z*deded8ee
 d"efd9d:„Z+d+S )=é    )ÚOptional)ÚSequence)Úmap_instance_to_supertype)Úformat_type)Ú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ÚapiÚstmtÚnodeÚleft_hand_explicit_typeÚinfer_from_right_sideÚreturnc                 C   s¤   t  |¡}|d u rd S |t ju rt| |||ƒ}|S |t ju r't| |||ƒ}|S |t ju r5t| |||ƒ}|S |t ju rBt	| ||ƒ}|S |t j
u rPt| |||ƒ}|S d S ©N)r   Útype_id_for_calleeÚCOLUMNÚ_infer_type_from_decl_columnZRELATIONSHIPÚ_infer_type_from_relationshipZCOLUMN_PROPERTYÚ%_infer_type_from_decl_column_propertyZSYNONYM_PROPERTYÚ#infer_type_from_left_hand_type_onlyZCOMPOSITE_PROPERTYÚ(_infer_type_from_decl_composite_property)r   r   r    r!   r"   Útype_idÚpython_type_for_type© r.   ú;D:\Flask\env\Lib\site-packages\sqlalchemy/ext/mypy/infer.pyÚ#infer_type_from_right_hand_nameexpr'   s:   

ÿ
íÿ
ñÿ
õÿ

ùÿþr0   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ƒrt |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   NZuselistZ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   Zget_callexpr_kwargZ
parse_boolÚ
named_typer   ZNAMED_TYPE_BUILTINS_LISTÚcalleer	   Útyper   r   Zret_typeÚfailr   r   ÚformatÚnamer*   Ú3_infer_collection_type_from_left_and_inferred_rightÚ(_infer_type_from_left_and_inferred_right)r   r   r    r!   Útarget_cls_argr-   Úrelated_object_typeZuselist_argZcollection_cls_argZtype_is_a_collectionÚrtZcallable_ret_typeÚmsgr.   r.   r/   r(   M   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 )z3Infer the type of mapping from a CompositeProperty.r   N)
r1   r2   r   r3   r   r    r   r   r*   r;   )r   r   r    r!   r<   r-   r=   r.   r.   r/   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)r1   r2   r   r3   r   r%   r5   r&   r'   ZQUERY_EXPRESSIONr*   )r   r   r    r!   Zfirst_prop_argr,   r.   r.   r/   r)     s2   

û
üÿr)   Nr@   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   é   r.   )r1   r   r2   r   r3   r5   r   r   r   r    r   r   r
   r   Z
mro_has_idÚmroZ
TYPEENGINEÚ#extract_python_type_from_typeenginer;   r   r   r*   )	r   r   r    r!   r@   r5   Z
column_argÚ	type_argsr-   r.   r.   r/   r'   D  sP   &
ü
ÿÿÿÿr'   r-   Úorig_left_hand_typeÚorig_python_type_for_typec              	   C   s\   |du r|}|du r|}t ||ƒs,|  tj|g¡}d}t | | |jt|ƒt|ƒ¡|¡ |S )zValidate 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   r4   r   ÚNAMED_TYPE_SQLA_MAPPEDr   r7   r8   r9   r   )r   r    r!   r-   rE   rF   Zeffective_typer?   r.   r.   r/   r;   ¨  s(   
ÿÿýù
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   )rE   rF   )r3   r   r1   r   r   r;   )r   r    r!   r-   rE   rF   Zleft_hand_argZpython_type_argr.   r.   r/   r:   Ò  s"   úr:   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   r7   r8   r9   r4   r   rG   r   r   Zspecial_form)r   r    r!   r?   r.   r.   r/   r*   ï  s   ÿÿr*   rD   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éÿÿÿÿ)Úfullnamer1   r   r    r   rB   r   r4   r   ZNAMED_TYPE_BUILTINS_STRZhas_baseZlookup_fully_qualified_or_noner   r   r3   )r   r    rD   Z	first_argZbase_Ztype_engine_symZtype_enginer.   r.   r/   rC     s2   ÿ
ÿÿÿÿ
ÿþrC   r$   )NN),Útypingr   r   Zmypy.maptyper   Zmypy.messagesr   Z
mypy.nodesr   r   r   r	   r
   r   r   r   r   r   r   Zmypy.pluginr   Zmypy.subtypesr   Z
mypy.typesr   r   r   r   r   r   r   r   Ú r   r   r0   r(   r+   r)   r'   r;   r:   r*   rC   r.   r.   r.   r/   Ú<module>   s  ÿþýüû
ú&ÿþýü
û 'ÿþýü
û ÿþýü
û6ûÿþýüû
úiúÿþýüûú
ù*ÿþýü
ûÿþý
üÿþýü