Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Type checking unexpectedly passing when misusing `default` and `converter`

Đang mở
#604 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Bug Typing

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:

image

Which is one error less than expected.

Should I perhaps report this to MyPy or Typeshed as well/instead?

Ngôn ngữ chính
Python
Star
5.8k
Fork
480
Merge trung bình
2 giờ 15 phút
Pull request đã merge (30 ngày)
2

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-attrs/attrs

Tất cả issue của python-attrs/attrs

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.