Python键入有效的布尔组合

发布于 2025-01-27 22:16:17 字数 904 浏览 2 评论 0原文

我正在尝试使用Python的类型检查器来捕获不兼容的权限。我有一个允许的情况,可以允许(布尔值)(布尔)(布尔值)进行特定的诉讼。显然,我想排除禁止和要求的情况,因为这是不可能的。

这是我尝试的。

Permissions = Union[
    Tuple[Literal[False], Literal[False]],  # forbidden and not required
    Tuple[Literal[True], Literal[False]],   # permitted and not required
    Tuple[Literal[True], Literal[True]]     # permitted and required
    # (No option for forbidden and required)
]

forbidden: Permissions = (False, False)
allowed: Permissions = (True, False)
required: Permissions = (True, True)
mypy_should_catch: Permissions = (False, True)

我希望Mypy允许前三个,但禁止最后一个。

但是,我实际上得到的是所有四个错误的错误:

error: Incompatible types in assignment (expression has type "Tuple[bool, bool]", variable has type "Union[Tuple[Literal[False], Literal[False]], Tuple[Literal[True], Literal[False]], Tuple[Literal[True], Literal[True]]]")

如何使这项工作?

I'm trying to use Python's type checker to catch incompatible permissions. I have a permission situation where a particular action can be permitted (boolean) and required (boolean). Obviously, I want to rule out the situation where it is forbidden and also required, since this is impossible.

Here's what I have tried.

Permissions = Union[
    Tuple[Literal[False], Literal[False]],  # forbidden and not required
    Tuple[Literal[True], Literal[False]],   # permitted and not required
    Tuple[Literal[True], Literal[True]]     # permitted and required
    # (No option for forbidden and required)
]

forbidden: Permissions = (False, False)
allowed: Permissions = (True, False)
required: Permissions = (True, True)
mypy_should_catch: Permissions = (False, True)

I expect mypy to allow the first three, but disallow the last.

However, what I actually get is an error like this for all four:

error: Incompatible types in assignment (expression has type "Tuple[bool, bool]", variable has type "Union[Tuple[Literal[False], Literal[False]], Tuple[Literal[True], Literal[False]], Tuple[Literal[True], Literal[True]]]")

How can I make this work?

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

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

发布评论

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

评论(2

李不 2025-02-03 22:16:18

Mypy 0.941确实只捕获最后一行:

$ mypy test.py
test.py:13: error: Incompatible types in assignment (expression has type "Tuple[bool, bool]", variable has type "Union[Tuple[Literal[False], Literal[False]], Tuple[Literal[True], Literal[False]], Tuple[Literal[True], Literal[True]]]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

因此,您只需要升级即可。

mypy 0.941 does catch the last line only:

$ mypy test.py
test.py:13: error: Incompatible types in assignment (expression has type "Tuple[bool, bool]", variable has type "Union[Tuple[Literal[False], Literal[False]], Tuple[Literal[True], Literal[False]], Tuple[Literal[True], Literal[True]]]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

So it looks like you just need to upgrade.

暖树树初阳… 2025-02-03 22:16:18

最后,我在尝试以类型安全的方式捕获禁止/允许/要求的逻辑方向略有不同。

class Permissions(Enum):
    FORBIDDEN = auto()
    PERMITTED = auto()
    REQUIRED = auto()

    @property
    def permitted(self) -> bool:
        return self is not self.FORBIDDEN

    @property
    def required(self) -> bool:
        return self is self.REQUIRED

但是,我不会将其标记为答案,因为仍然存在一个问题:为什么原始方法无法正常工作。

In the end I went a slightly different direction in trying to capture the logic of forbidden/permitted/required in a type-safe way.

class Permissions(Enum):
    FORBIDDEN = auto()
    PERMITTED = auto()
    REQUIRED = auto()

    @property
    def permitted(self) -> bool:
        return self is not self.FORBIDDEN

    @property
    def required(self) -> bool:
        return self is self.REQUIRED

However, I won't mark this as the answer as there's still the question of why the original approach didn't work as expected.

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