python / python/mypy

Skipping the import of parent packages when specifying custom mypy plugins

Aperta
#17,000 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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__ and a.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__.py and b/__init__.py can be written in a manner to skip runtime imports if we know that a mypy plugin is being loaded?

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia tracciando il caricamento dei plugin personalizzati di mypy per un plugin come numpy.typing.mypy_plugin, concentrandoti sul motivo per cui vengono eseguiti numpy.init e numpy.typing.init. Confronta le due direzioni proposte —saltare gli import dei pacchetti padre oppure esporre un modo per rilevare il caricamento dei plugin— e definisci un comportamento canonico che eviti import di runtime non necessari senza compromettere il caricamento dei plugin.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
numpy, python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.