@@ -959,27 +959,33 @@ def read_parquet_metadata_schema(
959
959
"""
960
960
metadata = pl .read_parquet_metadata (source )
961
961
if (schema_metadata := metadata .get (SCHEMA_METADATA_KEY )) is not None :
962
- try :
963
- return deserialize_schema (schema_metadata )
964
- except (JSONDecodeError , plexc .ComputeError ):
965
- return None
962
+ return deserialize_schema (schema_metadata , strict = False )
966
963
return None
967
964
968
965
969
- def deserialize_schema (data : str ) -> type [Schema ]:
966
+ @overload
967
+ def deserialize_schema (data : str , strict : Literal [True ] = True ) -> type [Schema ]: ...
968
+
969
+
970
+ @overload
971
+ def deserialize_schema (data : str , strict : Literal [False ]) -> type [Schema ] | None : ...
972
+
973
+
974
+ def deserialize_schema (data : str , strict : bool = True ) -> type [Schema ] | None :
970
975
"""Deserialize a schema from a JSON string.
971
976
972
977
This method allows to dynamically load a schema from its serialization, without
973
978
having to know the schema to load in advance.
974
979
975
980
Args:
976
981
data: The JSON string created via :meth:`Schema.serialize`.
982
+ strict: Whether to raise an exception if the schema cannot be deserialized.
977
983
978
984
Returns:
979
985
The schema loaded from the JSON data.
980
986
981
987
Raises:
982
- ValueError: If the schema format version is not supported.
988
+ ValueError: If the schema format version is not supported and ``strict=True`` .
983
989
984
990
Attention:
985
991
This functionality is considered unstable. It may be changed at any time
@@ -988,10 +994,15 @@ def deserialize_schema(data: str) -> type[Schema]:
988
994
See also:
989
995
:meth:`Schema.serialize` for additional information on serialization.
990
996
"""
991
- decoded = json .loads (data , cls = SchemaJSONDecoder )
992
- if (format := decoded ["versions" ]["format" ]) != SERIALIZATION_FORMAT_VERSION :
993
- raise ValueError (f"Unsupported schema format version: { format } " )
994
- return _schema_from_dict (decoded )
997
+ try :
998
+ decoded = json .loads (data , cls = SchemaJSONDecoder )
999
+ if (format := decoded ["versions" ]["format" ]) != SERIALIZATION_FORMAT_VERSION :
1000
+ raise ValueError (f"Unsupported schema format version: { format } " )
1001
+ return _schema_from_dict (decoded )
1002
+ except (ValueError , JSONDecodeError , plexc .ComputeError ) as e :
1003
+ if strict :
1004
+ raise e from e
1005
+ return None
995
1006
996
1007
997
1008
def _schema_from_dict (data : dict [str , Any ]) -> type [Schema ]:
0 commit comments