@@ -1962,6 +1962,8 @@ def _castPythonToLiteral( # noqa: N802
1962
1962
(Fraction , (None , _OWL_RATIONAL )),
1963
1963
]
1964
1964
1965
+ _OriginalGenericPythonToXSDRules = list (_GenericPythonToXSDRules )
1966
+
1965
1967
_SpecificPythonToXSDRules : List [
1966
1968
Tuple [Tuple [Type [Any ], str ], Optional [Callable [[Any ], Union [str , bytes ]]]]
1967
1969
] = [
@@ -1973,6 +1975,8 @@ def _castPythonToLiteral( # noqa: N802
1973
1975
((bytes , _XSD_B64BINARY ), b64encode ),
1974
1976
]
1975
1977
1978
+ _OriginalSpecificPythonToXSDRules = list (_SpecificPythonToXSDRules )
1979
+
1976
1980
XSDToPython : Dict [Optional [str ], Optional [Callable [[str ], Any ]]] = {
1977
1981
None : None , # plain literals map directly to value space
1978
1982
URIRef (_XSD_PFX + "time" ): parse_time ,
@@ -2031,32 +2035,52 @@ def _castPythonToLiteral( # noqa: N802
2031
2035
_toPythonMapping .update (XSDToPython )
2032
2036
2033
2037
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
+
2034
2052
def _castLexicalToPython ( # noqa: N802
2035
2053
lexical : Union [str , bytes ], datatype : Optional [str ]
2036
2054
) -> Any :
2037
2055
"""
2038
2056
Map a lexical form to the value-space for the given datatype
2039
2057
:returns: a python object for the value or ``None``
2040
2058
"""
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 :
2049
2066
try :
2050
2067
# type error: Argument 1 has incompatible type "Union[str, bytes]"; expected "str"
2051
2068
# NOTE for type ignore: various functions in _toPythonMapping will
2052
2069
# only work for str, so there is some inconsistency here, the right
2053
2070
# approach may be to change lexical to be of str type but this will
2054
2071
# 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
+ )
2057
2081
# not a valid lexical representation for this dt
2058
2082
return None
2059
- elif convFunc is None :
2083
+ else :
2060
2084
# no conv func means 1-1 lexical<->value-space mapping
2061
2085
try :
2062
2086
return str (lexical )
@@ -2065,9 +2089,6 @@ def _castLexicalToPython( # noqa: N802
2065
2089
# NOTE for type ignore: code assumes that lexical is of type bytes
2066
2090
# at this point.
2067
2091
return str (lexical , "utf-8" ) # type: ignore[arg-type]
2068
- else :
2069
- # no convFunc - unknown data-type
2070
- return None
2071
2092
2072
2093
2073
2094
_AnyT = TypeVar ("_AnyT" , bound = Any )
0 commit comments