python / python/mypy

mypy fails to recognise dunder init method override

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

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

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

説明

Bug Report
In an inheritance scenario with pydantic.BaseModel and generics, mypy 1.2.0 fails to recognise an overridden __init__(),
and wrongly reports an error (see "actual behavior").

To Reproduce

from __future__ import annotations

from typing import Generic, List, Type, TypeVar

from pydantic import BaseModel, PrivateAttr

B_co = TypeVar("B_co", bound=BaseModel, covariant=True)

class Container(Generic[B_co], BaseModel):
    """Container is the base class for any item-type specific containers,
    to allow bundling and processing of aggregates
    """

    _item_class: Type[B_co] = PrivateAttr()
    __root__: List[B_co]

    def __init__(self, *args: B_co) -> None:
        super().__init__(__root__=list(args))

    def __len__(self) -> int:
        return len(self.__root__)

    def __getitem__(self, index: int) -> B_co:
        return self.__root__[index]


class Item(BaseModel):
    name: str

class Items(Container[Item]):
    _item_class = Item

items = Items(Item(name="one"), Item(name="two"))
print(items)

Expected Behavior
The overridden __init__ method in the Container class has the correct signature, and mypy should not flag any issues.

Actual Behavior

ttt/ttt.py:33: error: Too many arguments for "Items"  [call-arg]
ttt/ttt.py:33: error: Too many positional arguments for "Items"  [misc]
ttt/ttt.py:33: error: Argument 1 to "Items" has incompatible type "Item"; expected "List[B_co]"  [arg-type]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
python_version = "3.10"
strict = true
namespace_packages = false
explicit_package_bases = false
plugins = [
  "pydantic.mypy"
]
follow_imports = "silent"
warn_redundant_casts = true
warn_unused_ignores = true
disallow_any_generics = true
check_untyped_defs = true
no_implicit_reexport = true
disallow_untyped_defs = true

[[tool.mypy.overrides]]
module = [
     "html_text.*"
]
ignore_missing_imports = true
ignore_errors = true

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true
  • Python version used: 3.10.10
  • Other versions: pydantic=1.10.7, typing-extensions=4.5.0

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

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

はじめの一歩

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

調査の方向性

まず、提供された例を mypy 1.2.0 と示された設定で再現します。mypy がジェネリックな BaseModel サブクラスのコンストラクターシグネチャをどのように決定しているか、また pydantic プラグインがそれにどのような影響を与えているかを追跡します。例が報告された 3 つのエラーなしで型チェックを通過し、意図したコンストラクターの型付けが維持されれば完了です。

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

評価

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

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

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