python / python/mypy

Decorator becomes untyped when it accepts an object of the same type as a certain global.

オープン
#14,005 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

A decorator becomes untyped when it accepts an argument for which we have another instance, globally defined and referred to in the same scope.

The comments in the example should make the bug much more clear.
I had no idea how to describe this issue or search for it, so no idea whether it was reported before.

To Reproduce

from typing import Callable


class Something:
    pass


def decorator(buffer: Something) -> Callable[[Callable[[], None]], Callable[[], None]]:
    return lambda x: x


def main() -> None:
    something  # Comment out this line and everything is fine.
    another_something = Something()

    @decorator(another_something)
    def func() -> None:
        return


something = Something()  # Move this above `main` and everything is fine.

Expected Behavior

The code snippet should pass.

Actual Behavior

 $ mypy --strict /tmp/test.py
test.py:16: error: Untyped decorator makes function "func" untyped
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.982
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.13

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、mypy --strict を使ってスニペットを再現し、グローバルな something を削除した場合、または main より上に移動した場合の挙動を比較します。この例に関係するデコレーターの型推論と名前解決の経路を追跡します。元のスニペットが、型付けされていないデコレーターを報告せずに strict チェックを通過すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。