Repository navigation
Types: Serialize geometry and geography without quoted parameters #4482
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1005,7 +1005,7 @@ class GeometryType(PrimitiveType): | |||||||||||||||||||
| >>> GeometryType("EPSG:4326") | ||||||||||||||||||||
| GeometryType(crs='EPSG:4326') | ||||||||||||||||||||
| >>> str(GeometryType("EPSG:4326")) | ||||||||||||||||||||
| "geometry('EPSG:4326')" | ||||||||||||||||||||
| 'geometry(EPSG:4326)' | ||||||||||||||||||||
| """ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| root: str = Field(default=DEFAULT_GEOMETRY_CRS) | ||||||||||||||||||||
|
|
@@ -1018,7 +1018,7 @@ def ser_model(self) -> str: | |||||||||||||||||||
| """Serialize the model to a string.""" | ||||||||||||||||||||
| if self.crs == DEFAULT_GEOMETRY_CRS: | ||||||||||||||||||||
| return "geometry" | ||||||||||||||||||||
| return f"geometry('{self.crs}')" | ||||||||||||||||||||
| return f"geometry({self.crs})" | ||||||||||||||||||||
|
Comment on lines
1019
to
+1021
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The spec's type serialization table lists
Suggested change
Same for |
||||||||||||||||||||
|
|
||||||||||||||||||||
| @property | ||||||||||||||||||||
| def crs(self) -> str: | ||||||||||||||||||||
|
|
@@ -1035,7 +1035,7 @@ def __str__(self) -> str: | |||||||||||||||||||
| """Return the string representation.""" | ||||||||||||||||||||
| if self.crs == DEFAULT_GEOMETRY_CRS: | ||||||||||||||||||||
| return "geometry" | ||||||||||||||||||||
| return f"geometry('{self.crs}')" | ||||||||||||||||||||
| return f"geometry({self.crs})" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def __hash__(self) -> int: | ||||||||||||||||||||
| """Return the hash of the CRS.""" | ||||||||||||||||||||
|
|
@@ -1074,7 +1074,7 @@ class GeographyType(PrimitiveType): | |||||||||||||||||||
| >>> GeographyType("EPSG:4326", "planar") | ||||||||||||||||||||
| GeographyType(crs='EPSG:4326', algorithm='planar') | ||||||||||||||||||||
| >>> str(GeographyType("EPSG:4326", "planar")) | ||||||||||||||||||||
| "geography('EPSG:4326', 'planar')" | ||||||||||||||||||||
| 'geography(EPSG:4326, planar)' | ||||||||||||||||||||
|
Comment on lines
1074
to
+1077
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| """ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| root: tuple[str, str] = Field(default=(DEFAULT_GEOGRAPHY_CRS, DEFAULT_GEOGRAPHY_ALGORITHM)) | ||||||||||||||||||||
|
|
@@ -1088,8 +1088,8 @@ def ser_model(self) -> str: | |||||||||||||||||||
| if self.crs == DEFAULT_GEOGRAPHY_CRS and self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: | ||||||||||||||||||||
| return "geography" | ||||||||||||||||||||
| if self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: | ||||||||||||||||||||
| return f"geography('{self.crs}')" | ||||||||||||||||||||
| return f"geography('{self.crs}', '{self.algorithm}')" | ||||||||||||||||||||
| return f"geography({self.crs})" | ||||||||||||||||||||
| return f"geography({self.crs}, {self.algorithm})" | ||||||||||||||||||||
|
Comment on lines
1088
to
+1092
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: the canonical form is
Suggested change
And the same for |
||||||||||||||||||||
|
|
||||||||||||||||||||
| @property | ||||||||||||||||||||
| def crs(self) -> str: | ||||||||||||||||||||
|
|
@@ -1114,8 +1114,8 @@ def __str__(self) -> str: | |||||||||||||||||||
| if self.crs == DEFAULT_GEOGRAPHY_CRS and self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: | ||||||||||||||||||||
| return "geography" | ||||||||||||||||||||
| if self.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM: | ||||||||||||||||||||
| return f"geography('{self.crs}')" | ||||||||||||||||||||
| return f"geography('{self.crs}', '{self.algorithm}')" | ||||||||||||||||||||
| return f"geography({self.crs})" | ||||||||||||||||||||
| return f"geography({self.crs}, {self.algorithm})" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def __hash__(self) -> int: | ||||||||||||||||||||
| """Return the hash of the tuple.""" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -777,7 +777,7 @@ def test_geometry_type_custom_crs() -> None: | |||||||||||||||||||||||||||||||||||
| """Test GeometryType with custom CRS.""" | ||||||||||||||||||||||||||||||||||||
| type_var = GeometryType("EPSG:4326") | ||||||||||||||||||||||||||||||||||||
| assert type_var.crs == "EPSG:4326" | ||||||||||||||||||||||||||||||||||||
| assert str(type_var) == "geometry('EPSG:4326')" | ||||||||||||||||||||||||||||||||||||
| assert str(type_var) == "geometry(EPSG:4326)" | ||||||||||||||||||||||||||||||||||||
| assert repr(type_var) == "GeometryType(crs='EPSG:4326')" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -799,7 +799,8 @@ def test_geometry_type_pickle() -> None: | |||||||||||||||||||||||||||||||||||
| def test_geometry_type_serialization() -> None: | ||||||||||||||||||||||||||||||||||||
| """Test GeometryType JSON serialization.""" | ||||||||||||||||||||||||||||||||||||
| assert GeometryType().model_dump_json() == '"geometry"' | ||||||||||||||||||||||||||||||||||||
| assert GeometryType("EPSG:4326").model_dump_json() == "\"geometry('EPSG:4326')\"" | ||||||||||||||||||||||||||||||||||||
| assert GeometryType("EPSG:4326").model_dump_json() == '"geometry(EPSG:4326)"' | ||||||||||||||||||||||||||||||||||||
| assert GeometryType.model_validate_json(GeometryType("EPSG:4326").model_dump_json()) == GeometryType("EPSG:4326") | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
801
to
+803
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the canonical form, the default includes the CRS as well:
Suggested change
If |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def test_geometry_type_deserialization() -> None: | ||||||||||||||||||||||||||||||||||||
|
|
@@ -847,7 +848,7 @@ def test_geography_type_custom_crs() -> None: | |||||||||||||||||||||||||||||||||||
| type_var = GeographyType("EPSG:4326") | ||||||||||||||||||||||||||||||||||||
| assert type_var.crs == "EPSG:4326" | ||||||||||||||||||||||||||||||||||||
| assert type_var.algorithm == DEFAULT_GEOGRAPHY_ALGORITHM | ||||||||||||||||||||||||||||||||||||
| assert str(type_var) == "geography('EPSG:4326')" | ||||||||||||||||||||||||||||||||||||
| assert str(type_var) == "geography(EPSG:4326)" | ||||||||||||||||||||||||||||||||||||
| assert repr(type_var) == "GeographyType(crs='EPSG:4326')" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -856,7 +857,7 @@ def test_geography_type_custom_crs_and_algorithm() -> None: | |||||||||||||||||||||||||||||||||||
| type_var = GeographyType("EPSG:4326", "planar") | ||||||||||||||||||||||||||||||||||||
| assert type_var.crs == "EPSG:4326" | ||||||||||||||||||||||||||||||||||||
| assert type_var.algorithm == "planar" | ||||||||||||||||||||||||||||||||||||
| assert str(type_var) == "geography('EPSG:4326', 'planar')" | ||||||||||||||||||||||||||||||||||||
| assert str(type_var) == "geography(EPSG:4326, planar)" | ||||||||||||||||||||||||||||||||||||
| assert repr(type_var) == "GeographyType(crs='EPSG:4326', algorithm='planar')" | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
857
to
861
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -879,8 +880,12 @@ def test_geography_type_pickle() -> None: | |||||||||||||||||||||||||||||||||||
| def test_geography_type_serialization() -> None: | ||||||||||||||||||||||||||||||||||||
| """Test GeographyType JSON serialization.""" | ||||||||||||||||||||||||||||||||||||
| assert GeographyType().model_dump_json() == '"geography"' | ||||||||||||||||||||||||||||||||||||
| assert GeographyType("EPSG:4326").model_dump_json() == "\"geography('EPSG:4326')\"" | ||||||||||||||||||||||||||||||||||||
| assert GeographyType("EPSG:4326", "planar").model_dump_json() == "\"geography('EPSG:4326', 'planar')\"" | ||||||||||||||||||||||||||||||||||||
| assert GeographyType("EPSG:4326").model_dump_json() == '"geography(EPSG:4326)"' | ||||||||||||||||||||||||||||||||||||
| assert GeographyType("EPSG:4326", "planar").model_dump_json() == '"geography(EPSG:4326, planar)"' | ||||||||||||||||||||||||||||||||||||
| assert GeographyType.model_validate_json(GeographyType("EPSG:4326").model_dump_json()) == GeographyType("EPSG:4326") | ||||||||||||||||||||||||||||||||||||
| assert GeographyType.model_validate_json(GeographyType("EPSG:4326", "planar").model_dump_json()) == GeographyType( | ||||||||||||||||||||||||||||||||||||
| "EPSG:4326", "planar" | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
882
to
+888
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def test_geography_type_deserialization() -> None: | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
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.
Same as the docstring:
planarisn't a spec-valid geography algorithm. TheGeographyType("EPSG:4326", "planar")example further up (line 57) needs the same change.