o
    gK                     @  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   [/var/www/html/ecg_monitoring/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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   Nuselist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   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 )zInfer 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
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      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 )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   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 )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   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
*
