python / python/mypy

reachable code reported as unreachable using __init__ vs Protocol vs decorators

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

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

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

説明

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

This code decorates init with typeguard, which gives the decorated init a wrapped attribute referring to the original init. This code attempts to unwrap that to get to the original init definition (in my real world use case, to inspect the argument definitions for constructing an instance of a class). At runtime, a Protocol can be used to detect if init has a __wrapped__ attribute, but this does not match up with what mypy believes.

from typing import cast, Any
from typing_extensions import Protocol, runtime_checkable
import typeguard

class Foo():

  @typeguard.typechecked
  def __init__(self, x: int, y: int,z: int = 3):
    self.x = x
    self.y = y
    self.z = z
    print("init")

f = Foo(9,10)
print(f)

func = type(f).__init__
print("__init__ func is {}".format(func))

if hasattr(func, '__wrapped__'):
  print("Path 1: is wrapped according to hasattr")
  print(cast(Any, type(f).__init__).__wrapped__)
else:
  print("Path 1: is not wrapped")


@runtime_checkable
class W(Protocol):
  __wrapped__: object

if isinstance(func, W):
  print("Path 2: is wrapped according to Protocol - mypy says this is unreachable")
  print(type(f).__init__.__wrapped__)
else:
  print("Path 2: is not wrapped")
  • What is the actual behavior/output?
    Execution prints my code's message: Path 2: is wrapped according to Protocol - mypy says this is unreachable from line 32.
    mypy reports: pp.py:32: error: Statement is unreachable for that print statement.

Mypy is reporting a statement as unreachable which is actually reached.

  • What is the behavior/output you expect?
    Mypy should not report that line as unreachable.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

I have tried mypy 0.770 and git master:

$ mypy --version
mypy 0.770+dev.52c0a63f4604a2bfa8c0edd6589ab25fab4bdaec
  • What are the mypy flags you are using? (For example --strict-optional)

$ mypy pp.py --warn-unreachable

with no mypy.ini

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

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

はじめの一歩

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

調査の方向性

pp.py の再現から始め、示されている Protocol、デコレーター、wrapped のチェックを使って mypy pp.py --warn-unreachable を実行します。実行時の出力を mypy の到達不能文の診断と比較します。到達した Protocol ブランチが到達不能として報告されなくなれば完了です。

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

評価

技術スタック
python
領域
compilers, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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