How can I add python type annotations to the flask global context g?

前端 未结 3 1291
挽巷
挽巷 2021-01-05 06:07

I have a decorator which adds a user onto the flask global context g:

class User:
    def __init__(self, user_data) -&         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 06:41

    You could proxy the g object. Consider the following implementation:

    import flask
    
    
    class User:
        ...
    
    
    class _g:
    
        user: User
        # Add type hints for other attributes
        # ...
    
        def __getattr__(self, key):
            return getattr(flask.g, key)
    
    
    g = _g()
    
    

提交回复
热议问题