Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Incorrect narrowing of Enum.Flag

未关闭
#21,937 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
65/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
compilers

调研方向

首先,使用提供的 Python 代码片段和 mypy 配置重现报告的 Enum.Flag narrowing 示例。跟踪与 Programs.NONE 和 Programs.P2 比较后执行的 narrowing,然后为 Programs.P1 in programs 之类的成员检查添加回归测试。当该示例不产生 unsupported-operand 警告时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

bug topic-enum topic-type-narrowing

Bug Report

Incorrect narrowing of Enum.Flag

To Reproduce

from enum import Flag, auto
from pathlib import Path
from tempfile import NamedTemporaryFile


def p1(path: str) -> None: ...
def p2(path: str) -> None: ...
def p2_fast(data: str) -> str: ... # type: ignore[empty-body]
def p3(path: str) -> None: ...


class Programs(Flag):
    NONE = 0
    P1 = auto()
    P2 = auto()
    P3 = auto()
    ALL = P1 | P2 | P3


def f(data: str, programs: Programs) -> str:
    if programs == Programs.NONE:
        return data
    if programs == Programs.P2:
        return p2_fast(data)
    with NamedTemporaryFile("w", encoding="utf-8", delete=False) as tmp:
        tmp.write(data)
        tmp.close()
        if Programs.P1 in programs:
            p1(tmp.name)
        if Programs.P3 in programs:
            p3(tmp.name)
        if Programs.P2 in programs:
            p2(tmp.name)
        return Path(tmp.name).read_text("utf-8")

Expected Behavior

no warning

Actual Behavior

main.py:32: error: Unsupported operand types for in ("Programs" and "Literal[Programs.P1, Programs.P3, Programs.ALL]") [operator]

Your Environment

  • Mypy version used: 2.3.1
  • Mypy command-line flags: no
  • Mypy configuration options from mypy.ini (and other config files): no
  • Python version used: 3.14
主要语言
Python
星标
20.6k
派生
3.3k
平均合并
1 天 3 小时
30 天内合并 PR
59

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。