U kf6)@sddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddl"m#Z#ddl"m$Z$dd l%m&Z&dd!l%m'Z'd"d#d$d%d&d'd(d)Z(d"d#d%d&d*d+d,Z)d#d-d.d/d/d&d0d1d2Z*d"d#d%d&d*d3d4Z+d#d"d5d5d&d6d7d8Z,d9S):) annotations)List)Optional)Union) ARG_NAMED_OPT)Argument)AssignmentStmt)CallExpr)ClassDef)MDEF) MemberExpr)NameExpr)RefExpr)StrExpr)SymbolTableNode)TempNode)TypeInfo)Var)SemanticAnalyzerPluginInterface)add_method_to_class)AnyType)get_proper_type)Instance)NoneTyp) ProperType) TypeOfAny) UnboundType) UnionType)infer)util)expr_to_mapped_constructor)NAMED_TYPE_SQLA_MAPPEDr rzUnion[NameExpr, StrExpr]zList[util.SQLAlchemyAttribute]None)clsapiitem attributesreturnc Cst|tr|j}nt|tr$|j}ndS|jjD]2}t|tr0t|jdtr0|jdj|kr0q|q0t |d||dS|j dkrt |d|dSt |j }t|t ttfst|t j||j|j||jdt|||jd|ddS)NrzCan't find mapped attribute zBStatement linked from _mypy_mapped_attrs has no typing information)namelinecolumntypinfo) isinstancer r)rvaluedefsbodyrlvaluesr ZfailtyperrrrAssertionErrorappendZSQLAlchemyAttributer*r+r-apply_type_to_mapped_statement)r$r%r&r'r)stmtleft_hand_explicit_typer9I/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/ext/mypy/apply.pyapply_mypy_mapped_attr-sV      r;)r$r%r'r(c Csdd|D}d}|jjD]}t|trt|jdtr|jdj|krt|jdjtr|jdj}||jdjj }t |j }t|t rxt|t sxt|j t rxt|j jtrxt|j jjtrx|j jjjdk rx|j jjjjtkrx|j jjdkrxt|j jdt rxt|j jdjtrxt|||||j jdj} | dk rxt| t sx| }|||jdj_ d}t|j tr|j j jtkr|dk st|t|g|_ q|rt|j|dS)zlFor multiple class passes, re-apply our left-hand side types as mypy seems to reset them in place. cSsi|] }|j|qSr9)r).0attrr9r9r: ksz4re_apply_declarative_assignments..FrNZ_empty_constructorT)r0r1r.rr2r r)noderr3rrrvaluer Zcalleer exprfullnamer"argsrrZ#infer_type_from_right_hand_nameexprrr4 named_typer Zset_mapped_attributesr-) r$r%r'Zmapped_attr_lookupZupdate_cls_metadatar7 left_nodepython_type_for_typeZleft_node_proper_typeZnew_python_type_for_typer9r9r: re_apply_declarative_assignmentsbs              rHrr zOptional[ProperType])r%r7lvaluer8rGr(cCs|j}t|tst|dk r4d|_|t|g|_n*d|_|t|dkrTtt j gn|g|_t |j |_ |jdk r|dk r||_dS)aApply the Mapped[] annotation and right hand object to a declarative assignment statement. This converts a Python declarative class statement such as:: class User(Base): # ... attrname = Column(Integer) To one that describes the final Python behavior to Mypy:: class User(Base): # ... attrname : Mapped[Optional[int]] = NF) r@r.rr4Zis_inferred_defrEr"r3rr special_formr!rA)r%r7rIr8rGrFr9r9r:r6s$ r6c Cs&t||}|dkrdSt|}d|jkr|sdd|D}|jddD]@}d|jkr\qLt||}|dkrrqL|D]}||j|j qvqLg} | D]:\} } | dkrt t j } | tt| | | t| tdqt||d| td |jkrt|rt||d d |s"t||d d dS) zCApply __init__, __table__ and other attributes to the mapped class.N__init__cSsi|]}|j|jqSr9)r)r3r<r9r9r:r? sz1add_additional_orm_attributes..rZ sqlalchemy)variableZtype_annotationZ initializerkindZ __table__zsqlalchemy.sql.schema.Tablezsqlalchemy.orm.mapper.MapperZ __mapper__)r Z info_for_clsZ get_is_basenamesmrometadataZget_mapped_attributes setdefaultr)r3itemsrrrJr5rrrrrrZ get_has_table _apply_placeholder_attr_to_class) r$r%r'r-Zis_baseZmapped_attr_namesbaseZbase_cls_attributesr> argumentsr)r,r9r9r:add_additional_orm_attributessR      rWstr)r%r$qualified_nameattrnamer(cCsr||}|r,t|jtstt|jg}n ttj}t |}|j d||_ |j |_ ||_ tt||j j|<dS)N.)Zlookup_fully_qualified_or_noner.r@rr4rrrrJrrCZ _fullnamer-r3rr rO)r%r$rYrZsymtype_varr9r9r:rT0s  rTN)- __future__rtypingrrrZ mypy.nodesrrrr r r r r rrrrrrZ mypy.pluginrZmypy.plugins.commonrZ mypy.typesrrrrrrrrrr rOr!r"r;rHr6rWrTr9r9r9r:sH                                5UD5