rambleraptor commented on code in PR #2224:
URL: https://github.com/apache/iceberg-python/pull/2224#discussion_r2232119133


##########
pyiceberg/types.py:
##########
@@ -85,6 +88,38 @@ def _parse_decimal_type(decimal: Any) -> Tuple[int, int]:
     else:
         return decimal
 
+def _parse_geography_type(geography: Any) -> Tuple[str, 
GeographyType.EdgeAlgorithm]:
+    if isinstance(geography, str):
+        matches = GEOGRAPHY_REGEX.search(geography)
+        if matches:
+            crs = None
+            edge_algorithm = None
+            if matches.group(1):
+                crs = matches.group(1)
+            if matches.group(2):
+                edge_algorithm = 
GeographyType.EdgeAlgorithm(matches.group(2).lower())
+            return crs, edge_algorithm
+        else:
+            raise ValidationError(f"Could not parse {geography} into a 
GeographyType")
+    elif isinstance(geography, dict):
+        return geography["crs"], geography["edge_algorithm"]
+    else:
+        return geography
+
+def _parse_geometry_type(geometry: Any) -> str:
+    if isinstance(geometry, str):
+        matches = GEOMETRY_REGEX.search(geometry)
+        if matches:
+            crs = None
+            if matches.group(1):
+                crs = matches.group(1)
+            return crs
+        else:
+            raise ValidationError(f"Could not parse {geometry} into a 
GeometryType")
+    elif isinstance(geometry, dict):
+        return geometry["crs"]

Review Comment:
   Can you throw a user-readable error if either of these do not exist on the 
dictionary?



##########
pyiceberg/types.py:
##########
@@ -85,6 +88,38 @@ def _parse_decimal_type(decimal: Any) -> Tuple[int, int]:
     else:
         return decimal
 
+def _parse_geography_type(geography: Any) -> Tuple[str, 
GeographyType.EdgeAlgorithm]:
+    if isinstance(geography, str):
+        matches = GEOGRAPHY_REGEX.search(geography)
+        if matches:
+            crs = None
+            edge_algorithm = None
+            if matches.group(1):
+                crs = matches.group(1)
+            if matches.group(2):
+                edge_algorithm = 
GeographyType.EdgeAlgorithm(matches.group(2).lower())
+            return crs, edge_algorithm
+        else:
+            raise ValidationError(f"Could not parse {geography} into a 
GeographyType")
+    elif isinstance(geography, dict):
+        return geography["crs"], geography["edge_algorithm"]

Review Comment:
   Can you throw a user-readable error if either of these do not exist on the 
dictionary? 



##########
tests/test_types.py:
##########
@@ -499,6 +501,41 @@ def test_repr_binary() -> None:
     assert repr(BinaryType()) == "BinaryType()"
 
 
+def test_serialization_geography() -> None:
+    assert GeographyType().model_dump_json() == '"geography"'
+
+
+def test_deserialization_geography() -> None:
+    assert GeographyType.model_validate_json('"geography"') == GeographyType()
+    assert GeographyType.model_validate_json('"geography(OGC:CRS84, thomas)"') 
== GeographyType(crs="OGC:CRS84", 
edge_algorithm=GeographyType.EdgeAlgorithm.THOMAS)
+    assert GeographyType.model_validate_json('"geography(OGC:CRS84)"') == 
GeographyType(crs="OGC:CRS84", edge_algorithm=None)
+
+
+def test_str_geography() -> None:
+    assert str(GeographyType()) == "geography"
+
+
+def test_repr_geography() -> None:
+    assert repr(GeographyType()) == "GeographyType(crs=None, 
edge_algorithm=None)"
+
+
+def test_serialization_geometry() -> None:
+    assert GeometryType().model_dump_json() == '"geometry"'
+
+
+def test_deserialization_geometry() -> None:
+    assert GeometryType.model_validate_json('"geometry"') == GeometryType()
+    assert GeometryType.model_validate_json('"geometry(OGC:CRS84)"') == 
GeometryType(crs="OGC:CRS84")
+
+
+def test_str_geometry() -> None:
+    assert str(GeometryType()) == "geometry"
+
+
+def test_repr_geometry() -> None:
+    assert repr(GeometryType()) == "GeometryType(crs=None)"

Review Comment:
   According to the spec, crs can not be None.
   
   <img width="883" height="147" alt="Image" 
src="https://github.com/user-attachments/assets/96011129-7842-4133-b933-0ec55b17938e";
 />



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to