@@ -1962,6 +1962,8 @@ def _castPythonToLiteral( # noqa: N802
19621962 (Fraction , (None , _OWL_RATIONAL )),
19631963]
19641964
1965+ _OriginalGenericPythonToXSDRules = list (_GenericPythonToXSDRules )
1966+
19651967_SpecificPythonToXSDRules : List [
19661968 Tuple [Tuple [Type [Any ], str ], Optional [Callable [[Any ], Union [str , bytes ]]]]
19671969] = [
@@ -1973,6 +1975,8 @@ def _castPythonToLiteral( # noqa: N802
19731975 ((bytes , _XSD_B64BINARY ), b64encode ),
19741976]
19751977
1978+ _OriginalSpecificPythonToXSDRules = list (_SpecificPythonToXSDRules )
1979+
19761980XSDToPython : Dict [Optional [str ], Optional [Callable [[str ], Any ]]] = {
19771981 None : None , # plain literals map directly to value space
19781982 URIRef (_XSD_PFX + "time" ): parse_time ,
@@ -2031,32 +2035,52 @@ def _castPythonToLiteral( # noqa: N802
20312035_toPythonMapping .update (XSDToPython )
20322036
20332037
2038+ def _reset_bindings () -> None :
2039+ """
2040+ Reset lexical<->value space binding for `Literal`
2041+ """
2042+ _toPythonMapping .clear ()
2043+ _toPythonMapping .update (XSDToPython )
2044+
2045+ _GenericPythonToXSDRules .clear ()
2046+ _GenericPythonToXSDRules .extend (_OriginalGenericPythonToXSDRules )
2047+
2048+ _SpecificPythonToXSDRules .clear ()
2049+ _SpecificPythonToXSDRules .extend (_OriginalSpecificPythonToXSDRules )
2050+
2051+
20342052def _castLexicalToPython ( # noqa: N802
20352053 lexical : Union [str , bytes ], datatype : Optional [str ]
20362054) -> Any :
20372055 """
20382056 Map a lexical form to the value-space for the given datatype
20392057 :returns: a python object for the value or ``None``
20402058 """
2041- convFunc = _toPythonMapping .get (datatype , False ) # noqa: N806
2042- if convFunc :
2043- if TYPE_CHECKING :
2044- # NOTE: This is here because convFunc is seen as
2045- # Union[Callable[[str], Any], bool, None]
2046- # even though it will never have value of True.
2047- assert not isinstance (convFunc , bool )
2048- convFunc
2059+ try :
2060+ conv_func = _toPythonMapping [datatype ]
2061+ except KeyError :
2062+ # no conv_func -> unknown data-type
2063+ return None
2064+
2065+ if conv_func is not None :
20492066 try :
20502067 # type error: Argument 1 has incompatible type "Union[str, bytes]"; expected "str"
20512068 # NOTE for type ignore: various functions in _toPythonMapping will
20522069 # only work for str, so there is some inconsistency here, the right
20532070 # approach may be to change lexical to be of str type but this will
20542071 # require runtime changes.
2055- return convFunc (lexical ) # type: ignore[arg-type]
2056- except :
2072+ return conv_func (lexical ) # type: ignore[arg-type]
2073+ except Exception :
2074+ logger .warning (
2075+ "Failed to convert Literal lexical form to value. Datatype=%s, "
2076+ "Converter=%s" ,
2077+ datatype ,
2078+ conv_func ,
2079+ exc_info = True ,
2080+ )
20572081 # not a valid lexical representation for this dt
20582082 return None
2059- elif convFunc is None :
2083+ else :
20602084 # no conv func means 1-1 lexical<->value-space mapping
20612085 try :
20622086 return str (lexical )
@@ -2065,9 +2089,6 @@ def _castLexicalToPython( # noqa: N802
20652089 # NOTE for type ignore: code assumes that lexical is of type bytes
20662090 # at this point.
20672091 return str (lexical , "utf-8" ) # type: ignore[arg-type]
2068- else :
2069- # no convFunc - unknown data-type
2070- return None
20712092
20722093
20732094_AnyT = TypeVar ("_AnyT" , bound = Any )
0 commit comments