Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Django VectorField and HalfVectorField to_python fail on a NumPy array since 0.5.0

已關閉 適合新手
#163 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
78/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
django, numpy, python
領域
database

研究方向

從 Django 整合中的 VectorField.to_python 和 HalfVectorField.to_python 開始,接著閱讀 tests/test_django.py 中的 test_clean,了解欄位清理是如何涵蓋的。為 NumPy 陣列新增測試,並檢查兩個欄位的 field.clean(value, None) 都能接受這些陣列,與現有的列表、字串和向量案例一致。

由索引模型根據 Issue 內容生成。

描述

Since fdd559a ("Simplified code", shipped in 0.5.0), VectorField.to_python and HalfVectorField.to_python pass anything that is not a list to _from_db, which only knows None, the vector class and str. A NumPy array reaches _from_text and fails on .split, so full_clean() breaks on a model that save() accepts, because get_prep_value still goes through Vector(value).

$ python -I pgv_repro.py      # master 1cbee1a, Django 6.1.2, NumPy 2.5.4, Python 3.12.3
VectorField list -> [1.0, 2.0, 3.0]
VectorField str -> [1.0, 2.0, 3.0]
VectorField Vector -> [1.0, 2.0, 3.0]
VectorField ndarray -> AttributeError 'numpy.ndarray' object has no attribute 'split'
HalfVectorField ndarray -> AttributeError 'numpy.ndarray' object has no attribute 'split'

$ PYTHONPATH=<tree at fdd559a^> python pgv_repro.py
VectorField ndarray -> [1.0, 2.0, 3.0]
HalfVectorField ndarray -> [1.0, 2.0, 3.0]

The script calls field.clean(value, None) on each input, which is the path Model.full_clean() takes. test_clean in tests/test_django.py only passes lists, which is why the suite stayed green.

_to_db already builds the class from anything else, so I'd add that one step to to_python and keep the _from_db call (HalfVector the same way in halfvec.py):

if isinstance(value, list):
    return value
if value is not None and not isinstance(value, (str, Vector)):
    value = Vector(value)
return Vector._from_db(value)
$ PYTHONPATH=<1cbee1a + that change, both fields> python pgv_repro.py      # Python 3.12.6
VectorField list -> [1.0, 2.0, 3.0]
VectorField str -> [1.0, 2.0, 3.0]
VectorField Vector -> [1.0, 2.0, 3.0]
VectorField ndarray -> [1.0, 2.0, 3.0]
HalfVectorField list -> [1.0, 2.0, 3.0]
HalfVectorField str -> [1.0, 2.0, 3.0]
HalfVectorField HalfVector -> [1.0, 2.0, 3.0]
HalfVectorField ndarray -> [1.0, 2.0, 3.0]

@ankane I can add the NumPy case to test_clean in the same change, if you want it as a pull request.

主要語言
Python
星號
1.5k
分支
97
平均合併
13 分鐘
30 天內合併 PR
1

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。