Skip to content

Commit e62ccbf

Browse files
committed
Fix GeometryField value conversion crashing on non-None values
GeometryField subclasses Field directly (single base), so the _FieldMeta metaclass — which only auto-assigns field_type when there are multiple bases — leaves field_type as None. The base to_python_value/to_db_value converters then call isinstance(value, self.field_type), raising TypeError: isinstance() arg 2 must be a type ... on any non-None value. Declare field_type = str and parametrize as Field[str], mirroring the fix applied to TSVectorField in #2237.
1 parent 8c12adc commit e62ccbf

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from tortoise.contrib.mysql.fields import GeometryField
2+
3+
4+
def test_geometry_field_type() -> None:
5+
"""GeometryField must declare a concrete ``field_type``.
6+
7+
A single-base ``Field`` subclass does not get ``field_type`` assigned by the
8+
``_FieldMeta`` metaclass (that only fires for multiple bases), so an explicit
9+
attribute is required. When it is missing, ``field_type`` is ``None`` and the
10+
base value converters raise ``TypeError`` from ``isinstance(value, None)``.
11+
"""
12+
assert GeometryField().field_type is str
13+
14+
15+
def test_geometry_field_to_python_value() -> None:
16+
field = GeometryField()
17+
# ``None`` must pass through untouched.
18+
assert field.to_python_value(None) is None
19+
# A concrete DB value must be converted without raising.
20+
assert field.to_python_value("POINT(1 1)") == "POINT(1 1)"
21+
22+
23+
def test_geometry_field_to_db_value() -> None:
24+
field = GeometryField()
25+
assert field.to_db_value(None, None) is None
26+
assert field.to_db_value("POINT(1 1)", None) == "POINT(1 1)"

tortoise/contrib/mysql/fields.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
from tortoise.models import Model
1111

1212

13-
class GeometryField(Field):
13+
class GeometryField(Field[str]):
1414
SQL_TYPE = "GEOMETRY"
15+
field_type = str
1516

1617

1718
class UUIDField(UUIDFieldBase):

0 commit comments

Comments
 (0)