mypy:具有动态生成的类变量的类,总是具有相同类型

发布于 2025-02-06 23:02:09 字数 446 浏览 1 评论 0原文

我想拥有一个只存储字符串变量的数据类,类似的东西:

class Properties:
   pass


class MyClass:
    def __init__(self):
        self.properties = Properties()
        properties_json = self.get_properties_json() 
        for key, value in properties_json:
            setattr(self.properties, key, value)

    def get_properties_json(self) -> dict[str, str]:
        ...

我怎么能告诉mypy,属性具有始终strings的动态变量?

I'd like to have a data class that only stores string variables, something like this:

class Properties:
   pass


class MyClass:
    def __init__(self):
        self.properties = Properties()
        properties_json = self.get_properties_json() 
        for key, value in properties_json:
            setattr(self.properties, key, value)

    def get_properties_json(self) -> dict[str, str]:
        ...

How can I tell MyPy that Properties has dynamic variables that are always strings?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文