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

未关闭
#8,213 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
devtools

调研方向

使用 mypy 0.761 或 master 重现提供的 scratch.py 示例,然后检查 typeshed stdlib/2and3/builtins.pyi 中链接的 round 定义。确定该不匹配属于 mypy 还是 typeshed,并将其行为与相关 issue 进行比较。完成的标准是已确认归属方,并且预期的 round() 调用已由适当的测试或交接覆盖。

由索引模型根据 Issue 内容生成。

描述

bug

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
主要语言
Python
星标
20.6k
派生
3.3k
平均合并
1 天 18 小时
30 天内合并 PR
54

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。