Guidance on how to write a `mypy` plugin to workaround limitations related to `MetaPathFinder` / `PathEntryFinder` instances
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beschreibung
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:
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 withsetuptools.extern.
- This is the motivation that brought me here today (there has been a lot of effort recently on type checking
- More broadly,
MetaPathFinder/PathEntryFinderare 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?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Das Issue nennt mypy.plugin.Plugin sowie die Fälle für MetaPathFinder/PathEntryFinder in setuptools.extern und pkg_resources.extern. Beginne mit der Prüfung der Plugin-API und der vorhandenen Dokumentation zur Importbehandlung und vergleiche anschließend die vorgeschlagenen Hinweise oder das Verhalten des Hooks mit diesen Finder-Beispielen. Erledigt ist die Aufgabe, wenn das unterstützte Verhalten und die Beispiele dokumentiert sind und ein eventuell erforderlicher Plugin-Einstiegspunkt eindeutig angegeben ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100