U kf&@sRdZddlmZddlmZddlmZddlmZddlmZddlmZ ddlm Z dd l m Z dd l mZdd l mZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlmZddl"m#Z#ddl"m$Z$ddl"m%Z%z e&dWne'k rYn Xe'd Gd!d"d"eZ(d#d$d%d&d'Z)d(d)d*d+d,Z*d-d)d*d.d/Z+d-d)d*d0d1Z,d-d)d*d2d3Z-d-d)d*d4d5Z.d-d)d*d6d7Z/d-d)d*d8d9Z0d:d;d*dd)d*d?d@Z2dAdBd)dCdDdEZ3dFS)Gz" Mypy plugin for SQLAlchemy ORM. ) annotations)Callable)List)Optional)Tuple)Type)Union)nodes) calculate_mro)MroError)Block)ClassDef)GDEF)MypyFile)NameExpr) SymbolTable)SymbolTableNode)TypeInfo)AttributeContext)ClassDefContext)DynamicClassDefContext)Plugin)SemanticAnalyzerPluginInterface)get_proper_type)Instance) decl_class)names)utilzsqlalchemy-stubszThe SQLAlchemy mypy plugin in SQLAlchemy 2.0 does not work with sqlalchemy-stubs or sqlalchemy2-stubs installed, as well as with any other third party SQLAlchemy stubs. Please uninstall all SQLAlchemy stubs packages.c@s|eZdZdddddZdddddZdddd d Zdddd d Zdddd dZdddddZdddddZ dS)SQLAlchemyPluginstrz2Optional[Callable[[DynamicClassDefContext], None]])fullnamereturncCst|tjkrtSdSN)rtype_id_for_fullnameZDECLARATIVE_BASE_dynamic_class_hookselfr!r(J/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/ext/mypy/plugin.pyget_dynamic_class_hook<sz'SQLAlchemyPlugin.get_dynamic_class_hookz+Optional[Callable[[ClassDefContext], None]]cCstSr#)_fill_in_decoratorsr&r(r(r)get_customize_class_mro_hookCsz-SQLAlchemyPlugin.get_customize_class_mro_hookcCs\||}|dk rX|jdk rXt|j}|tjkr6tS|tjtjfkrJtS|tj krXt SdSr#) lookup_fully_qualifiednodertype_id_for_named_nodeZMAPPED_DECORATOR_cls_decorator_hookZAS_DECLARATIVEZAS_DECLARATIVE_BASE_base_cls_decorator_hookZDECLARATIVE_MIXIN_declarative_mixin_hook)r'r!symZtype_idr(r(r)get_class_decorator_hookHs    z)SQLAlchemyPlugin.get_class_decorator_hookcCst|tjkrtSdSr#)rr$ZDECLARATIVE_META_metaclass_cls_hookr&r(r(r)get_metaclass_hook[sz#SQLAlchemyPlugin.get_metaclass_hookcCs.||}|r*t|jtr*t|jr*tSdSr#)r- isinstancer.rrZhas_declarative_base_base_cls_hook)r'r!r3r(r(r)get_base_class_hookes   z$SQLAlchemyPlugin.get_base_class_hookz,Optional[Callable[[AttributeContext], Type]]cCs|drtSdS)Nz-sqlalchemy.orm.attributes.QueryableAttribute.) startswith_queryable_getattr_hookr&r(r(r)get_attribute_hookss z#SQLAlchemyPlugin.get_attribute_hookrzList[Tuple[int, str, int]])filer"cCs dddgS)N) sqlalchemy.orm)r>zsqlalchemy.orm.attributesr@)r>zsqlalchemy.orm.decl_apir@r()r'r=r(r(r)get_additional_deps}sz$SQLAlchemyPlugin.get_additional_depsN) __name__ __module__ __qualname__r*r,r4r6r9r<rAr(r(r(r)r;s  rr zTypingType[SQLAlchemyPlugin])versionr"cCstSr#)r)rEr(r(r)pluginsrFrNone)ctxr"cCs4t|t|jtg}|j|j|_tt||jj }||_ t |j|t j |jdtfd}|dk rt|jtrt |jtj|jj|jddt|jgg|_n|jtj}|g|_z t|WnDtk rt |jd|j|jtj}|g|_d|_YnX|j|jt t!|t |dS)zZGenerate a declarative Base class when the declarative_base() function is encountered.cls)Z expr_typesNTZ is_mixin_scanz.Not able to calculate MRO for declarative base)" _add_globalsr namer apiZqualified_namer!rrZ cur_mod_idinfo_set_declarative_metaclassrZget_callexpr_kwargcallrr7r. set_is_baser,scan_declarative_assignments_and_apply_typesZdefnrbasesZ named_typerZNAMED_TYPE_BUILTINS_OBJECTr r failZfallback_to_anyZadd_symbol_table_noderr)rHrIrNZcls_argobjr(r(r)r%s<    r%rcCs|jjD]}t|tjr:t|jtjr:|jjdkr:|j}nt|tjr|jdkr|}nqt|jt r|j j |jj|dd}nq|r|j rt |j}t|tr|jjd|j|_qt|j d|j|j qdS)NZas_declarative_baseZmappedT)Zsuppress_errors.zClass decorator called %s(), but we can't tell if it's from an ORM registry. Please annotate the registry assignment, e.g. my_registry: registry = registry())rIZ decoratorsr7r ZCallExprZcallee MemberExprrLexprrrMZlookup_qualifiedr.rtyperr!rrT)rH decoratortargetr3Zsym_typer(r(r)r+s@         r+cCst|t|jtjst|jj}t|tjrt||j}t|j|t|jjtj||jdddSNTrJ) rKrIrOrMrrQrNrrR)rHrIr(r(r)r1s r1cCs.t|t|jjtj|j|jdddSr^)rKrrQrIrNrrRrMrHr(r(r)r2sr2cCst|jjdSr#)rrQrIrNr_r(r(r)r5sr5cCst|t|j|jdSr#)rKrrRrIrMr_r(r(r)r8sr8rrcCs|jSr#)Zdefault_attr_typer_r(r(r)r;sr;z.Union[ClassDefContext, DynamicClassDefContext]cCst|ddddS)z`Add __sa_DeclarativeMeta and __sa_Mapped symbol to the global space for all class defs r?ZMappedZ __sa_MappedN)rZ add_globalr_r(r(r)rKsrKrr )rM target_clsr"cCs@|j}|d}|dk r$t|jts(tt|jg|_|_dS)Nz'sqlalchemy.orm.decl_api.DeclarativeMeta) rNZlookup_fully_qualified_or_noner7r.rr]rZdeclared_metaclassZmetaclass_type)rMr`rNr3r(r(r)rO's rON)4__doc__ __future__rtypingrrrrrZ TypingTyperZmypyr Zmypy.mror r Z mypy.nodesr r rrrrrrZ mypy.pluginrrrrrZ mypy.typesrrrrr __import__ ImportErrorrrFr%r+r0r1r2r5r8r;rKrOr(r(r(r)s`                               M'6