python / python/mypy

Incorrect handling of `import from` namespace package that spans site-packages and the source tree

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

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

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

説明

Bug Report

The following command:

mypy --explicit-package-bases ns_package/sub_ns_package1/test.py

Resulted in

ns_package/sub_ns_package1/test.py:1: error: Module "ns_package" has no attribute "sub_ns_package2"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

With the following setup:

Source code tree:

ns_package
    sub_ns_package1
        test.py

(note that there is no __init__.py)

site-packages tree (partial):

ns_package
    sub_ns_package2
        module.py    

(same, no __init__.py)

test.py contents:

from ns_package import sub_ns_package2

While scratching my head I also found the following:

  • import sub_ns_package.ns_package2 works.
  • it also works if the above import appears together with from ns_package import sub_ns_package2

To Reproduce
See the above.

Expected Behavior

mypy should finish happily in the above example.

Actual Behavior

ns_package/sub_ns_package1/test.py:1: error: Module "ns_package" has no attribute "sub_ns_package2"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.1
  • Mypy command-line flags: --explicit-package-bases (tried with or without it) ; --namespace-packages (tried with or without it)
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.8.16

Potential Fix
I have very little knowledge about the codebase, but the following patch would make the problem go away. I'm not sure it's the right fix though.

https://github.com/python/mypy/commit/e69bbd2c82e60640171dd7e2653c010976ad4239

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

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

はじめの一歩

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

調査の方向性

示されている namespace-package ツリーと mypy --explicit-package-bases ns_package/sub_ns_package1/test.py を使って報告を再現し、その後、参照されているコミット e69bbd2c82e60640171dd7e2653c010976ad4239 を調査してください。分割された source-tree/site-packages のケースに対する回帰テストのカバレッジを追加し、コマンドが attr-defined エラーなしで完了することを確認してください。

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

評価

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

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

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