Django VectorField and HalfVectorField to_python fail on a NumPy array since 0.5.0
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start with VectorField.to_python and HalfVectorField.to_python in the Django integration, then read test_clean in tests/test_django.py to see how field cleaning is covered. Add coverage for NumPy arrays and check that field.clean(value, None) accepts them for both fields, matching the existing list, string, and vector cases.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 97
- Avg merge
- 13m
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
openwisp/openwisp-utils#777 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
google-research/weatherbenchX#310 ·
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
huggingface/peft#3955 ·
Maintainers usually reply within 1 day
-
author-page correction
Difficulty 2/5 1-3 hours Newbie friendliness 60/100
acl-org/acl-anthology#10313 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
AOSSIE-Org/Gitcord-GithubDiscordBot#147 · 1 comment ·
Maintainers usually reply within 1 day