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

[DO NOT MERGE] try generic position #104

Closed
wants to merge 3 commits into from
Closed

Conversation

vincentsarago
Copy link
Member

@vincentsarago vincentsarago commented Feb 9, 2023

This is a try at solving (at least partially) #98

from geojson_pydantic.types import Position2D, Position3D
from geojson_pydantic import Point

p2 = Point[Position2D]
>> p2(type="Point", coordinates=(0,0,0))
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 p2(type="Point", coordinates=(0,0,0))

ValidationError: 1 validation error for Point[Tuple[float, float]]
coordinates
  wrong tuple length 3, expected 2 (type=value_error.tuple.length; actual_length=3; expected_length=2)

p2(type="Point", coordinates=(0,0))
>> Point[Tuple[float, float]](type='Point', coordinates=(0.0, 0.0))


p = Polygon[Position3D]
p(type="Polygon", coordinates=[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]])
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Input In [21], in <cell line: 1>()
----> 1 p(type="Polygon", coordinates=[[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0), (1.0, 2.0)]])

ValidationError: 4 validation errors for Polygon[Tuple[float, float, float]]
coordinates -> 0 -> 0
  wrong tuple length 2, expected 3 (type=value_error.tuple.length; actual_length=2; expected_length=3)
coordinates -> 0 -> 1
  wrong tuple length 2, expected 3 (type=value_error.tuple.length; actual_length=2; expected_length=3)
coordinates -> 0 -> 2
  wrong tuple length 2, expected 3 (type=value_error.tuple.length; actual_length=2; expected_length=3)
coordinates -> 0 -> 3
  wrong tuple length 2, expected 3 (type=value_error.tuple.length; actual_length=2; expected_length=3)

@eseglem what do you think about this?

cc @geospatial-jeff

MultiPolygonCoords = List[PolygonCoords]
Position2D = Tuple[float, float]
Position3D = Tuple[float, float, float]
Position = Union[Position2D, Position3D]
Copy link
Member Author

@vincentsarago vincentsarago Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could get rid of the types.py file if we go this way

@eseglem
Copy link
Collaborator

eseglem commented Feb 9, 2023

I haven't done much with generics myself. Does this end up with something like Feature[Point[Position2D], MyProperties]? And everything still works?

Would we also provide Goemetry2D and Feature2D or is that going to be left to an end User?

I'm not necessarily a fan of preventing the Z, based on my reading of the spec. I totally understand the usefulness and desire to have it though. Heck I might even use it after debating myself about it for way too long.

I was going to caution against moving the types because it's a breaking change if anyone uses them. But the next release will be have breaking changes anyways, so seems like a reasonable time to do it.

And while I'm thinking of it and it's semi relivant: Why doesn't GeometryCollection inherit from a shared base with Geometry? They share most of the same functions and there's a little unnecessary duplication I think.

@vincentsarago
Copy link
Member Author

Would we also provide Goemetry2D and Feature2D or is that going to be left to an end User?

yep this was going to be my next commit

@vincentsarago
Copy link
Member Author

🤔 I think we are hitting a limit of what we can do! The latest commit added Geometry2D and Geometry3D Annotated types but it doesn't seem to work with python 3.8 (it does in 3.9)

Another note, it seems it breaks the discriminator

from typing import Dict
from geojson_pydantic import Feature
from geojson_pydantic.geometries import Position3D, Position2D, Polygon, Geometry2D

f2d = Feature[Geometry2D, Dict]

f2d(type="Feature", geometry={"type": "Point", "coordinates": (0, 0)}, properties=None)
>> Feature[Annotated[Union[geojson_pydantic.geometries.Point[Tuple[float, float]], geojson_pydantic.geometries.MultiPoint[Tuple[float, float]], geojson_pydantic.geometries.LineString[Tuple[float, float]], geojson_pydantic.geometries.MultiLineString[Tuple[float, float]], geojson_pydantic.geometries.Polygon[Tuple[float, float]], geojson_pydantic.geometries.MultiPolygon[Tuple[float, float]]], FieldInfo(default=PydanticUndefined, discriminator='type', extra={})], Dict](type='Feature', geometry=Point[Tuple[float, float]](type='Point', coordinates=(0.0, 0.0)), properties=None, id=None, bbox=None)

f2d(type="Feature", geometry={"type": "Point", "coordinates": (0, 0)}, properties=None).dict()
>> {'type': 'Feature',
 'geometry': {'type': 'Point', 'coordinates': (0.0, 0.0)},
 'properties': None,
 'id': None,
 'bbox': None}

f2d(type="Feature", geometry={"type": "Point", "coordinates": (0, 0, 3)}, properties=None)
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Input In [26], in <cell line: 1>()
----> 1 f(type="Feature", geometry={"type": "Point", "coordinates": (0, 0, 3)}, properties=None)

ValidationError: 21 validation errors for Feature[Annotated[Union[geojson_pydantic.geometries.Point[Tuple[float, float]], geojson_pydantic.geometries.MultiPoint[Tuple[float, float]], geojson_pydantic.geometries.LineString[Tuple[float, float]], geojson_pydantic.geometries.MultiLineString[Tuple[float, float]], geojson_pydantic.geometries.Polygon[Tuple[float, float]], geojson_pydantic.geometries.MultiPolygon[Tuple[float, float]]], FieldInfo(default=PydanticUndefined, discriminator='type', extra={})], Dict]
geometry -> coordinates
  wrong tuple length 3, expected 2 (type=value_error.tuple.length; actual_length=3; expected_length=2)
geometry -> type
  unexpected value; permitted: 'MultiPoint' (type=value_error.const; given=Point; permitted=('MultiPoint',))
geometry -> coordinates -> 0
  value is not a valid tuple (type=type_error.tuple)
geometry -> coordinates -> 1
  value is not a valid tuple (type=type_error.tuple)
geometry -> coordinates -> 2
  value is not a valid tuple (type=type_error.tuple)
geometry -> type
  unexpected value; permitted: 'LineString' (type=value_error.const; given=Point; permitted=('LineString',))
geometry -> coordinates -> 0
  value is not a valid tuple (type=type_error.tuple)
geometry -> coordinates -> 1
  value is not a valid tuple (type=type_error.tuple)
geometry -> coordinates -> 2
  value is not a valid tuple (type=type_error.tuple)
geometry -> type
  unexpected value; permitted: 'MultiLineString' (type=value_error.const; given=Point; permitted=('MultiLineString',))
geometry -> coordinates -> 0
  value is not a valid list (type=type_error.list)
geometry -> coordinates -> 1
  value is not a valid list (type=type_error.list)
geometry -> coordinates -> 2
  value is not a valid list (type=type_error.list)
geometry -> type
  unexpected value; permitted: 'Polygon' (type=value_error.const; given=Point; permitted=('Polygon',))
geometry -> coordinates -> 0
  value is not a valid list (type=type_error.list)
geometry -> coordinates -> 1
  value is not a valid list (type=type_error.list)
geometry -> coordinates -> 2
  value is not a valid list (type=type_error.list)
geometry -> type
  unexpected value; permitted: 'MultiPolygon' (type=value_error.const; given=Point; permitted=('MultiPolygon',))
geometry -> coordinates -> 0
  value is not a valid list (type=type_error.list)
geometry -> coordinates -> 1
  value is not a valid list (type=type_error.list)
geometry -> coordinates -> 2
  value is not a valid list (type=type_error.list)

@eseglem
Copy link
Collaborator

eseglem commented Feb 9, 2023 via email

@vincentsarago
Copy link
Member Author

maybe instead of annotated type we could go back to a simpler method #93 (comment) 🙈

@vincentsarago
Copy link
Member Author

I removed the annotated type in last commit (so reverting to verbose/useless validation error message) just to see if it would work with python 3.8 ... but it doesn't

I've tried other things locally without much luck. I'm going to pause this work and maybe re-check when pydantic v2 will be out

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

Successfully merging this pull request may close these issues.

2 participants