With v1.4 module-global variables become fixed in type.
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ả
This seems to be a regression with v1.4 of mypy.
It looks to me like globals are considered constant if they’re not explicitly typed. For example (see issue https://github.com/Bogdanp/dramatiq/issues/555) a module-global variable like
global_broker = None
seems to be typed now as None. Consequently reading that variable and then asserting its value is always false and therefore raises an exception
broker = some.module.global_broker
assert broker is not None # Previously this worked; now it's considered None and therefore raises.
and that means that the next statement
broker.enqueue(message, delay=delay)
becomes unreachable.
I therefore changed the module-global variable declaration to
global_broker: Optional["Broker"] = None
which solved the problem. However, that’s in an external module and therefore my own code is now broken after updating to v1.4 of mypy.
Would it make sense to consider the type for such a module-global variable as Any | None (or just Any) instead of just None? (Assuming that a module-global None variable will change.)
To Reproduce
See above.
Expected Behavior
As with v1.3 I would have expected mypy to pass the type check.
Actual Behavior
With v1.4 mypy now flags the statement following the assert as unreachable.
Your Environment
- Mypy version used: mypy 1.4.1
- Mypy configuration options from
mypy.ini(and other config files):[tool.mypy] show_error_codes = true show_column_numbers = true check_untyped_defs = true incremental = false strict_equality = true warn_return_any = true warn_redundant_casts = true warn_unreachable = true warn_unused_configs = true warn_unused_ignores = true disallow_untyped_calls = true disallow_untyped_defs = true disallow_incomplete_defs = true disallow_untyped_decorators = true disable_error_code = [] plugins = "sqlalchemy.ext.mypy.plugin" - Python version used: 3.10.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 với reproducer tối thiểu ở cấp module trong issue và so sánh mypy 1.3 với 1.4.1 bằng cấu hình được liệt kê, đặc biệt là warn_unreachable và strict_equality. Được xem là hoàn tất khi hành vi của một biến global không được chú thích, được khởi tạo bằng None, đã được xác định và được bao phủ bởi một regression test, bao gồm assert và lệnh gọi phương thức sau đó.
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
- compilers
- 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
- 35/100