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

Mypy type checking does not work with enums

未关闭
#616 2 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

首先,使用受影响的 betterproto 版本对提供的 test-mypy.py 复现代码运行 mypy,然后检查与 #293 和 #540 相关的枚举类型标注更改。当 mypy 将 Colour.RED 和 Colour.try_value(Colour.RED) 视为 Colour 值,并且示例在没有报告的 argument-type 错误的情况下通过时,即表示完成。

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

描述

bug low priority
Summary

When using an enum in a code project mypy is broken and has the impression that all the enum fields are of type 'int'

Reproduction Steps

Run type checking on this file and see the problem in version 2.0.0b7

import betterproto

class Colour(betterproto.Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

def print_color(color: Colour) -> None:
        print(color.value)

def test_mypy_types() -> None:
    print(Colour.RED == Colour.try_value(Colour.RED))

    print_color(Colour.RED)
    print_color(Colour.try_value(Colour.RED))


if __name__ == '__main__':
    test_mypy_types()

Expected Results

I would have expected the type checking to pass as in version 2.0.0-beta6

This is caused by #293 (and is not solved by #540)

The expected type of Colour.RED is a Colour not an int.

Actual Results
$ poetry run mypy test-mypy.py
test-mypy.py:16: error: Argument 1 to "print_color" has incompatible type "int"; expected "Colour"  [arg-type]
        print_color(Colour.RED)
                    ^~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)
System Information
Python 3.11.9
Name: betterproto
Version: 2.0.0b7
Summary: A better Protobuf / gRPC generator & library
Home-page: https://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: [email protected]
License: MIT
Location: /home/jns/product/.venv/lib/python3.11/site-packages
Requires: grpclib, python-dateutil, typing-extensions
Required-by: inventory_code_project_enumeration, inventory_name_inference, inventory_py_schema_description, inventory_py_schema_endpoints_extraction, inventory_py_schema_services_extraction, inventory_repository_analysis, inventory_repository_clone, inventory_repository_extract_schema, inventory_repository_extract_secret, inventory_repository_extract_service, inventory_repository_owner_search, inventory_schema_analysis, inventory_schema_classification, inventory_schema_conversion, inventory_schema_data_leaks, inventory_service_description, inventory_software_types_fingerprinter, protocol
Checklist
  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.
主要语言
Python
星标
1.8k
派生
235
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

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

danielgtaylor/python-betterproto 的其他 Issue

查看 danielgtaylor/python-betterproto 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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