第三方库中类的属性的覆盖类型提示

发布于 2025-01-23 03:05:25 字数 503 浏览 4 评论 0原文

我有一个第三方Web-Framework,其某些代码中具有类型的提示。我无法控制上述框架的代码,

假设我有以下内容

from framework import web
...


class UserPosts(web.WebView):
    async def get(self):
        user_id = await self.request.user_dict['id']
        ...
    

web.webview class在其代码中具有类型提示,request> request属性被标记为特定的实例在定义中没有user_dict attr的类,因为这是我的自定义中间件动态附加到请求实例上的。

我想为user_dict提供类型提示,但我不明白如何处理。

有人可以帮助我提供有关正确方向的提示吗?

谢谢。

I have a 3rd-party web-framework which has type hints in some of its code. I can not control the code of the said framework

Suppose I have following piece

from framework import web
...


class UserPosts(web.WebView):
    async def get(self):
        user_id = await self.request.user_dict['id']
        ...
    

web.WebView class has type hints in its code and request attribute is marked as an instance of a specific class which doesn't have user_dict attr in definition because it is a dict that my custom made middleware dynamically attaches to the request instance.

I want to provide type hint for user_dict but I don't understand how this can be approached.

Can someone help me with a tip about the right direction?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2025-01-30 03:05:25

定义一个新课程,
并在该类中覆盖CTOR:

class MyWebView(web.WebView):

    def __init__(self, ...):
        super().__init__( ... )

到目前为止,什么都没有改变。

现在调整构造函数,以便使用适当的类型提示,
添加记录和参数验证以品尝,
世界是你的牡蛎!

Define a new class,
and override the ctor within that class:

class MyWebView(web.WebView):

    def __init__(self, ...):
        super().__init__( ... )

So far nothing has changed.

Now adjust the constructor so it uses proper type hints,
add logging and parameter validation to taste,
the world is your oyster!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文