python / python/mypy

Mypy reports a function is "missing return statement" when unused imports are removed

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

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

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

説明

I know this sounds ridiculous, but I'm not sure how else to explain it. In the middle of a large refactor of code, which can be found here in its original, mypy-passing state:

https://github.com/mesonbuild/meson/blob/83d18d137dc50437a8263983e8883198c7bc41ed/mesonbuild/compilers/detect.py#L272-L352

I have a function which looks like this:

def _handle_exceptions(data: StuffICareAbout) -> typing.NoReturn:
    raise mesonlib.EnvironmentException(f'{data} happened and this is bad')

def detect_static_linker(env: 'Environment', compiler: Compiler) -> StaticLinker:
    data = do_stuff()
    if foo_condition:
        return StaticLinkerA()
    elif bar_condition:
        return StaticLinkerB()
    _handle_exceptions(data)

mypy says this is fine. detect_static_linker() doesn't always return, but if it doesn't return, it runs a function that is guaranteed to never return, always raise.

This somehow stops working during the course of this series of changes, which doesn't affect the high-level overview of that function: https://github.com/eli-schwartz/meson/compare/83d18d137dc50437a8263983e8883198c7bc41ed...681609624b687d660b234a9393d446a8f2f6663b

After way too much troubleshooting, and separating out parts of the commit to see what caused it, I produced the above series of commits, and...

... this commit is fingered by git bisect: https://github.com/eli-schwartz/meson/compare/fe42a1cadade94cb39e2761500a1f5849ad7d2e8...b1676e57f60c6a1fb7533096402ae5c8ff713a35

All it does is finally drop the unused imports rendered redundant by the previous commit. I don't understand how this could be possible.

Furthermore, if I run mypy mesonbuild/compilers/detect.py, mypy passes. If I run ./run_mypy.py, a script that contains a list of files to check, and runs mypy "list" "of" "files", it triggers the issue.

Discussion in the meson review can be seen here, in case that's interesting (two people failed to reproduce it locally at first): https://github.com/mesonbuild/meson/pull/10810#discussion_r970164045

Your Environment

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

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

はじめの一歩

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

調査の方向性

まず、Python 3.10、mypy 0.971、および .mypy.ini を使用して、mypy mesonbuild/compilers/detect.py./run_mypy.py の違いを再現します。参照されているリビジョンを比較し、mesonbuild/compilers/detect.pyrun_mypy.py を調査します。未使用の import を削除すると集約チェックが変わる理由を説明し、一貫性のない診断を修正できれば完了です。

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

評価

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

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

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