Django VectorField and HalfVectorField to_python fail on a NumPy array since 0.5.0
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
Hướng nghiên cứu
Bắt đầu với VectorField.to_python và HalfVectorField.to_python trong phần tích hợp Django, sau đó đọc test_clean trong tests/test_django.py để xem việc làm sạch trường được kiểm thử như thế nào. Bổ sung kiểm thử cho các mảng NumPy và kiểm tra rằng field.clean(value, None) chấp nhận chúng ở cả hai trường, tương tự các trường hợp danh sách, chuỗi và vector hiện có.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Python
- Star
- 1.5k
- Fork
- 97
- Merge trung bình
- 13 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue tương tự
-
`burr` fails to start when the Python path has a space in itCó thể đã có người làm @enekoreto đã nhận hôm nay. Đang mởstatus/needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 5 ngày
-
gemini 出现了新的域名需要加入Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 72/100
debpalash/VoiceStudio#2903 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
dagster-io/community-integrations#367 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
aws-samples/sample-per-model-bedrock#243 ·
Maintainer thường phản hồi trong vòng 1 ngày