=| and &= allowed for immutable set ABC
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug Report
mypy seems to allow |= and &= for collections.abc.Set, which is an immutable set type. Its documentation declares that it doesn't offer the __ior__ and __iand__ methods. Indeed, mypy rejects the use of these methods directly, but it does allow |= and &=.
A consequence of this is that because collections.abc.Set is covariant in its element type, breaking out of its immutability allows breaking the Liskov substitution principle.
I've checked both the collections.abc and the typeshed implementation of the relevant types and as far as I can make out they seem to be defined in line with the documentation, so I assume this is a mypy issue, but I could be wrong about that.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=ca538f567c30d410778f87454f9d3494
import collections.abc
from typing import TYPE_CHECKING
myset: collections.abc.Set[int] = {1}
if TYPE_CHECKING:
reveal_type(myset)
alias: collections.abc.Set[int] = myset
myset |= {2}
# proof that the set object has been modified despite being declared immutable
assert alias == {1, 2}
# Consequence: break Liskov
class Parent: pass
class Child(Parent): pass
def f(s: collections.abc.Set[Parent]) -> None:
# s should be immutable and yet we can extend it
s |= {Parent()}
children: collections.abc.Set[Child] = {Child()}
# mypy allows this because the immutable `Set` is covariant in its element type.
# This is safe because, it being immutable, the method can not add elements to it.
# Except that with this bug it can, adding an `Parent` to a `Set[Child]`
f(children)
if TYPE_CHECKING:
reveal_type(children)
# This should never happen
assert any(isinstance(child, Parent) for child in children)
Expected Behavior
I expected mypy to reject |= and &= for collections.abc.Set and only allow it for collections.abc.MutableSet.
Actual Behavior
Mypy finds no issues with the snippet:
main.py:6: note: Revealed type is "typing.AbstractSet[builtins.int]"
main.py:27: note: Revealed type is "typing.AbstractSet[__main__.Child]"
Success: no issues found in 1 source file
Your Environment
- Mypy version used: 0.931 locally, 1.0.0 on mypy-play.net
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.9 locally, 3.11 on mypy-play.net
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 bản tái hiện được liên kết trên mypy-play và theo dõi cách các phép gán tăng cường được kiểm tra đối với collections.abc.Set so với MutableSet. Công việc được hoàn tất khi |= và &= bị từ chối đối với Set bất biến nhưng vẫn được cho phép đối với MutableSet, kèm theo kiểm thử hồi quy cho các ví dụ đã được báo cáo.
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ó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- 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
- 42/100