Skip to content

Commit 49c68d3

Browse files
committed
Fix first batch of suggestions
1 parent a8cc06c commit 49c68d3

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

pyiceberg/types.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,22 @@ def _parse_decimal_type(decimal: Any) -> Tuple[int, int]:
8484
else:
8585
raise ValidationError(f"Could not parse {decimal} into a DecimalType")
8686
elif isinstance(decimal, dict):
87+
_raise_if_any_missing_dictionary_key(decimal, "DecimalType", "precision", "scale")
8788
return decimal["precision"], decimal["scale"]
8889
else:
8990
return decimal
9091

92+
93+
def _parse_fixed_type(fixed: Any) -> int:
94+
if isinstance(fixed, str):
95+
return FIXED_PARSER.match(fixed)
96+
elif isinstance(fixed, dict):
97+
_raise_if_any_missing_dictionary_key(fixed, "FixedType", "length")
98+
return fixed["length"]
99+
else:
100+
return fixed
101+
102+
91103
def _parse_geography_type(geography: Any) -> Tuple[str, GeographyType.EdgeAlgorithm]:
92104
if isinstance(geography, str):
93105
matches = GEOGRAPHY_REGEX.search(geography)
@@ -102,10 +114,12 @@ def _parse_geography_type(geography: Any) -> Tuple[str, GeographyType.EdgeAlgori
102114
else:
103115
raise ValidationError(f"Could not parse {geography} into a GeographyType")
104116
elif isinstance(geography, dict):
117+
_raise_if_any_missing_dictionary_key(geography, "GeographyType", "crs", "edge_algorithm")
105118
return geography["crs"], geography["edge_algorithm"]
106119
else:
107120
return geography
108121

122+
109123
def _parse_geometry_type(geometry: Any) -> str:
110124
if isinstance(geometry, str):
111125
matches = GEOMETRY_REGEX.search(geometry)
@@ -117,17 +131,20 @@ def _parse_geometry_type(geometry: Any) -> str:
117131
else:
118132
raise ValidationError(f"Could not parse {geometry} into a GeometryType")
119133
elif isinstance(geometry, dict):
134+
_raise_if_any_missing_dictionary_key(geometry, "GeometryType", "crs")
120135
return geometry["crs"]
121136
else:
122137
return geometry
123138

124-
def _parse_fixed_type(fixed: Any) -> int:
125-
if isinstance(fixed, str):
126-
return FIXED_PARSER.match(fixed)
127-
elif isinstance(fixed, dict):
128-
return fixed["length"]
129-
else:
130-
return fixed
139+
140+
def _raise_if_any_missing_dictionary_key(d: Dict, expected_type: str, *keys: str):
141+
missing_keys = []
142+
for key in keys:
143+
if key not in d:
144+
missing_keys.append(key)
145+
if len(missing_keys) == 0:
146+
return
147+
raise ValidationError(f"Missing required key(s): {', '.join(missing_keys)} for expected_type")
131148

132149

133150
def strtobool(val: str) -> bool:
@@ -954,7 +971,7 @@ def edge_algorithm(self) -> EdgeAlgorithm:
954971

955972
def __repr__(self) -> str:
956973
"""Return the string representation of the GeographyType class."""
957-
return f"GeographyType(crs={self.crs}, edge_algorithm={self.edge_algorithm})"
974+
return f"GeographyType(crs={self.crs or GeographyType.default_crs}, edge_algorithm={self.edge_algorithm or GeographyType.EdgeAlgorithm.SPHERICAL.value})"
958975

959976
def __str__(self) -> str:
960977
"""Return the string representation."""
@@ -963,7 +980,7 @@ def __str__(self) -> str:
963980
return f"geography"
964981
else:
965982
return f"geometry({self.crs})"
966-
return f"geography({self.crs or GeographyType.default_crs}, {self.edge_algorithm})"
983+
return f"geography({self.crs or GeographyType.default_crs}, {self.edge_algorithm.value})"
967984

968985
def __hash__(self) -> int:
969986
"""Return the hash of the crs."""
@@ -1010,7 +1027,7 @@ def crs(self) -> str:
10101027

10111028
def __repr__(self) -> str:
10121029
"""Return the string representation of the GeometryType class."""
1013-
return f"GeometryType(crs={self.crs})"
1030+
return f"GeometryType(crs={self.crs or GeometryType.default_crs})"
10141031

10151032
def __str__(self) -> str:
10161033
"""Return the string representation."""

tests/test_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def test_str_geography() -> None:
516516

517517

518518
def test_repr_geography() -> None:
519-
assert repr(GeographyType()) == "GeographyType(crs=None, edge_algorithm=None)"
519+
assert repr(GeographyType()) == "GeographyType(crs=OGC:CRS84, edge_algorithm=spherical)"
520520

521521

522522
def test_serialization_geometry() -> None:
@@ -533,7 +533,7 @@ def test_str_geometry() -> None:
533533

534534

535535
def test_repr_geometry() -> None:
536-
assert repr(GeometryType()) == "GeometryType(crs=None)"
536+
assert repr(GeometryType()) == "GeometryType(crs=OGC:CRS84)"
537537

538538

539539
def test_serialization_decimal() -> None:

0 commit comments

Comments
 (0)