#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2022, Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------""" Provide ``Nullable`` and ``NonNullable`` properties. """#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromtypingimportAny# Bokeh importsfrom._sphinximportproperty_link,register_type_link,type_linkfrom.basesimportSingleParameterizedPropertyfrom.singletonsimportUndefined#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=("NonNullable","Nullable",)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------
[docs]classNullable(SingleParameterizedProperty):""" A property accepting ``None`` or a value of some other type. """def__init__(self,type_param,*,default=None,help=None,serialized=None,readonly=False)->None:super().__init__(type_param,default=default,help=help,serialized=serialized,readonly=readonly)deffrom_json(self,json,*,models=None):returnNoneifjsonisNoneelsesuper().from_json(json,models=models)deftransform(self,value):returnNoneifvalueisNoneelsesuper().transform(value)defwrap(self,value):returnNoneifvalueisNoneelsesuper().wrap(value)defvalidate(self,value:Any,detail:bool=True)->None:ifvalueisNone:returntry:super().validate(value,detail=False)exceptValueError:passelse:returnmsg=""ifnotdetailelsef"expected either None or a value of type {self.type_param}, got {value!r}"raiseValueError(msg)
[docs]classNonNullable(SingleParameterizedProperty):""" A property accepting a value of some other type while having undefined default. """def__init__(self,type_param,*,default=Undefined,help=None,serialized=None,readonly=False)->None:super().__init__(type_param,default=default,help=help,serialized=serialized,readonly=readonly)
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------@register_type_link(Nullable)@register_type_link(NonNullable)def_sphinx_type_link(obj):returnf"{property_link(obj)}({type_link(obj.type_param)})"