python / python/typeshed

Adding Overloads of stdlib Built-in Types __new__ with No Arguments

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

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

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

説明

Calling int() and str() with no arguments returns 0 and "" respectively, likewise for bool() == False and bytes() == b"". But to actually call these constructors without arguments is very unusual and is likely a mistake on part of the user.

# a and b are two strings
x = int(a)
y = int()    # typo, should be int(b)

x / y    # ZeroDivisionError

It will be helpful for type stubs to denote that such no-argument calls return a constant value, and for IDEs to possibly notify of such use.

Currently the stub for int.__new__ looks like this:

https://github.com/python/typeshed/blob/904ea9ec022ae75d282f6a4c34563e4e9b8eed15/stdlib/builtins.pyi#L257

class int:
    @overload
    def __new__(cls, x: ConvertibleToInt = 0, /) -> Self: ...

and str.__new__:

class str(Sequence[str]):
    @overload
    def __new__(cls, object: object = "") -> Self: ...

I suggest adding an overload with no argument and removing the default parameter value from the one-arg __new__, though I'm not sure if there are existing special behaviors of type checkers regarding builtins that I'm unaware of.

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

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

はじめの一歩

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

調査の方向性

stdlib/builtins.pyi から始め、特に issue でリンクされている int.new と str.new の定義を確認し、対応する組み込みコンストラクターの bool と bytes のスタブを調べてください。既存の型チェッカーがデフォルトパラメーターと個別の overload をどのように扱っているかを確認し、そのうえで、スタブが引数なしの呼び出しと引数ありの呼び出しを区別し、結果として推論される定数値を検証するようにしてください。

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

評価

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

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

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