bazel-contrib / bazel-contrib/rules_python

Allow specifying custom stubs for Gazelle

未关闭
#3,199 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Starlark
星标
688
派生
721
平均合并
15 小时 7 分钟
30 天内合并 PR
76

描述

# 🚀 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

贡献指南

打开贡献指南

调研方向

从 gazelle_python.yaml 和 gazelle/python/resolve.go 中的类型包选择逻辑开始,尤其关注链接的行。然后跟踪 modules_mapping 及其 include_stub_packages 和 extra_stubs 选项。完成的标准是:显式的 stub_mapping 可以注册自定义 stub,并且 Gazelle 会保留并使用该映射,而不是猜测类型包。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
build-system
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。