Factor out logic to generate ModuleNotFoundError in importlib?

オープン
#100,208 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
48/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
backend

調査の方向性

まず、_find_and_load_unlocked エントリポイントと、既存の _ERR_MSG および ModuleNotFoundError の構築箇所を特定します。その構築を _get_module_not_found_error(name) に切り出し、spec が見つからないパスでの直接的な構築を置き換え、結果として得られるエラーが引き続きモジュール名を保持し、monkey patching に適したままであることを確認します。

索引モデルが issue の本文から書いたものです。

説明

stdlib type-feature

Feature or enhancement

PEP 534 discusses improving error messages for standard library modules. We like to do this in Pyodide because some stdlib modules either are removed or are not included by default but can be installed. If someone tries to import these modules, we want to print some extra info about why the import is failing.

The following patch is a simple change that makes it easy to monkey patch _get_module_not_found_error to improve the ModuleNotFound errors:

+def _get_module_not_found_error(name):
+    return ModuleNotFoundError(_ERR_MSG.format(name), name=name)
+
 def _find_and_load_unlocked(name, import_):
     path = None
     parent = name.rpartition('.')[0]
@@ -1001,7 +1004,7 @@ def _find_and_load_unlocked(name, import_):
             raise ModuleNotFoundError(msg, name=name) from None
     spec = _find_spec(name, path)
     if spec is None:
-        raise ModuleNotFoundError(_ERR_MSG.format(name), name=name)
+        raise _get_module_not_found_error(name)
     else:
         module = _load_unlocked(spec)
     if parent:

In Pyodide since we are fairly tightly coupled to the Python version we would be okay with risking the possibility that this gets rearranged in the future.

Pitch

It would be nice to apply this patch. See the Pyodide PR: https://github.com/pyodide/pyodide/pull/3263

Previous discussion

PEP 534

主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。