python / python/mypy

Guidance on how to write a `mypy` plugin to workaround limitations related to `MetaPathFinder` / `PathEntryFinder` instances

Aperta
#16,988 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature

It would be nice if mypy docs contains guidance/examples on how to write a mypy plugin that works around the limitations related to the use of MetaPathFinder/PathEntryFinders.

I suppose this FR also entails adding hooks that allows plugins to "redirect imports" to different files.
Maybe something like get_module_path_hook? Or even better, something that allows MetaPathFinder / PathEntryFinder instances themselves to be re-used?

If we can at least tell mypy where to find the files for certain module names, that would be a huge improvement.

Pitch

Understandably MetaPathFinder/PathEntryFinder are very dynamic aspects of the Python machinery implementation that cannot be easily reconciled with static analysis tools.

However, these are still very important parts of the Python programming language. Therefore, it would be nice if we can write plugins that workaround some of the limitations of mypy on handling these finders.

The following are a couple of examples of finders used in the wild:

  1. setuptools.extern/pkg_resources.extern
    • This is the motivation that brought me here today (there has been a lot of effort recently on type checking setuptools, so eventually we could retire the typeshed stub), and it is been a challenge to work with setuptools.extern.
  2. More broadly, MetaPathFinder/PathEntryFinder are mentioned in PEP 660 as a viable solution for editable installs[^1].

[^1]: Indeed this strategy is somewhat praised in the text[^2]:
> The proxy strategy can achieve a higher level of fidelity than path-based methods.
[^2]: Note that the use of the term "proxy" is associated with the strategy used by the editables project, which is a MetaPathFinder: https://github.com/pfmoore/editables/blob/c8cf40778c829ec434537c131fc866c262bd6bbd/src/editables/redirector.py#L11. So I think it is safe to assume that the mention/praise in PEP 660 refers to use of finders.

Would it be possible to have something like mypy.plugin.Plugin.register_finder(finder: MetaPathFinder) or mypy.plugin.Plugin.get_module_path_hook(fullname: str) -> str | os.PathLike[str] | None?

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

La issue cita mypy.plugin.Plugin e i casi MetaPathFinder/PathEntryFinder in setuptools.extern e pkg_resources.extern. Inizia esaminando l’API del plugin e la documentazione esistente sulla gestione degli import, quindi confronta le indicazioni proposte o il comportamento dell’hook con questi esempi di finder. Il lavoro è completato quando il comportamento supportato e gli esempi sono documentati e qualsiasi entry point del plugin richiesto è specificato chiaramente.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.