U aE @sRddlmZddlmZddlmZddlmZddlmZddlm Z ddlm Z ddlm Z dd lm Z dd lm Z dd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!eeeeee eedddZ"eeeeeeedd d!Z#eeeeeeedd"d#Z$eeeeeeedd$d%Z%d6eeeeeee eed'd(d)Z&d7eeeeeeeeeed*d+d,Z'eeeeeed-d.d/Z(eeeeeed0d1d2Z)eeee ed3d4d5Z*d&S)8)Optional)Sequence)map_instance_to_supertype) format_type)AssignmentStmt)CallExpr) Expression)FuncDef) MemberExpr)NameExpr)RefExpr)StrExpr)TypeInfo)Var)SemanticAnalyzerPluginInterface) is_subtype)AnyType) CallableType)get_proper_type)Instance)NoneType) ProperType) TypeOfAny) UnionType)names)util)apistmtnodeleft_hand_explicit_typeinfer_from_right_sidereturncCst|}|dkrdS|tjkr0t||||}nj|tjkrJt||||}nP|tjkrdt||||}n6|tjkr|t |||}n|tj krt ||||}ndS|S)N) r_type_id_for_calleeCOLUMN_infer_type_from_decl_columnZ RELATIONSHIP_infer_type_from_relationshipZCOLUMN_PROPERTY%_infer_type_from_decl_column_propertyZSYNONYM_PROPERTY$_infer_type_from_left_hand_type_onlyZCOMPOSITE_PROPERTY(_infer_type_from_decl_composite_property)rrrr r!type_idpython_type_for_typer,ZC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-nyjtotrf\sqlalchemy\ext\mypy\infer.py$_infer_type_from_right_hand_nameexpr&sN      r.)rrrr r"c Cst|jtst|jjd}d}t|trFt|jtrF|j}t|g}t |jd}t |jd}d} |dk r| |dkr|dkrd} |dk r| d|g}nt|dks| |dkr|dk rd} t|tr|j }t|tr t|jtr |dk rt|j|g}nt|trt|jtr|jjdk r|dk rt|jjtrt|jjj} t| trt| j} t| trt| j|g}nt |d|jd}nr|dk r| |dkr|dk rt |d |j|dk rt|tg}n$|dkrd } t || |j||dkr,t|||S|dk rz| rjt|tsLtt|ts\tt||||St||||Sn|SdS) aInfer 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"] rNZuselistZcollection_classFTz__builtins__.listz>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.) isinstancervaluerAssertionErrorargsr rrrrZ_get_callexpr_kwargZ parse_bool named_typecalleer typerrZret_typefailrrformatnamer(3_infer_collection_type_from_left_and_inferred_right(_infer_type_from_left_and_inferred_right) rrrr target_cls_argr+related_object_typeZ uselist_argZcollection_cls_argZtype_is_a_collectionrtZcallable_ret_typemsgr,r,r-r&Ls                  r&cCs~t|jtst|jjd}d}t|trHt|jtrH|j}t|g}nd}|dkr`t |||S|dk rvt ||||S|SdS)z3Infer the type of mapping from a CompositeProperty.rN) r/r0rr1r2r rrrr(r:)rrrr r;r+r<r,r,r-r)s0   r)cCsZt|jtst|jjd}t|trNt|j}|tjkrNt |||||dSt |||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/r0rr1r2rr#r4r$r%r()rrrr Zfirst_prop_argr*r,r,r-r's"     r'N)rrrr r?r"c Cs$t|tstd}|dkr0t|jts*dS|j}|jddD]p}t|trjt|jtr|j}|j}qq>t|tt frt|j t r>|}d}qqq>q>t|t frq>q>ds>tq>|dkrdSt|j t rt |j jt jrt||j |}|dk rt||||St|tgSn t|||SdS)aInfer 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] Nrr,F)r/rr1r0rr2r4r r r rrr rZ _mro_has_idmroZ TYPEENGINE$_extract_python_type_from_typeenginer:rrr() rrrr r?r4Z column_arg type_argsr+r,r,r-r%4sZ&       r%)rrr r+orig_left_hand_typeorig_python_type_for_typer"c CsZ|dkr |}|dkr|}t||sV|d|g}d}t|||jt|t|||S)zValidate type when a left hand annotation is present and we also could infer the right hand side:: attrname: SomeType = Column(SomeDBType) N __sa_MappedzRLeft hand assignment '{}: {}' not compatible with ORM mapped expression of type {})rr3rr6r7r8r)rrr r+rDrEZeffective_typer>r,r,r-r:s*  r:)rrr r+r"cCsl|}|}|jr,t|jd}t|jd}n|}|}t|ttfsFtt|ttfsXtt||||||dS)Nr)rDrE)r2rr/rrr1r:)rrr r+rDrEZ left_hand_argZpython_type_argr,r,r-r9s"r9)rrr r"cCs>|dkr6d}t|||j||dttjgS|SdS)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[] on the left hand side.rF)rr6r7r8r3rrZ special_form)rrr r>r,r,r-r(s r()rrrCr"cCs|jdkrb|rb|d}t|trVt|jtrV|jjD]}|jdkr4t|jgSq4n |dgS|dsxt d|| d}|dk rt|jtst t t|g|j}t |j dS)Nzsqlalchemy.sql.sqltypes.Enumrz enum.Enumz__builtins__.strz"sqlalchemy.sql.type_api.TypeEnginez+could not extract Python type from node: %s)fullnamer/r rrrArr3Zhas_baser1Zlookup_fully_qualified_or_nonerrr2)rrrCZ first_argZbase_Ztype_engine_symZ type_enginer,r,r-rBs2      rB)N)NN)+typingrrZ mypy.maptyperZ mypy.messagesrZ mypy.nodesrrrr r r r r rrZ mypy.pluginrZ mypy.subtypesrZ mypy.typesrrrrrrrrrrr.r&r)r'r%r:r9r(rBr,r,r,r-s                           ' ( ! ' e +