python / python/typeshed

Unify typing for `__path__`?

Aperta
#6,650 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

After the last mypy update v0.920, I started to notice an inconsistency with how the module variable __path__ is typed mainly across typeshed, mypy, and pyright.

Type Link
typeshed - ModuleType MutableSequence[str] https://github.com/python/typeshed/pull/6200
typeshed - pkgutil.extend_path list[str] https://github.com/python/typeshed/pull/5222
mypy list[str] https://github.com/python/mypy/pull/9454
pyright Iterable[str] https://github.com/microsoft/pylance-release/issues/1098

In the Python docs it's mentioned as:

`__path__` must be an iterable of strings, but it may be empty.

However the docs for pkgutil.extend_path go on say it's mostly a list (?) cpython/pkgutil.py

    If the input path is not a list (as is the case for frozen
    packages) it is returned unchanged.  The input path is not
    modified; an extended copy is returned.  Items are only appended
    to the copy at the end.

From my point of view, it isn't entirely clear what the "correct" type should be. Some points to consider though:

  • In most cases it seems to be a list[str].
  • It was mentioned here however that it can sometimes also be a MutableSequence[str]
  • The __path__ type should be valid as first argument to pkgutil.extend_path. At the moment that only applies to list[str] but obviously the argument type for extend_path could be changed to, for example, accept a TypeVar bound to Iterable[str] instead.
  • It seems to be fairly common (and safe ?) to access the at least the first item of __path__ via __getitem__.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia confrontando le annotazioni di typeshed ModuleType e pkgutil.extend_path con la documentazione di Python sui percorsi dei moduli e pkgutil, quindi esamina le discussioni collegate di mypy e pyright. Determina un contratto di tipi coerente che rifletta il comportamento documentato e osservato. Il lavoro è completato quando le annotazioni rilevanti concordano tra gli stub interessati e rimangono compatibili con extend_path.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Refactoring
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
20/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.