Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use field_serializer with fields allowed to be only a Point #156

Open
void-rooster opened this issue Mar 5, 2024 · 2 comments

Comments

@void-rooster
Copy link

We have to provide coordinates with 6 digits of precision and use a field serializer to do so. Curiously, we can use the serializer on fields allowed to be either a Point or a Polygon, but not with fields allowed to be only a Point.

This produces a SchemaError:

from pydantic import BaseModel, field_serializer
from geojson_pydantic import Point

class Test(BaseModel):
    point: Point
    @field_serializer("point")
    def six_digits_precision(self, pt: Point) -> Point:
        rounded_coords = [round(coord, 6) for coord in pt.coordinates]
        return Point(type="Point", coordinates=rounded_coords)

traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kpenner/mambaforge/envs/pydantic/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 183, in __new__
    complete_model_class(
  File "/home/kpenner/mambaforge/envs/pydantic/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 535, in complete_model_class
    cls.__pydantic_validator__ = create_schema_validator(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kpenner/mambaforge/envs/pydantic/lib/python3.12/site-packages/pydantic/plugin/_schema_validator.py", line 49, in create_schema_validator
    return SchemaValidator(schema, config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.SchemaError: Definitions error: definition `geojson_pydantic.geometries.Point:94014434282928` was never filled

without the field serializer, or with the field serializer and different treatment for coordinate extraction based on Point vs Polygon, it succeeds.

@vincentsarago
Copy link
Member

Maybe related to pydantic/pydantic#8984 🤷

@vincentsarago
Copy link
Member

add the same time you could do something like

from pydantic import field_serializer
from geojson_pydantic import Point
from geojson_pydantic.types import Position


class MyPoint(Point):

    @field_serializer("coordinates")
    def six_digits_precision(self, coords: Position) -> Position:
        rounded_coords = [round(coord, 6) for coord in coords]
        return tuple(rounded_coords)


MyPoint(type="Point", coordinates=(0.000000000001,0)).model_dump(exclude_none=True)
>> {'type': 'Point', 'coordinates': (0.0, 0.0)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants