@@ -959,27 +959,33 @@ def read_parquet_metadata_schema(
959959 """
960960 metadata = pl .read_parquet_metadata (source )
961961 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 )
966963 return None
967964
968965
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 :
970975 """Deserialize a schema from a JSON string.
971976
972977 This method allows to dynamically load a schema from its serialization, without
973978 having to know the schema to load in advance.
974979
975980 Args:
976981 data: The JSON string created via :meth:`Schema.serialize`.
982+ strict: Whether to raise an exception if the schema cannot be deserialized.
977983
978984 Returns:
979985 The schema loaded from the JSON data.
980986
981987 Raises:
982- ValueError: If the schema format version is not supported.
988+ ValueError: If the schema format version is not supported and ``strict=True`` .
983989
984990 Attention:
985991 This functionality is considered unstable. It may be changed at any time
@@ -988,10 +994,15 @@ def deserialize_schema(data: str) -> type[Schema]:
988994 See also:
989995 :meth:`Schema.serialize` for additional information on serialization.
990996 """
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
9951006
9961007
9971008def _schema_from_dict (data : dict [str , Any ]) -> type [Schema ]:
0 commit comments