Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
abravalheri opened this issue Mar 5, 2024 · 1 comment
Labels

Comments

@abravalheri
Copy link

abravalheri commented Mar 5, 2024

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 installs1.

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?

Footnotes

  1. Indeed this strategy is somewhat praised in the text2:

    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.

bpkroth added a commit to microsoft/MLOS that referenced this issue Jul 10, 2024
1. Temporarily avoid recent asyncssh version that breaks the tests
2. Workaround pylance issue with pyproject.toml related changes and pip
editable modules install format (#768)
   See Also: 
   - microsoft/pylance-release#3473
   May also affect `mypy`:
   - python/mypy#16988
   - python/mypy#12313
bpkroth added a commit to bpkroth/MLOS that referenced this issue Jul 10, 2024
1. Temporarily avoid recent asyncssh version that breaks the tests
2. Workaround pylance issue with pyproject.toml related changes and pip
editable modules install format (microsoft#768)
   See Also:
   - microsoft/pylance-release#3473
   May also affect `mypy`:
   - python/mypy#16988
   - python/mypy#12313
DelphianCalamity pushed a commit to DelphianCalamity/MLOS that referenced this issue Jul 12, 2024
1. Temporarily avoid recent asyncssh version that breaks the tests
2. Workaround pylance issue with pyproject.toml related changes and pip
editable modules install format (microsoft#768)
   See Also: 
   - microsoft/pylance-release#3473
   May also affect `mypy`:
   - python/mypy#16988
   - python/mypy#12313
@pelson
Copy link

pelson commented Aug 30, 2024

I too would love the ability to handle meta_hooks. For me, I would be fine writing a specific mypy plugin which emulates my meta hook, and returns the code that should be checked.

In short: I would like a mypy hook that allows me to transform imported code before it is analysed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants