bazel-contrib / bazel-contrib/rules_python

Allow specifying custom stubs for Gazelle

オープン
#3,199 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Starlark
スター
688
フォーク
721
平均マージ
15時間 7分
マージ済み PR(30日)
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 でカスタムスタブを登録でき、Gazelle が推測するのではなく、そのマッピングを保持して使用できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
build-system
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。