Specifying a default of `None` for `Dict.get()` results in a type of `Any` instead of `Optional[Any]`

Đang mở
#4,359 5 bình luận 1 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
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Start by reproducing the two examples in the issue and inspect mypy's handling of Dict.get() type inference. Trace the relevant type-checking path and add a regression test showing that an explicit None default does not become Any; done means the revealed and return types match the intended Optional behavior.

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

Mô tả

bug priority-1-normal topic-type-variables

Example:

from typing import Dict

def foo(x: Dict) -> str:
    result = x.get('foo', None)
    reveal_type(result)  # E: Revealed type is 'Any'
    return result
from typing import Dict

def foo(x:Dict) -> str:
    result = x.get('foo')
    reveal_type(result)  # E: Revealed type is 'Union[Any, builtins.None]'
    return result  # E: Incompatible return value type (got "Optional[Any]", expected "str")
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

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

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

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.