python / python/mypy

fails to import module from source directory for sources without .py extension

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

To Reproduce

Given the files:

testdir/integration-tests/common.py

class CommonConfig:
    pass

testdir/integration-tests/test1

#!/usr/bin/env python3
import common
assert __name__ == '__main__'
config = common.CommonConfig()

If I run mypy from testdir/ on integration-tests/test1, it fails to find the import common from the same directory:

$ mypy integration-tests/test1
integration-tests/test1:3: error: Cannot find implementation or library stub for module named "common"  [import-not-found]
integration-tests/test1:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I expect mypy to be able to find the common module from the directory containing the source file, and thus to exit without errors:

$ mypy integration-tests/test1

IIUC, it should find the import, since the search path should include: "The directories containing the sources given on the command line"

Other notes

If I rename test1 to test1.py, it works as expected.

If I run mypy from testdir/integration-tests instead of from testdir (such that common.py is in the current working directory), it works as expected.

Environment

$ python3 --version
Python 3.10.12
$ mypy --version
mypy 1.19.1 (compiled: yes)

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

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

はじめの一歩

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

調査の方向性

testdir/integration-tests/common.py と拡張子のない testdir/integration-tests/test1 で問題を再現し、その後 testdir から mypy を実行します。.py 拡張子のないソースに対して mypy が検索パスをどのように構築するかを追跡します。import common が見つかり、mypy がエラーなしで終了し、.py の場合と別の作業ディレクトリの場合も引き続き動作すれば完了です。

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

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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