Variable assigned in branches of an if statement is considered as being redefined
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
The same variable assigned in different branches of an if statement is considered as being redefined, despite the fact that at in reality there is only one assignment being performed, it's just unknown at that stage which branch will be taken.
To Reproduce
from io import BytesIO
from typing import ClassVar
class IntegerAdapter:
_size_: ClassVar[int] = 4
@classmethod
def from_wire(cls, buffer: bytes | bytearray | memoryview | BytesIO) -> int:
if isinstance(buffer, BytesIO):
data = buffer.read(cls._size_)
else:
data = buffer[:cls._size_]
return int.from_bytes(data, byteorder='big', signed=True)
Expected Behavior
I would expect mypy to infer the type of the variable as the union of the types in all branches. This is not really a redefinition since the variable is not assigned twice in succession, only one branch is taken, it's just unknown which one.
Using --allow-redefinition doesn't help as one of the requirements for that is to read the variable before the next assignment, which is impossible.
Also the behavior is inconsistent with different ways of writing the if statement. Inverting the condition and reversing the branches makes the error go away. So does using an equivalent conditional expression.
Any of the following ways to rewrite the if statement will eliminate the error, but one should not have to retort to such gimmicks to avoid this.
# this works because the 1st assignment is the one with a wider type that includes the type of the 2nd
if not isinstance(buffer, BytesIO):
data = buffer[:cls._size_]
else:
data = buffer.read(cls._size)
# this works because the type is correctly inferred to be the union of the branch types
data = buffer.read(cls._size_) if isinstance(buffer, BytesIO) else buffer[:cls._size_]
IMO the conditional statement is the only one that behaves correctly here as it infers the type as the union of the two.
But semantically the if-else and the conditional statement are the same, just differently written: take one branch and assign computed value to the variable.
Actual Behavior
typing-6.py:14: error: Incompatible types in assignment (expression has type "bytes | bytearray | memoryview", variable has type "bytes") [assignment]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 (compiled: no)
- Mypy command-line flags: with or without --allow-redefinition, makes no difference
- Mypy configuration options from
mypy.ini(and other config files):
[tool.mypy]
enable_incomplete_feature = "NewGenericSyntax"
disable_bytearray_promotion = true
disable_memoryview_promotion = true
check_untyped_defs = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
- Python version used: 3.12
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 bằng cách chạy reproducer Python được cung cấp với mypy và cấu hình đã cho, so sánh dạng if-else với biểu thức điều kiện tương đương. Theo dõi cách các phép gán trong các nhánh được kiểm tra và làm cho trường hợp if-else suy luận union mà không báo cáo việc định nghĩa lại không tương thích; xác minh rằng reproducer hoàn tất mà không có lỗi.
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
- tooling
- 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
- 45/100