Bad typing for requests.cookies.RequestsCookieJar

Đang mở Phù hợp với người mới
#15,457 5 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ó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu trong stubs/requests/requests/cookies.pyi tại khai báo RequestsCookieJar ở khoảng dòng 38, sau đó so sánh hành vi iter của nó với chú thích MutableMapping[str, str]. Chạy bản tái hiện được cung cấp bằng pyright và mypy để xác minh rằng việc lặp được định kiểu và hoạt động như các giá trị Cookie. Hoàn thành khi stub không còn cung cấp cho bên gọi kiểu phần tử có thể lặp không chính xác.

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

Mô tả

stubs: false positive topic: collections

RequestsCookieJar is typed as a subtype of both http.CookieJar and MutableMapping[str, str]:

https://github.com/python/typeshed/blob/6ee36bacecd24fca98d080a6965821e7e2d9e590/stubs/requests/requests/cookies.pyi#L38

including a comment ignoring the type error. This is simply wrong; RequestsCookieJar is not a MutableMapping because the __iter__ fn differs.

This causes real problems:

import requests.cookies
import typing

cookiejar = requests.cookies.RequestsCookieJar()
cookiejar["a"] = "b"

print("outside func")
for cookie in cookiejar:
    typing.reveal_type(cookie) # type is Cookie at typecheck and runtime
    print(f"{type(cookie)=}")

def in_func(not_a_cookiejar:typing.Iterable[str]):
    print("in_func")
    for maybe_cookie in not_a_cookiejar:
        typing.reveal_type(maybe_cookie) # type is str at typecheck
        print(f"{type(maybe_cookie)=}") # type is Cookie at runtime

in_func(cookiejar)

pyright 1.1.407 says:

information: Type of "cookie" is "Cookie"
information: Type of "maybe_cookie" is "str"

mypy 1.19.1 says:

bad-types.py:9: note: Revealed type is "http.cookiejar.Cookie"
bad-types.py:15: note: Revealed type is "builtins.str"
Ngôn ngữ chính
Python
Star
5.1k
Fork
2.1k
Merge trung bình
1 ngày 13 giờ
Pull request đã merge (30 ngày)
73

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/typeshed

Tất cả issue của python/typeshed

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.