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 類別上點號 decorator 的屬性查找。不取消直接匯入的註解,且範例不再回報缺少屬性錯誤,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
devtools
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。