Pylint can't find SQLAlchemy query member

前端 未结 13 1583
予麋鹿
予麋鹿 2021-01-30 08:25

I have a Flask (v0.10.1) application using Flask-SQLAlchemy (v2.0) and I\'m trying to configure Pylint to check it. Running with Python 3.4.2.

First error was:



        
13条回答
  •  耶瑟儿~
    2021-01-30 08:59

    Here's a version of joeforker's answer that dynamically adds all public methods from the Session object back into a scoped_session's locals at lint-time, instead of hardcoding a few well-known method names.

    Define {path}/{to}/pylintplugins.py:

    import sys
    
    from astroid import MANAGER, scoped_nodes
    from astroid.builder import AstroidBuilder
    from sqlalchemy.orm import Session
    
    
    def register(_linter):
        pass
    
    def transform(cls):
        if cls.name == 'scoped_session':
            builder = AstroidBuilder(MANAGER)
            module_node = builder.module_build(sys.modules[Session.__module__])
            session_cls_node = [
                c for c in module_node.get_children()
                if getattr(c, "type", None) == "class" and c.name == Session.__name__
            ][0]
    
            for prop in Session.public_methods:
                cls.locals[prop] = [
                    c for c in session_cls_node.get_children() 
                    if getattr(c, "type", None) == "method" and c.name == prop
                ]
    
    MANAGER.register_transform(scoped_nodes.Class, transform)
    

    And in your .pylintrc file:

    load-plugins={path}.{to}.pylintplugins
    

提交回复
热议问题