#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2023, 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,TypeVar,Union# Bokeh importsfrom...util.deprecationimportdeprecatedfrom._sphinximportproperty_link,register_type_link,type_linkfrom.basesimport(Init,Property,SingleParameterizedProperty,TypeOrInst,)from.requiredimportRequiredfrom.singletonsimportUndefined#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=("NonNullable","Nullable",)T=TypeVar("T")#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------
[docs]classNullable(SingleParameterizedProperty[Union[T,None]]):""" A property accepting ``None`` or a value of some other type. """def__init__(self,type_param:TypeOrInst[Property[T]],*,default:Init[T|None]=None,help:str|None=None)->None:super().__init__(type_param,default=default,help=help)deftransform(self,value:Any)->T|None:returnNoneifvalueisNoneelsesuper().transform(value)defwrap(self,value:Any)->Any: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(Required[T]):""" A property accepting a value of some other type while having undefined default. .. deprecated:: 3.0.0 Use ``bokeh.core.property.required.Required`` instead. """def__init__(self,type_param:TypeOrInst[Property[T]],*,default:Init[T]=Undefined,help:str|None=None)->None:deprecated((3,0,0),"NonNullable(Type)","Required(Type)")super().__init__(type_param,default=default,help=help)
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------@register_type_link(Nullable)@register_type_link(NonNullable)def_sphinx_type_link(obj:SingleParameterizedProperty[Any])->str:returnf"{property_link(obj)}({type_link(obj.type_param)})"