python / python/mypy

Can't check only submodules (except if installed with `pip install -e .`)

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

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

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

説明

Bug Report

Checking a submodule only of a project seems impossible, when not installed using pip install -e .

Usecase:
I have project that contains auto-generated API code which is not correctly typed.
I only want to check the controllers that are located in a submodule.

To Reproduce

  1. Create a example project with the following structure
.
├── setup.py
└──src
    └── example_project
        ├── __init__.py
        ├── calc.py
        ├── py.typed
        └── sub
            ├── __init__.py
            └── foo.py

src/example_project/calc.py :

def int_add(a: int, b: int) -> int:
    """
    Simply add two numbers
    """
    return a + b


def float_diff(a: float, b: float) -> float:
    """
    Diff two numbers
    """
    return a / b


if __name__ == "__main__":
    int_add("a", "b")

src/ems_example_project/sub/foo.py:

import example_project.calc

example_project.calc.int_add("foo", "bar")

a: int = 1
a = "2"
  1. Install the package with pip install . OR do not install it at all.

  2. Run mypy for the given submodule

mypy src/example_project/sub

Expected Behavior

Only type errors for the submodule are found:

src/example_project/sub/foo.py:3: error: Argument 1 to "int_add" has incompatible type "str"; expected "int"  [arg-type]
src/example_project/sub/foo.py:3: error: Argument 2 to "int_add" has incompatible type "str"; expected "int"  [arg-type]
src/example_project/sub/foo.py:6: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
Found 3 errors in 1 file (checked 2 source files)

This actually works, if I install the package with pip install -e ..
But I expect this to work no matter if I install the package normally or not installing it at all.

Actual Behavior

The type errors for the top level modules are also listed.

src/example_project/calc.py:21: error: Argument 1 to "int_add" has incompatible type "str"; expected "int"  [arg-type]
src/example_project/calc.py:21: error: Argument 2 to "int_add" has incompatible type "str"; expected "int"  [arg-type]
src/example_project/sub/foo.py:3: error: Argument 1 to "int_add" has incompatible type "str"; expected "int"  [arg-type]
src/example_project/sub/foo.py:3: error: Argument 2 to "int_add" has incompatible type "str"; expected "int"  [arg-type]
src/example_project/sub/foo.py:6: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
Found 5 errors in 2 files (checked 2 source files)

If I use the option -p example_project.sub instead of the path it works as expected. But in my environment I am only able to add specific files (think pre-commit or similar).
It also works if I install the package using pip install -e.
Still I would not expect a such a difference just depending on how I installed the package.

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: none besides the path to the file
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.16

This might be related to:

  • #10956
  • #10260
  • #6861
  • #10428

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

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

はじめの一歩

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

調査の方向性

報告されている setup.py の構成から始め、mypy src/example_project/sub-p example_project.sub、および editable インストールと通常のインストールの違いを再現します。calc.py と sub/foo.py の診断を比較し、その後、パスベースのモジュール検出の挙動を追跡します。要求されたサブモジュールだけをチェックしたときに、calc.py もチェックせず、期待される3つのエラーが出れば完了です。

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

評価

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

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

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