Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open Beginner friendly
#163 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
django, numpy, python
Domain
database

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.