python / python/mypy

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

Open
#16,988 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names mypy.plugin.Plugin and the MetaPathFinder/PathEntryFinder cases in setuptools.extern and pkg_resources.extern. Start by reviewing the plugin API and existing import-handling documentation, then compare the proposed guidance or hook behavior with those finder examples. Done means the supported behavior and examples are documented, with any required plugin entry point clearly specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.