pydantic_model_creator returns a cached class that ignores model_config, validators and module
メンテナーはふだん 2 日以内に返信
@moneebullah25 がすでに取り組んでいます。
2026年10月10日 から。
評価
調査の方向性
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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
tortoise/tortoise-orm のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
tortoise/tortoise-orm#668 ·
メンテナーはふだん 2 日以内に返信
-
refresh_from_db silently ignores fields supplied as an iterator対応中かも @IlyaKhalafi が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 35/100
tortoise/tortoise-orm#2341 ·
メンテナーはふだん 2 日以内に返信
-
Renamed fields break dict(model) and default values_list対応中かも @IlyaKhalafi が 1 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
tortoise/tortoise-orm#2337 ·
メンテナーはふだん 2 日以内に返信
-
Reverse relation filters use attribute names for SQL columns対応中かも @IlyaKhalafi が 1 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
tortoise/tortoise-orm#2336 ·
メンテナーはふだん 2 日以内に返信
-
Full refresh_from_db leaves source_field attributes stale対応中かも @IlyaKhalafi が 1 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 20/100
tortoise/tortoise-orm#2334 ·
メンテナーはふだん 2 日以内に返信
tortoise/tortoise-orm の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
EchoTools/nevr-runtime#651 ·
メンテナーはふだん 1 日以内に返信
-
bug frontend rt sev-3
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
noontide-co/mainbranch#1211 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[1.x Bug] run-operation --sql reports success but rolls back writes on transaction-capable adaptersオープンtriage
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
dbt-labs/dbt#16784 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信