python / python/typeshed

function is lacking a `__signature__` attribute

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

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

主要言語
Python
スター
5.1k
フォーク
2.1k
平均マージ
1日 19時間
マージ済み PR(30日)
82

説明

PEP-362 specifies that in order to cache the signature of any function, it is possible for the user to set its __signature__ attribute.

Actual

This attribute is not set in the builtins module over at https://github.com/python/typeshed/blob/main/stdlib/builtins.pyi#L1009

As a result a typechecker might reject the following piece of code

from inspect import signature


def main():

    def f():
        pass

    f.__signature__ = signature(f)


if __name__ == "__main__":
    main()

e.g. with pyright I have a false positive

/Users/leogermond/Lab/python/signature/main.py
  /Users/leogermond/Lab/python/signature/main.py:9:7 - error: Cannot assign to attribute "__signature__" for class "function"
    Attribute "__signature__" is unknown (reportFunctionMemberAccess)
1 error, 0 warnings, 0 informations

Expected

I would the __signature__ attribute to be specified in the function class (with a special marker indicating it's optional?) of the builtins module. A type checker such as pyright could then infer that setting this attribute is possible and accept the piece of code given in example.

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

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

はじめの一歩

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

調査の方向性

stdlib/builtins.pyi の 1009 行目付近から始め、function class がどのように宣言されているかを確認します。signature に対して要求されているオプションのサポートを、PEP 362 および提供された代入例と比較し、そのうえで、stub の変更が完了したときに pyright などの型チェッカーがそれを受け入れることを確認します。

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

評価

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

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

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