Skipping the import of parent packages when specifying custom mypy plugins
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
For mypy plugins which are located within a parent library/application (e.g. mypy_plugins = a.b.plugin), provide a canonical way of skipping runtime code in the parent packages of the plugin (a.__init__ and a.b.__init__) when loading the plugin.
Pitch
The numpy mypy plugin (specfied as plugins = numpy.typing.mypy_plugin) executes all of the following extra modules whilst running mypy (see companion issue at https://github.com/numpy/numpy/issues/25967), most of which aren't needed for static typing:
{'numpy.fft._pocketfft', 'numpy.typing', '_compat_pickle', 'numpy.lib.type_check', 'numpy.core._dtype_ctypes', 'numpy._typing._add_docstring', 'numpy.core._multiarray_tests', 'numpy.polynomial.hermite', 'numpy.core.multiarray', 'base64', 'numpy.random._sfc64', 'numpy.dtypes', 'numpy._typing._char_codes', 'numpy._typing._nbit', 'numpy.core.numerictypes', 'numpy.lib.index_tricks', 'numpy.core._add_newdocs_scalars', 'cython_runtime', 'numpy.random._philox', 'numpy.lib.arrayterator', 'numpy.lib.shape_base', 'numpy.core.einsumfunc', 'numpy._utils', 'numpy.polynomial', 'numpy.lib.mixins', 'numpy.core.numeric', 'numpy.fft', 'numpy.compat.py3k', 'numbers', 'numpy.linalg', 'numpy.core._exceptions', 'numpy.lib._iotools', 'numpy._typing._shape', 'numpy.lib.ufunclike', 'numpy._typing._dtype_like', 'numpy.core._ufunc_config', 'numpy.random._common', 'numpy.fft.helper', 'numpy.random._mt19937', '_cython_3_0_8', 'numpy.core.fromnumeric', 'numpy.lib.histograms', 'numpy.core.getlimits', 'numpy.lib.polynomial', 'numpy.polynomial.polyutils', 'numpy.typing.mypy_plugin', 'ctypes', 'numpy.ma.core', 'numpy.exceptions', 'numpy.polynomial._polybase', 'numpy.core._asarray', 'numpy.core.umath', 'numpy.__config__', 'numpy.lib.function_base', 'numpy.compat', 'numpy.polynomial.chebyshev', 'numpy.lib.arraypad', 'numpy.core._dtype', 'numpy._globals', 'numpy.core.records', 'numpy._typing', 'numpy.lib.format', 'numpy._utils._convertions', 'numpy.core._internal', 'numpy.random.mtrand', 'numpy.random._pcg64', 'numpy.random._pickle', 'numpy.core._multiarray_umath', 'numpy.core.shape_base', 'numpy.lib._datasource', 'numpy.lib.nanfunctions', 'numpy.lib._version', 'numpy.core._add_newdocs', 'numpy.fft._pocketfft_internal', 'numpy.lib.utils', 'numpy.core.defchararray', 'numpy.random', 'contextvars', 'numpy.matrixlib.defmatrix', 'numpy.polynomial.legendre', 'numpy.core', 'numpy.random._bounded_integers', 'numpy.ma.extras', 'numpy.core._methods', '_ctypes', '_contextvars', 'pickle', 'numpy._distributor_init', 'numpy.core._type_aliases', 'numpy._typing._array_like', 'numpy.matrixlib', 'hmac', 'numpy.polynomial.laguerre', 'numpy.core.function_base', 'numpy.polynomial.hermite_e', 'numpy.lib.npyio', 'numpy.core._machar', '_pickle', 'secrets', 'numpy.core.arrayprint', 'ctypes._endian', 'numpy._typing._scalars', 'numpy.lib.arraysetops', 'numpy.core._string_helpers', 'numpy._pytesttester', 'numpy.version', 'numpy', 'numpy.random._generator', 'numpy.lib.scimath', 'numpy.linalg._umath_linalg', 'numpy.lib', 'numpy.core.memmap', 'numpy._utils._inspect', 'numpy._typing._nested_sequence', 'numpy.lib.stride_tricks', 'numpy.lib.twodim_base', 'numpy.polynomial.polynomial', 'numpy.ctypeslib', 'numpy.ma', 'numpy.core.overrides', 'numpy.linalg.linalg', 'numpy.random.bit_generator'}
This is currently unavoidable, because the numpy mypy plugin deliberately imports numpy at runtime for resolving dtype aliases. However, even if they found an alternative way of resolving dtype aliases without importing numpy, the plugin's location of numpy.typing.mypy_plugin means that numpy.__init__ and numpy.typing.__init__ will be executed while running mypy.
Putting the mypy plugin inside the library/application eases the packaging and distribution process, but the current plugin loading mechanism means that parent packages of the plugin module will always be executed.
Is it possible to either
- Provide a way of skipping the import of
a.__init__anda.b.__init__during loading of the mypy plugin, maybe via an import hook, or - Provide a canonical way of detecting that mypy is being run, so that
a/__init__.pyandb/__init__.pycan be written in a manner to skip runtime imports if we know that a mypy plugin is being loaded?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing mypy's custom plugin loading for a plugin such as numpy.typing.mypy_plugin, focusing on why numpy.init and numpy.typing.init execute. Compare the two proposed directions—skipping parent-package imports or exposing a way to detect plugin loading—and define a canonical behavior that prevents unnecessary runtime imports without breaking plugin loading.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100