U kfC@sddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#d d!l$m%Z%d d"l$m&Z&d d#l$m'Z'd d$l$m(Z(d?d&d'd(d)d*d+d,Z)d&d'd-d.d/d0d1d2d3Z*d&d'd4d/d0d5d6d7Z+d&d'd8d/d0d5d9d:Z,d&d'd0d;dS)@) annotations)List)Optional)Union)AssignmentStmt)CallExpr)ClassDef) Decorator) LambdaExpr)ListExpr) MemberExpr)NameExpr)PlaceholderNode)RefExpr)StrExpr) SymbolNode)SymbolTableNode)TempNode)TypeInfo)Var)SemanticAnalyzerPluginInterface)AnyType) CallableType)get_proper_type)Instance)NoneType) ProperType)Type) TypeOfAny) UnboundType) UnionType)apply)infer)names)utilFrrboolz(Optional[List[util.SQLAlchemyAttribute]])clsapi is_mixin_scanreturncCst||}|dkrdS|jdr(dSt||}t||dk r\|sXt||||Sg}|jj s|j D]\}}t |||||qrnFt |jj D]6}t|trt||||qt|trt||||qt|||st|||t|||S)Nbuiltins)r% info_for_clsfullname startswithZget_mapped_attributesZestablish_as_sqlalchemyr"Z re_apply_declarative_assignmentsdefsbodyr$items_scan_symbol_table_entryZflatten_typechecking isinstancer!_scan_declarative_assignment_stmtr _scan_declarative_decorator_stmt_scan_for_mapped_basesZadd_additional_orm_attributesZset_mapped_attributes)r'r(r)infoZmapped_attributesZsym_namesymstmtr:N/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/ext/mypy/decl_class.py,scan_declarative_assignments_and_apply_types0sT        r<strrzList[util.SQLAlchemyAttribute]None)r'r(namevalue attributesr*c Cst|j}t|tsdSd}t|j}d}|tjtjtjtj tj tj hkrh|j rbt|j d}nd}n|tj kr|j sd}nt|j d} t| tr| j} t| ttfr|| j| } | dk rt| jtrt| jtjrtt|| jgtg}nt|d| jj||rFd} t|| ||j|ttj}|dk r|jdk s`t |!tj"||jj#|jj$||j%ddS)zaExtract mapping information from a SymbolTableNode that's in the type.names dictionary. NFrT4Column type should be a TypeEngine subclass not '{}'zCan't infer type from attribute {} on class {}. please specify a return type from this function that is one of: Mapped[], relationship[], Column[], MapperProperty[]r?linecolumntypr7)&rtyper3rr$type_id_for_named_nodeMAPPED RELATIONSHIPCOMPOSITE_PROPERTYMAPPER_PROPERTYSYNONYM_PROPERTYCOLUMN_PROPERTYargsCOLUMNrrlookup_qualifiedr?nodehas_base_type_id TYPEENGINEr r##extract_python_type_from_typeenginerr%failformatr-rr special_formAssertionErrorappendSQLAlchemyAttributerDrEr7) r'r(r?r@rAZ value_typeleft_hand_explicit_typetype_iderrtypeengine_argr8msgr:r:r;r2rsz        r2r )r'r(r9rAr*c Csv|jD](}t|tttfrt|tjkrq4qdS|jj |}d}t |j rttj}t|jj }|j|_t|gt|} |jj| _| |jj |<dSt|jjtr|jjj} t| trt| ||} ndS| tjtjtjtjtjtj hkr | j!r t"| j!d}n| tj#kr| j!r| j!d} t| tr|$| j | } | dk rt| jt%rt&| jtj'rt(t)*|| jgt+g}nt ,|d-| jj.| |dkrd}t ,||-|jj |ttj}t|jj }|j|_t|trt"t /||}|0tj1|g|j_t2t3|jj4|jj }t|g|} |jj| _|5t j6|j |j7|j8||j9d| |jj |<dS)aExtract mapping information from a @declared_attr in a declarative class. E.g.:: @reg.mapped class MyClass: # ... @declared_attr def updated_at(cls) -> Column[DateTime]: return Column(DateTime) Will resolve in mypy as:: @reg.mapped class MyClass: # ... updated_at: Mapped[Optional[datetime.datetime]] NrrBzCan't infer type from @declared_attr on function '{}'; please specify a return type from this function that is one of: Mapped[], relationship[], Column[], MapperProperty[]rC):Z decoratorsr3r r rr$rHZ DECLARED_ATTRr/r0indexr%Zname_is_dunderr?rrrXvarrRrrrGfuncrZret_typerZtype_id_for_unbound_typerIrJrKrLrMrNrOrrPrQrrSrTr r#rUrrVrWr-Zunbound_to_instanceZ named_typeZNAMED_TYPE_SQLA_MAPPEDZexpr_to_mapped_constructorr argumentsrZr[rDrEr7)r'r(r9rAdecZ dec_indexr\Zany_Z left_nodeZnew_stmtZ func_typer]r_r8r`rvaluer:r:r;r5s                      r5rc Cs|jd}t|tsdS|jj|j}|dk s4t|j}t|t rHdS||jksVtt|t sdt|jdkr| |j dkrt |jdS|jdkrt |jnd|jdrdS|jdkr t|j tst |d|n,|j jD]"}t|ttfrt||||qd}d} |js&|jdkrt|jtr|j} |jjd kr|d |} | dk r| jdk rt| jtjkrt|jjd} |j}nFt|j} t| trt| jtjkrt| jd} | }n| } d}t|j t r|dk r| } nHt|j t!r>t|j j"t#r>t$%|||| |j j"} | dkrBdSndS| dk sPt|&t j'|j|j(|j)| |jd t*|||| | dS) zZExtract mapping information from an assignment statement in a declarative class. rNZ __abstract__TZ __tablename____Z_mypy_mapped_attrsz+_mypy_mapped_attrs is expected to be a listZMappedrC)+Zlvaluesr3r r7r$getr?rYrRrrZ parse_boolrfr%Z set_is_baseZ set_has_tabler.r rVr1rr"Zapply_mypy_mapped_attrZ is_inferredrGrrQrHrIrrOrrrZcalleerr#Z#infer_type_from_right_hand_nameexprrZr[rDrEZapply_type_to_mapped_statement) r'r(r9rAZlvaluer8rRitemZleft_hand_mapped_typer\Z mapped_symZ node_typeZpython_type_for_typer:r:r;r4gs                 r4)r'r(r*cCsNt||}|dkrdS|jddD]"}|jdr8q&t|j|ddq&dS)zGiven a class, iterate through its superclass hierarchy to find all other classes that are considered as ORM-significant. Locates non-mapped mixins and scans them for mapped attributes to be applied to subclasses. Nr!r+T)r))r%r,mror-r.r<Zdefn)r'r(r7Z base_infor:r:r;r6s  r6N)F). __future__rtypingrrrZ mypy.nodesrrrr r r r r rrrrrrrrZ mypy.pluginrZ mypy.typesrrrrrrrrrr r"r#r$r%r<r2r5r4r6r:r:r:r;sT                                   BY