bazel-contrib / bazel-contrib/rules_python
Allow specifying custom stubs for Gazelle
- Ngôn ngữ chính
- Starlark
- Star
- 688
- Fork
- 721
- Merge trung bình
- 15 giờ 7 phút
- Pull request đã merge (30 ngày)
- 76
Mô tả
# 🚀 feature request
### Description
If a library does not provide type stubs, and if there isn't already a type stub library, you might need to write your own stubs (e.g. [kafka-python](https://pypi.org/project/kafka-python/)). Doing so is easy enough:
```py
py_library(
name = "kafka",
imports = ["."],
pyi_srcs = [
"kafka/__init__.pyi",
"kafka/consumer/fetcher.pyi",
"kafka/errors.pyi",
],
visibility = ["//visibility:public"],
)
```
Gazelle currently has a feature to automatically add stubs for a dependency (e.g. automatically add `types-request` whenever `request` is a dependency), but there's no way to register this target as being the type stubs for the `@pypi//kafka` library. Furthermore, adding it manually will remove it without `# keep`
### Describe the solution you'd like
First, in `gazelle_python.yaml`, add a `stub_mapping` section that explicitly maps package to stubs
```yaml
stub_mapping:
requests: types_requests
```
Then instead of [guessing which type packages to include](https://github.com/bazel-contrib/rules_python/blob/bb2aad2d1e3f883c9cdc2264e0b4a2815233db57/gazelle/python/resolve.go#L258-L269), refer to this explicit mapping.
After this refactor, update `modules_mapping` to register additional stubs:
```py
modules_mapping(
name = "modules_map",
include_stub_packages = True,
wheels = all_whl_requirements,
extra_stubs = {
"kafka-python": "//my_stubs:kafka",
},
)
```
This would get added to the new `stub_mapping` section in `gazelle_python.yaml` and everything should Just Work.
### Describe alternatives you've considered
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu với gazelle_python.yaml và logic chọn gói kiểu trong gazelle/python/resolve.go, đặc biệt là các dòng được liên kết. Sau đó, lần theo modules_mapping và các tùy chọn include_stub_packages và extra_stubs của nó. Công việc được hoàn tất khi một stub_mapping tường minh có thể đăng ký các stub tùy chỉnh và Gazelle giữ nguyên rồi sử dụng mapping đó thay vì đoán các gói kiểu.
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
- build-system
- 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