python / python/mypy

SupportsRound[int] hinted functions work with floats if round is called with no additional args

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

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

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

説明

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

this genuinely might be a typeshed issue i'm not 100% sure. To be sure some similar issues about __round__ have cropped up both here and in typeshed:

#6211
python/typeshed#2638

If maintainers think this is solely a typeshed issue and would like me to repost over there please just let me know. If it is a typeshed issue and is a regression ( which it may be) then perhaps I could help by adding a test somewhere?

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

possible bug

Example:
import typing

def round_it(x: typing.SupportsRound[int]) -> int:
    return round(x)

round_it(3.2)
What is the actual behavior/output?

though round is called with no ndigits arg in the example above we still get the following error when running mypy:

scratch.py:11: error: Argument 1 to "round_it" has incompatible type "float"; expected "SupportsRound[int]"
scratch.py:11: note: Following member(s) of "float" have conflicts:
scratch.py:11: note:     Expected:
scratch.py:11: note:         @overload
scratch.py:11: note:         def __round__(self) -> int
scratch.py:11: note:         @overload
scratch.py:11: note:         def __round__(self, ndigits: int) -> int
scratch.py:11: note:     Got:
scratch.py:11: note:         @overload
scratch.py:11: note:         def __round__(self, ndigits: None = ...) -> int
scratch.py:11: note:         @overload
scratch.py:11: note:         def __round__(self, ndigits: int) -> float

note that even if I modify this bit of typeshed to include :

@overload
def __round__(self, ndigits: None = ...) -> int: ...

I still have the same issue ( just with <1 more overload not shown>, etc)

What is the behavior/output you expect?

since SupportsRound is parameterised by return type and I have called the round method with no extra args I would expect that the example provided above would pass.

Specifically :
given both float and SupportsRound[int] more or less share a signature for the def __round__(self) and round is called w/ no args. I would expect mypy to be able to interpret that we're using def __round__(self, ndigits: None = ...) -> int and not care about the conflicting overload since it's not called.

What are the versions of mypy and Python you are using?

python: 3.8.0
mypy: 0.761 (and master)

What are the mypy flags you are using?

I have tried running with each of the following flag sets :

  • no flags
  • --strict
  • --no-implicit-optional
  • --no-strict-optional
  • --strict-optional

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

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

はじめの一歩

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

調査の方向性

提供された scratch.py の例を mypy 0.761 または master で再現し、その後、typeshed の stdlib/2and3/builtins.pyi にあるリンク先の round の定義を調べます。不一致が mypy と typeshed のどちらに属するのかを判断し、関連する issue と動作を比較します。完了とは、担当箇所が確認され、期待される round() 呼び出しが適切なテストまたは引き継ぎによってカバーされていることを意味します。

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

評価

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

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

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