Type checking unexpectedly passing when misusing `default` and `converter`
还没有人认领这个 Issue。
评估
调研方向
Start by running the Python 2/3 reproducer and checking the reported MyPy diagnostics for A, especially the y attribute using default=None and _convert_int. Trace whether the missing error belongs to attrs' generated typing information or to MyPy/Typeshed, and consider the issue done when the ownership and a regression check for this case are established.
由索引模型根据 Issue 内容生成。
描述
I was playing around with type annotations and Attrs, and I think I may have found an issue.
Given the following Python 2/3 script:
from __future__ import absolute_import
from pprint import pprint as pp
import attr
def _convert_int(val):
# type: (int) -> int
if val <= 0:
return -1
return val
@attr.s
class A(object):
x = attr.ib(default=0, converter=_convert_int, type=int)
y = attr.ib(default=None, converter=_convert_int, type=int) # `default` is incompatible with our declared `type`
def main():
# type: () -> None
a1 = A(5, 5) # correct: type checks
pp(a1)
a2 = A(5, None) # correct: doesn't type check
pp(a2)
a3 = A(5) # incorrect: type checks but shouldn't!
pp(a3)
if __name__ == '__main__':
main()
I'd expect MyPy to complain about the relationship between default and converter for y. Nevertheless, I'm getting:

Which is one error less than expected.
Should I perhaps report this to MyPy or Typeshed as well/instead?
- 主要语言
- Python
- 星标
- 5.8k
- 派生
- 480
- 平均合并
- 2 小时 15 分钟
- 30 天内合并 PR
- 2
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-attrs/attrs 的其他 Issue
-
难度 5/5 一周以上 新手友好度 20/100
python-attrs/attrs#1620 ·
-
难度 3/5 1-2 天 新手友好度 55/100
python-attrs/attrs#1596 · 2 条评论 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 35/100
python-attrs/attrs#1549 · 3 条评论 ·
-
难度 4/5 3-5 天 新手友好度 38/100
python-attrs/attrs#1543 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
python-attrs/attrs#1532 ·
查看 python-attrs/attrs 的全部 Issue
相似的 Issue
-
area: harness bug status: needs-triage
难度 2/5 1-3 小时 新手友好度 75/100
Human-Agent-Society/reef#625 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 1/5 1 小时以内 新手友好度 80/100
learningequality/kolibri#15351 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
Name consistency 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
eellak/triplestore#65 · 1 条评论 ·