Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

pydantic_model_creator returns a cached class that ignores model_config, validators and module

オープン 初心者向け
#2,310 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@moneebullah25 がすでに取り組んでいます。

2026年10月10日 から。

  • #2312 @moneebullah25 による — オープン
  • #2325 @Fire162 による — オープン

評価

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

調査の方向性

tortoise/contrib/pydantic/creator.py から始め、特に _hash、_is_default、および 362〜365 行目付近のキャッシュヒット経路に注目します。キャッシュキーの入力と model_config、バリデーター、module を比較し、#1741 の既存テストの隣に、各引数を網羅する回帰テストを追加します。完了”是指、異なる値で呼び出した場合に、渡された設定を反映する異なるモデルクラスが生成されることです。

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

説明

Describe the bug
pydantic_model_creator returns a previously created (cached) class when called again with a different model_config, validators or module, so those arguments are silently ignored.

The cache key, _hash, covers only the model's fully qualified name, field types, relations, optional, allow_cycles, exclude_readonly and computed. _is_default also does not look at them. On a cache hit (L362-L365) the first class is returned. #1741 fixed the same kind of problem for exclude_readonly only.

To Reproduce

from pydantic import field_validator
from tortoise import fields
from tortoise.models import Model
from tortoise.contrib.pydantic import pydantic_model_creator

class Tour(Model):
    id = fields.IntField(primary_key=True)
    name = fields.CharField(max_length=20)

A = pydantic_model_creator(Tour)
B = pydantic_model_creator(Tour, model_config={"extra": "allow"})
print(A is B, B.model_config["extra"])   # True forbid

v1 = {"v": field_validator("name")(classmethod(lambda c, x: x.upper()))}
v2 = {"v": field_validator("name")(classmethod(lambda c, x: x.lower()))}
A = pydantic_model_creator(Tour, name="TourIn", validators=v1)
B = pydantic_model_creator(Tour, name="TourIn", validators=v2)
print(A is B, B(id=1, name="Ab").name)   # True AB   (expected "ab")

A = pydantic_model_creator(Tour, name="TourM", module="a.b")
B = pydantic_model_creator(Tour, name="TourM", module="c.d")
print(A is B, B.__module__)              # True a.b  (expected c.d)

Output is as commented (Python 3.12 and 3.14, pydantic 2.x).

Expected behavior
Distinct classes that honour each call's arguments.

Additional context
Possible fix: include the model_config, the validators (names and function identities) and module in the hash string (and in _is_default), or skip the cache when any of them is given. A regression test next to the one added in #1741 would cover it.

Related but different: #1741 (exclude_readonly), #1130 (exclude), #1301 (same name for different models).

Checked at 5815573 (develop).

主要言語
Python
スター
5.6k
フォーク
523
平均マージ
2日 18時間
マージ済み PR(30日)
12

環境構築

はじめの一歩

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

tortoise/tortoise-orm のほかの issue

tortoise/tortoise-orm の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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