Guidance on how to write a mypy
plugin to workaround limitations related to MetaPathFinder
/ PathEntryFinder
instances
#16988
Labels
Feature
It would be nice if
mypy
docs contains guidance/examples on how to write amypy
plugin that works around the limitations related to the use ofMetaPathFinder
/PathEntryFinder
s.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 allowsMetaPathFinder
/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
setuptools
, so eventually we could retire the typeshed stub), and it is been a challenge to work withsetuptools.extern
.MetaPathFinder
/PathEntryFinder
are mentioned in PEP 660 as a viable solution for editable installs1.Would it be possible to have something like
mypy.plugin.Plugin.register_finder(finder: MetaPathFinder)
ormypy.plugin.Plugin.get_module_path_hook(fullname: str) -> str | os.PathLike[str] | None
?Footnotes
Indeed this strategy is somewhat praised in the text2:
↩Note that the use of the term "proxy" is associated with the strategy used by the
editables
project, which is aMetaPathFinder
: 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. ↩The text was updated successfully, but these errors were encountered: