python / python/mypy

Dotted decorators on `TypedDict` classes fails for a certain package layout

未关闭
#17,653 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

Bug Report, To Reproduce, & Actual Behaviour

I'm hitting a strange error in data_structures.py with the following project structure, with a potential culprit in runtime_validation.py:

Project/
└── package/
    ├── __init__.py
    ├── data_structures.py
    └── runtime_validation.py
  • Contents of data_structures.py
    import typing as tp
    import package.runtime_validation as _runtime_validation
    # from package.runtime_validation import register_runtime_validator  # Uncomment to make errors disappear
    
    deco = _runtime_validation.register_runtime_validator  # OK
    
    @_runtime_validation.register_runtime_validator  # OK
    class A: ...
    
    @_runtime_validation.register_runtime_validator  # E: Module has no attribute "register_runtime_validator"
    class TD(tp.TypedDict): ...
    
  • Contents of __init__.py
    import typing as tp
    
    RUNTIME_VALIDATORS: tp.Final = ...
    
    from package.data_structures import TD
    
  • Contents of runtime_validation.py
    import typing as tp
    
    from package import RUNTIME_VALIDATORS  # Comment out to make errors in `data_structures.py` disappear
    
    def register_runtime_validator(Type: type[tp.Any], /) -> type[tp.Any]:
        ...
        return Type
    

Expected Behavior

No issues

Your Environment

  • Mypy version used: 1.11.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10.11

贡献指南

打开贡献指南

从这里开始

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

调研方向

使用 data_structures.py、init.py 和 runtime_validation.py 中所示的软件包布局和内容,通过 mypy 1.11.1 重现该问题。首先跟踪导入循环如何影响 TypedDict 类上点号装饰器的属性查找。无需取消直接导入的注释,且示例不再报告缺少属性错误,即表示完成。

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

评估

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

把新 issue 发到你的邮箱

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