python / python/mypy

Property return type not inferred when using `field_name = property(func)`

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

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

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

説明

Bug Report

When using the field_name = property(func) syntax, the return type is not correctly inferred.
For descriptors however, that seems to work fine, so I assume this is a bug.
(Sorry if it has been reported before, I couldn't find anything related.)

To Reproduce

from __future__ import annotations
from typing import Any

def func1(self: Any) -> int:
    return 3

def func2(self: object) -> int:
    return 3

def func3(self: Test) -> int:
    return 3

class Test:
    def func4(self) -> int:
        return 3

    test1 = property(func1)
    test2 = property(func2)
    test3 = property(func3)
    test4 = property(func4)

reveal_type(Test().test1)
reveal_type(Test().test2)
reveal_type(Test().test3)
reveal_type(Test().test4)

# Output:
/tmp/test.py:27: note: Revealed type is "Any"
/tmp/test.py:28: note: Revealed type is "Any"
/tmp/test.py:29: note: Revealed type is "Any"
/tmp/test.py:30: note: Revealed type is "Any"
Success: no issues found in 1 source file

Expected Behavior

I'd expect mypy to understand the property return types.

It also seems that there is no way around this.
The following gives "Incompatible types in assignment":

class Test:
    test4: int = property(func4)

and we can't do property[int] as if it was a generic. (Shouldn't property be a generic in typeshed?)

Actual Behavior

We're getting Any instead.

Your Environment

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

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

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

はじめの一歩

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

調査の方向性

レポートにはリポジトリのファイルもテストも記載されていません。まず、提供された Python の再現コードを mypy 0.981 で実行し、property/descriptor の処理と typeshed の property 定義を調べてください。完了条件は、互換性のない代入なしに、reveal_type が各 property の int 戻り値型を報告することです。

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

評価

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

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

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