bazel-contrib / bazel-contrib/rules_python

Allow specifying custom stubs for Gazelle

Aperta
#3,199 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Starlark
Stelle
688
Fork
721
Merge medio
15h 7m
PR unite (30g)
76

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start with gazelle_python.yaml and the type-package selection logic in gazelle/python/resolve.go, especially the linked lines. Then trace modules_mapping and its include_stub_packages and extra_stubs options. Done means an explicit stub_mapping can register custom stubs and Gazelle preserves and uses that mapping instead of guessing type packages.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
build-system
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.