python / python/mypy

Cannot assert a callable returns `None` when it must return `None`

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

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

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

説明

Bug Report

mypy prevents you making assertions about callables that must return None when they're called.
This is important as not everyone works in a type-checked context, and so library code must be robust to this.
In my case, I have a chain of callables, the ones in the middle take iterables and return iterables, but the one at the start must take no arguments and the one at the end must consume the upstream iterable and return nothing.

To Reproduce

def returns_none() -> None:
    pass

assert returns_none() is None

Expected Behavior

No mypy complaints

Actual Behavior

"returns_none" does not return a value (it only ever returns None) [func-returns-value]

Your Environment

  • Mypy version used: mypy 1.10.1 (compiled: yes)
  • Mypy command-line flags: none
  • Python version used: Python 3.12.1

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

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

はじめの一歩

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

調査の方向性

示されている returns_none 関数とアサーションを使用して、Python 3.12.1 上の mypy 1.10.1 で報告を再現します。func-returns-value 診断を追跡し、その後、None を返すとアノテーションされた callable に関するアサーションでは何も報告されず、不適切な値の使用に対する既存の診断は残ることを確認します。

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

評価

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

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

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