python / python/typeshed

Replace `float` with `float | int`

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

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

project: policy
Ngôn ngữ chính
Python
Star
5.1k
Fork
2.1k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
82

Mô tả

As discussed in python/typing#1748: I propose to (slowly) replace float with float | int and complex with complex | float | int, where applicable.

  • It's more correct, even if float is current implicitly equivalent to int | float.
  • We are already prepared should the implicit promotion ever be removed.
  • We give type checkers the ability to optionally disable the promotion so that type checker authors and users can experiment with it.
  • Typeshed would always need to be the first step for this, so let's remove this blocker.

This issue proposes an ugly and slow, but correct and safe procedure:

  1. Disable flake8-pyi code Y041.
    #16096
  2. Introduce two type aliases FloatInt = float | int and ComplexInt = complex | float | int to _typeshed.
    #16068
  3. Globally replace all instances of float with FloatInt and of complex with ComplexInt in the stdlib.
  4. After 2026-09-22 we do the same for third-party stubs.
  5. Review FloatInt and ComplexInt instances are replace them with the proper types. (This will take a while.)
  6. Remove the type aliases once they are not needed anymore (in 10 years or so ...)

Using FloatInt and ComplexInt would be disallowed for new code and the type aliases should explicitly be marked as not to be used by non-typeshed code.

We should also probably add flake8-pyi checks that checks that argument types use float | int and complex | float | int. Cases where only float/complex is allowed are probably rare.

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.

Hướng nghiên cứu

Bắt đầu với các stub của stdlib và các alias FloatInt và ComplexInt trong _typeshed, sau đó kiểm tra cấu hình Y041 của flake8-pyi. Tìm kiếm các annotation float và complex trên toàn bộ stdlib và xem xét từng ứng viên; hoàn thành nghĩa là thực hiện migration stdlib trên toàn bộ phạm vi đã lên kế hoạch, còn các stub của bên thứ ba vẫn được hoãn đến 2026-09-22.

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

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Tái cấu trúc
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

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.