o
    E6dM                     @  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_calleeZMAPPED_infer_type_from_mapped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 r0   pC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\sqlalchemy/ext/mypy/infer.py#infer_type_from_right_hand_nameexpr(   sD   








r2   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rd}	t |trj|j}t |trt |jtr|durt|j|g}nyt |trt |jtr|jjdur|durt |jjtrt|jjj}
t |
trt|
j}t |trt|j|g}n?t| d|j d}n4|dur| 
|du r|durt| d|j |durt|t g}n|du rd	}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 sensezCan'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msgr0   r0   r1   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)
r3   r4   r   r5   r   r    r   r   r,   r=   )r   r   r    r!   r>   r/   r?   r0   r0   r1   r-      s$   r-   c                 C  s&   t |jtsJ t|}t| ||S )zVInfer the type of mapping from a right side expression
    that returns Mapped.


    )r3   r4   r   r   Ztype_for_calleer,   )r   r   r    r!   r#   Zthe_mapped_typer0   r0   r1   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 )zInfer the type of mapping from a ColumnProperty.

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

    r   )right_hand_expression)r3   r4   r   r5   r   r&   r7   r(   r)   ZQUERY_EXPRESSIONr,   )r   r   r    r!   Zfirst_prop_argr.   r0   r0   r1   r+   6  s2   


r+   NrB   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
rt|j	jtjrt| |j	|}|durt| |||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      r0   )r3   r   r4   r   r5   r7   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!   rB   r7   Z
column_arg	type_argsr/   r0   r0   r1   r)   g  sP   &

r)   r   r/   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 )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   r6   r   NAMED_TYPE_SQLA_MAPPEDr   r9   r:   r;   Zformat_typeoptions)r   r    r!   r/   rH   rI   Zeffective_typerA   r0   r0   r1   r=     s(   

r=   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   )rH   rI   )r5   r   r3   r   r   r=   )r   r    r!   r/   rH   rI   Zleft_hand_argZpython_type_argr0   r0   r1   r<     s"   r<   c                 C  s<   |du rd}t | ||j| | tjttj	gS |S )zDetermine the type based on explicit annotation only.

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

    NzCan'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   r9   r:   r;   r6   r   rJ   r   r   Zspecial_form)r   r    r!   rA   r0   r0   r1   r,     s   r,   r   rG   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)fullnamer3   r   r    r   rE   r   r6   r   ZNAMED_TYPE_BUILTINS_STRZhas_baseZlookup_fully_qualified_or_noner   r   r5   )r   r    rG   Z	first_argZbase_Ztype_engine_symZtype_enginer0   r0   r1   rF   .  s2   

rF   )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"   rB   rC   r$   r"   )NN)r   r   r    r   r!   r   r/   r   rH   r"   rI   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   rG   rL   r$   r   )-
__future__r   typingr   r   Zmypy.maptyper   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   r2   r*   r-   r'   r+   r)   r=   r<   r,   rF   r0   r0   r0   r1   <module>   sR   

( 
'
 
 6h
*
