Skipping the import of parent packages when specifying custom mypy plugins
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das benutzerdefinierte Laden von Plugins durch mypy für ein Plugin wie numpy.typing.mypy_plugin nachzuverfolgen, und konzentriere dich darauf, warum numpy.init und numpy.typing.init ausgeführt werden. Vergleiche die beiden vorgeschlagenen Richtungen – das Überspringen von Importen übergeordneter Pakete oder das Bereitstellen einer Möglichkeit, das Laden von Plugins zu erkennen – und definiere ein kanonisches Verhalten, das unnötige Laufzeitimporte verhindert, ohne das Laden von Plugins zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- numpy, python
- Bereich
- tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100