-
Notifications
You must be signed in to change notification settings - Fork 34
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
Conversation
MultiPolygonCoords = List[PolygonCoords] | ||
Position2D = Tuple[float, float] | ||
Position3D = Tuple[float, float, float] | ||
Position = Union[Position2D, Position3D] |
There was a problem hiding this comment.
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
79a57ba
to
c073aef
Compare
I haven't done much with generics myself. Does this end up with something like 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. |
yep this was going to be my next commit |
🤔 I think we are hitting a limit of what we can do! The latest commit added Another note, it seems it breaks the 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) |
I believe there are some weird limitation with `Annotated` being nested
inside stuff. I ran into some of that when trying to discriminate between
Geometry and GeometryCollection. I can't remember the exact details. But
one of those things that pretty much can't / won't be fixed until
pydantic2.
I kind of expected things would end up real weird / nasty nesting generic
inside generic with discriminator and such. It's right around the limits of
pydantic. I'll try to play with a few things if I have time tonight though,
see if I can come up with something.
…On Thu, Feb 9, 2023 at 8:15 AM Vincent Sarago ***@***.***> wrote:
🤔 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)
—
Reply to this email directly, view it on GitHub
<#104 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABNFN3ANF2UBJA63KEEVYRLWWTUWHANCNFSM6AAAAAAUWKQ53M>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
maybe instead of annotated type we could go back to a simpler method #93 (comment) 🙈 |
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 |
This is a try at solving (at least partially) #98
@eseglem what do you think about this?
cc @geospatial-jeff