Type inference of generic types: no way to be warned about Generic[Any]
Chưa có ai nhận issue này.
- 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
Mô tả
Usage specs: mypy 0.720 on Python 3.7.3
Bug or feature request: Not really a bug, just a usability inconvenience and potential source of errors
Problem Description
Hello!
I've just realised that a certain way of type checking generic classes (even built-in ones like Sequence, List, Mapping...) can be very error-prone:
There's no way to be warned by mypy when inferring the type of a generic variable which has no explicit type parameter, e.g when inferring the Generic[Any] type through an isinstance(value, Generic) call.
This is error-prone due to the dynamic nature of Any, which allows a Generic[Any] to masquerade as a more specific generic type of the same kind: for instance, a Sequence[Any] variable can be assigned to a variable of Sequence[str] type.
I know that the disallow_any_generics option warns us when we define type hints for generic types with no type parameters: sequence:Sequence = ["hello"] would trigger an error from mypy.
What bothers me is that there is no way to be warned about it when the type is inferred by mypy through an isinstance call.
Minimal Example
Consider the function below, which is supposed to return a Sequence[str], but does not actually ensure that each item inside the sequence is a string.
# coding: utf-8
# mypy: warn_return_any
# mypy: disallow_any_expr
# mypy: disallow_any_generics
from typing import Sequence
def getStringSequence(value:object) -> Sequence[str]:
if isinstance(value, Sequence):
reveal_type(value) # Revealed type is Sequence[Any]
return value
raise TypeError
This example is successfully type checked by mypy, while I would expect it to trigger an error:
In the return statement, mypy allows me to return a Sequence[Any] from the function, when a Sequence[str] was expected instead.
I understand that this is due to the dynamic nature of Any, but given my use of the warn_return_any, disallow_any_expr and disallow_any_generics options, I would expect mypy to warn me about it.
Discussion
Not sure about which combination of these 3 options should trigger an error?
This is all highly debatable, but at a glance, I'd say that:
- When
warn_return_anyis True, mypy should warn us whenever we return aGeneric[Any], even if it is merely inferred and not explicitly typed through an hint. In the above code example, an error would be raised in the return statement. - When
warn_any_expris True, mypy should warn us whenever we use aGeneric[Any], even if it is merely inferred. I assume the error would be raised on the first statement to use the variable whose type was inferred. In the above example, an error would be raised in the return statement again. - Both of these behaviours should be enabled only when
disallow_any_genericsis True as well.
To provide more context, my use case is type checking arbitrary data structures loaded from declarative formats like JSON and YAML, hence my reliance on type inference over type hints.
I realise it's possible to correct this function's return statement easily: return tuple(item for item in value if isinstance(item, str))
But I still find the risk of accidentally having Any masquerade as a more specific type parameter fairly worrying, so it'd be great to have a way to be warned about it.
Thanks for reading, hope I was clear enough.
Many, many thanks for your collective hard work on mypy!
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với ví dụ Python tối giản trong issue và tái hiện kiểu Sequence[Any] được chỉ ra bằng cách sử dụng warn_return_any, disallow_any_expr và disallow_any_generics. Xem xét cách mypy xử lý các kiểu generic được suy luận và các chẩn đoán cho những tùy chọn này, sau đó xác định và kiểm thử hành vi cảnh báo dự kiến cho câu lệnh return.
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
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100