python / python/typeshed

Incompatible typing for `logging.Formatter.converter` and `time.gmtime`/`time/localtime` ?

Đang mở
#14,880 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.

stubs: false positive
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ả

The typing for converter here is as follows:

converter: Callable[[float | None], struct_time]

And the typing for time.gmtime/time.localtime here is:

def gmtime(seconds: float | None = None, /) -> struct_time: ...
def localtime(seconds: float | None = None, /) -> struct_time: ...

Yet when I try to typecheck this code:

import logging
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

I get the following error:

$ mypy main.py
main.py:5: error: Incompatible types in assignment (expression has type "Callable[[], struct_time]", base class "Formatter" defined the type as "Callable[[float | None], struct_time]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

I tested this on a fresh python3.13 venv with the following deps installed:

mypy==1.18.2
mypy_extensions==1.1.0
pathspec==0.12.1
typing_extensions==4.15.0

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 stdlib/logging/init.pyi và stdlib/time.pyi tại các định nghĩa được tham chiếu, sau đó tái hiện phép gán bằng mypy với ví dụ trong issue. Kiểm tra lý do hai chú thích callable bị coi là không tương thích và cập nhật phần kiểu hóa stub liên quan để ví dụ được kiểm tra kiểu mà không có lỗi phép gán.

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
devtools
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
50/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.