Skip to content

Load config plugins from python executable paths#21653

Open
w-Jessamine wants to merge 2 commits into
python:masterfrom
w-Jessamine:fix/plugin-python-executable-path
Open

Load config plugins from python executable paths#21653
w-Jessamine wants to merge 2 commits into
python:masterfrom
w-Jessamine:fix/plugin-python-executable-path

Conversation

@w-Jessamine

Copy link
Copy Markdown

Summary

This updates config plugin loading so module-name plugins can be imported from the search paths discovered for --python-executable.

Previously mypy used --python-executable to discover imports from the target interpreter, but config plugins such as pydantic.mypy were still imported only from the current mypy process sys.path. As a result, globally installed mypy could type-check packages from a virtualenv while failing to load plugins installed in that same virtualenv.

The new helper temporarily prepends the target interpreter's discovered sys.path and site-packages paths while loading config plugins, then restores the original sys.path. It intentionally compares executable paths textually rather than with samefile(), because a virtualenv bin/python can resolve to the same underlying binary while still having different site-packages.

Fixes #21621.

Tests

  • /tmp/mypy-pr-venv/bin/python -m pytest -n0 mypy/test/testplugins.py -q
  • /tmp/mypy-pr-venv/bin/python -m pytest -n0 mypy/test/testargs.py mypy/test/testplugins.py -q
  • /tmp/mypy-pr-venv/bin/python -m pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-custom-plugin.test -q
  • /tmp/mypy-pr-venv/bin/python -m mypy --config-file mypy_self_check.ini mypy/build.py mypy/test/testplugins.py
  • /tmp/mypy-pr-venv/bin/python -m compileall -q mypy/build.py mypy/test/testplugins.py

Disclosure

I used an LLM coding assistant while investigating and preparing this change, and I reviewed and tested the resulting code.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

django-modern-rest (https://github.com/wemake-services/django-modern-rest)
- pyproject.toml:1: error: Error importing plugin "pydantic.mypy": No module named 'pydantic'  [misc]
+ dmr/validation/settings.py:29: error: Unused "type: ignore" comment  [unused-ignore]
+ dmr/validation/settings.py:30: error: Unused "type: ignore" comment  [unused-ignore]
+ dmr/validation/settings.py:31: error: Unused "type: ignore" comment  [unused-ignore]
+ dmr/validation/settings.py:32: error: Unused "type: ignore" comment  [unused-ignore]
+ dmr/validation/settings.py:33: error: Unused "type: ignore" comment  [unused-ignore]
+ dmr/validation/settings.py:34: error: Unused "type: ignore" comment  [unused-ignore]
+ dmr/validation/settings.py:35: error: Unused "type: ignore" comment  [unused-ignore]
+ dmr/throttling/backends/redis.py:56: error: "Redis" expects no type arguments, but 1 given  [type-arg]
+ dmr/throttling/backends/redis.py:158: error: "Redis" expects no type arguments, but 1 given  [type-arg]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant