U
    /eQI                     @  s  d Z ddlmZ ddlZeeZddlZddlmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZ ddlZddlmZ ddlmZ d	d
lmZ ddlmZmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z( e
rddl)m*Z* d	dlm+Z+ edZ,dZ-edZ.eee. e.f Z/ee.e(e&f Z0G dd de1Z2G dd de!e. Z3ede3e dZ4G dd de3e4 Z5G dd de5e. Z6G dd  d e3e. Z7G d!d" d"e5e. Z8d#d$d%d&Z9ee6d'd( Z:dS ))al   Provide base classes for the Bokeh property system.

.. note::
    These classes form part of the very low-level machinery that implements
    the Bokeh model and property system. It is unlikely that any of these
    classes or their methods will be applicable to any standard usage or to
    anyone who is not directly developing on Bokeh's own infrastructure.

    )annotationsN)copy)
TYPE_CHECKINGAnyCallableClassVarDictListTupleTypeTypeVarUnion   )import_optional)	nice_join   )HasProps   )property_linkregister_type_link	type_link)PropertyDescriptorFactoryPropertyDescriptor)	IntrinsicIntrinsicType	UndefinedUndefinedType)DocumentPatchedEvent)IDZpandas)ContainerPropertyDeserializationErrorPrimitivePropertyPropertyvalidation_onTc                   @  s   e Zd ZdS )r!   N)__name__
__module____qualname__ r)   r)   =/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/core/property/bases.pyr!   ^   s   r!   c                   @  s  e Zd ZU dZdZded< ded< ded< d	ed
< edddfdddddddZddddZdddddZ	ddddZ
edddd d!Zddd"d#Zd$dd%dd&d'd(Zeddd)d*Zeddd+d,Zdddd-d.d/Zdd0d1d2dd3d4d5Zdd1d6d7d8Zd1dd6d9d:ZdYd1dd;d<d=d>Zd1dd6d?d@Zddd6dAdBZd1dCd1dDdEdFZddGdHdd1dCddIdJdKZedddLdMZdNdOdPdQdRdSZdTdUdPdVdWdXZdS )Zr#   a   Base class for Bokeh property instances, which can be added to Bokeh
    Models.

    Args:
        default (obj, optional) :
            A default value for attributes created from this property to have.

        help (str or None, optional) :
            A documentation string for this property. It will be automatically
            used by the :ref:`bokeh.sphinxext.bokeh_prop` extension when
            generating Spinx documentation. (default: None)

        serialized (bool, optional) :
            Whether attributes created from this property should be included
            in serialization (default: True)

        readonly (bool, optional) :
            Whether attributes created from this property are read-only.
            (default: False)

    TzClassVar[bool]_should_validatebool	_readonlyz8List[Tuple[Property[Any], Callable[[Property[Any]], T]]]alternativeszTList[Tuple[Callable[[HasProps, T], bool], str | Callable[[HasProps, str, T], None]]]
assertionsNFInit[T]
str | Nonebool | Nonedefaulthelp
serializedreadonlyc                 C  s^   |t k	r|nt}|d kr0|r(|tkr(dnd| _n|| _|| _|| _|| _|| _g | _g | _d S )NFT)	r   r   _serializedr-   _default_help__doc__r.   r/   )selfr4   r5   r6   r7   r)   r)   r*   __init__   s    zProperty.__init__strreturnc                 C  s   | j jS N)	__class__r&   r<   r)   r)   r*   __str__   s    zProperty.__str__zList[PropertyDescriptor[T]])namer@   c                 C  s   t || gS )a   Return a list of ``PropertyDescriptor`` instances to install
        on a class, in order to delegate attribute access to this property.

        Args:
            name (str) : the name of the property these descriptors are for

        Returns:
            list[PropertyDescriptor]

        The descriptors returned are collected by the ``MetaHasProps``
        metaclass and added to ``HasProps`` subclasses during class creation.
        r   )r<   rE   r)   r)   r*   make_descriptors   s    zProperty.make_descriptorsc                 C  s   t | jtjS )z False if we have a default that is immutable, and will be the
        same every time (some defaults are generated on demand by a function
        to be called).

        )
isinstancer9   typesFunctionTyperC   r)   r)   r*   _may_have_unstable_default   s    z#Property._may_have_unstable_defaultzCallable[[], T] | Tr%   )r4   r@   c                 C  s   t |tjst|S | S dS )zj Return a copy of the default, or a new value if the default
        is specified by a function.

        N)rG   rH   rI   r   )clsr4   r)   r)   r*   _copy_default   s    zProperty._copy_defaultc                 C  s   |  | jS )z Return the untransformed default value.

        The raw_default() needs to be validated and transformed by
        prepare_value() before use, and may also be replaced later by
        subclass overrides or by themes.

        )rL   r9   rC   r)   r)   r*   _raw_default   s    zProperty._raw_defaultzType[HasProps]zDict[str, Any] | None)rK   rE   theme_overridesr@   c                 C  sJ   |}|dks||kr|  }||kr4| || }n|  }| |||S )zO The default, transformed by prepare_value() and the theme overrides.

        N)Z_overridden_defaultsrL   rM   prepare_value)r<   rK   rE   rN   Z	overridesr4   r)   r)   r*   themed_default   s    zProperty.themed_defaultc                 C  s   | j S )z Whether the property should be serialized when serializing an object.

        This would be False for a "virtual" or "convenience" property that duplicates
        information already available in other properties, for example.
        )r8   rC   r)   r)   r*   r6      s    zProperty.serializedc                 C  s   | j S )z Whether this property is read-only.

        Read-only properties may only be modified by the client (i.e., by BokehJS
        in the browser).

        )r-   rC   r)   r)   r*   r7      s    zProperty.readonly)newoldr@   c                   s   t  tjst tjr$t S trpt  tjs@t tjrLt S t  tjsdt tjrpt S zXt  trt trt 	 t	 krW dS t
 fdd D W S  kW S  tk
r   Y dS X dS )aj   Whether two parameters match values.

        If either ``new`` or ``old`` is a NumPy array or Pandas Series or Index,
        then the result of ``np.array_equal`` will determine if the values match.

        Otherwise, the result of standard Python equality will be returned.

        Returns:
            True, if new and old match, False otherwise

        Fc                 3  s"   | ]}  | | V  qd S rA   )matches).0krQ   rR   r<   r)   r*   	<genexpr>   s     z#Property.matches.<locals>.<genexpr>N)rG   npZndarrayZarray_equalpdZSeriesZIndexdictsetkeysall
ValueError)r<   rQ   rR   r)   rV   r*   rS      s    
zProperty.matchesmodelsr   zDict[ID, HasProps] | Nonejsonr`   r@   c                C  s   |S )z Convert from JSON-compatible values into a value for this property.

        JSON-compatible values are: list, dict, number, string, bool, None

        r)   r<   rb   r`   r)   r)   r*   	from_json	  s    zProperty.from_jsonvaluer@   c                 C  s   |S )z< Change the value into a JSON serializable format.

        r)   r<   rf   r)   r)   r*   serialize_value  s    zProperty.serialize_valuec                 C  s   |S )z Change the value into the canonical format for this property.

        Args:
            value (obj) : the value to apply transformation to.

        Returns:
            obj: transformed value

        r)   rg   r)   r)   r*   	transform  s    
zProperty.transformNonerf   detailr@   c                 C  s   dS )a   Determine whether we can set this property from this value.

        Validation happens before transform()

        Args:
            value (obj) : the value to validate against this property type
            detail (bool, options) : whether to construct detailed exceptions

                Generating detailed type validation error messages can be
                expensive. When doing type checks internally that will not
                escape exceptions to users, these messages can be skipped
                by setting this value to False (default: True)

        Returns:
            None

        Raises:
            ValueError if the value is not valid for this property type

        Nr)   r<   rf   rl   r)   r)   r*   validate#  s    zProperty.validatec                 C  s6   zt  r| |d W n tk
r,   Y dS X dS dS )z Whether the value passes validation

        Args:
            value (obj) : the value to validate against this property type

        Returns:
            True if valid, False otherwise

        FTN)r$   rn   r^   rg   r)   r)   r*   is_valid:  s    
zProperty.is_validc                 C  s   |S )zT Some property types need to wrap their values in special containers, etc.

        r)   rg   r)   r)   r*   wrapL  s    zProperty.wrapzDocumentPatchedEvent | None)rf   hintr@   c                 C  s   |S rA   r)   )r<   rf   rq   r)   r)   r*   _hinted_valueR  s    zProperty._hinted_value)rq   zHasProps | Type[HasProps])ownerrE   rf   rq   r@   c             
   C  sJ  |t kr|  }|tkr|S d }z t r>| ||}| | W nL tk
r } z.| jD ]\}}	||rX|	|} q|qX|}W 5 d }~X Y nX |d kr| 	|}n.t
|tr|n|j}
td|
 d| d| t
|tr@|}| jD ]X\}}t
|tr|}n
|||}t
|tst|st
|tr2t|q|||| q| |S )Nzfailed to validate .z: )r   rM   r   r$   rr   rn   r^   r.   ro   ri   rG   r   r&   r/   r,   AssertionErrorr>   rp   )r<   rs   rE   rf   rq   errorZhinted_valueetp	converterZobj_reprobjfn	msg_or_fnresultr)   r)   r*   rO   U  s>    



zProperty.prepare_valuec                 C  s   dS )NFr)   rC   r)   r)   r*   has_ref  s    zProperty.has_refTypeOrInst[Property[Any]]zCallable[[Property[Any]], T]zProperty[T])rx   ry   r@   c                 C  s   t |}| j||f | S )a   Declare that other types may be converted to this property type.

        Args:
            tp (Property) :
                A type that may be converted automatically to this property
                type.

            converter (callable) :
                A function accepting ``value`` to perform conversion of the
                value to this property type.

        Returns:
            self

        )ParameterizedProperty_validate_type_paramr.   append)r<   rx   ry   r)   r)   r*   accepts  s    
zProperty.acceptszCallable[[HasProps, T], bool]z(str | Callable[[HasProps, str, T], None])r{   r|   r@   c                 C  s   | j ||f | S )a   Assert that prepared values satisfy given conditions.

        Assertions are intended in enforce conditions beyond simple value
        type validation. For instance, this method can be use to assert that
        the columns of a ``ColumnDataSource`` all collectively have the same
        length at all times.

        Args:
            fn (callable) :
                A function accepting ``(obj, value)`` that returns True if the value
                passes the assertion, or False otherwise.

            msg_or_fn (str or callable) :
                A message to print in case the assertion fails, or a function
                accepting ``(obj, name, value)`` to call in in case the assertion
                fails.

        Returns:
            self

        )r/   r   )r<   r{   r|   r)   r)   r*   asserts  s    zProperty.asserts)T)r&   r'   r(   r;   r+   __annotations__r   r=   rD   rF   rJ   classmethodrL   rM   rP   propertyr6   r7   rS   rd   rh   ri   rn   ro   rp   rr   rO   r~   r   r   r)   r)   r)   r*   r#   a   sB   
 

	(-r#   TItem)boundc                   @  sP   e Zd ZdZedddddddd	Zed
dddZeddddZdS )r   zT A base class for Properties that have type parameters, e.g. ``List(String)``.

    F)help_allowedr   r,   zProperty[Any])
type_paramr   r@   c                C  sX   t | tr"t| tr|  S | j} n$t | trF| jd k	rB|sBtd| S td|  d S )Nz4setting 'help' on type parameters doesn't make sensez+expected a Property as type parameter, got )rG   type
issubclassr#   r&   r:   r^   )r   r   r)   r)   r*   r     s    


z*ParameterizedProperty._validate_type_paramList[Property[Any]]r?   c                 C  s   t dd S )Nzabstract method)NotImplementedErrorrC   r)   r)   r*   type_params  s    z!ParameterizedProperty.type_paramsc                 C  s   t dd | jD S )Nc                 s  s   | ]}|j V  qd S rA   )r~   )rT   r   r)   r)   r*   rW     s     z0ParameterizedProperty.has_ref.<locals>.<genexpr>)anyr   rC   r)   r)   r*   r~     s    zParameterizedProperty.has_refN)	r&   r'   r(   r;   staticmethodr   r   r   r~   r)   r)   r)   r*   r     s   r   c                      s   e Zd ZdZeddddddddd	d
 fddZeddddZddddZd'dd	dd fddZ	ddddddddZ
ddd d!d"Zddd d#d$Zd	dd%d&Z  ZS )(SingleParameterizedPropertyz8 A parameterized property with a single type parameter. NFr3   r   r0   r1   r2   r,   )r   r4   r5   r6   r7   c                  s:   |  || _|tk	r|n| j }t j||||d d S )Nr3   )r   r   r   rM   superr=   )r<   r   r4   r5   r6   r7   rB   r)   r*   r=     s    z$SingleParameterizedProperty.__init__r   r?   c                 C  s   | j gS rA   )r   rC   r)   r)   r*   r     s    z'SingleParameterizedProperty.type_paramsr>   c                 C  s   | j j d| j dS N())rB   r&   r   rC   r)   r)   r*   rD     s    z#SingleParameterizedProperty.__str__Tr   rj   rk   c                   s$   t  j||d | jj||d d S )N)rl   )r   rn   r   rm   r   r)   r*   rn     s    z$SingleParameterizedProperty.validater_   Dict[str, HasProps] | Noner%   ra   c                C  s   | j j||dS )Nr_   )r   rd   rc   r)   r)   r*   rd     s    z%SingleParameterizedProperty.from_jsonre   c                 C  s   | j |S rA   )r   ri   rg   r)   r)   r*   ri     s    z%SingleParameterizedProperty.transformc                 C  s   | j |S rA   )r   rp   rg   r)   r)   r*   rp     s    z SingleParameterizedProperty.wrapc                 C  s
   | j  S rA   )r   rJ   rC   r)   r)   r*   rJ     s    z6SingleParameterizedProperty._may_have_unstable_default)T)r&   r'   r(   r;   r   r=   r   r   rD   rn   rd   ri   rp   rJ   __classcell__r)   r)   r   r*   r     s     r   c                      sN   e Zd ZU dZded< ddddd fd	d
ZddddddddZ  ZS )r"   a   A base class for simple property types.

    Subclasses should define a class attribute ``_underlying_type`` that is
    a tuple of acceptable type values for the property.

    Example:

        A trivial version of a ``Float`` property might look like:

        .. code-block:: python

            class Float(PrimitiveProperty):
                _underlying_type = (numbers.Real,)

    zClassVar[Tuple[Type[T]]]_underlying_typeTr   r,   rj   rk   c                   sf   t  || t|| jrd S |s*tdtdd | jD }d| d| dt|j }t|d S )N c                 S  s   g | ]
}|j qS r)   r&   rT   rK   r)   r)   r*   
<listcomp>  s     z.PrimitiveProperty.validate.<locals>.<listcomp>zexpected a value of type , got 	 of type )r   rn   rG   r   r^   r   r   r&   )r<   rf   rl   expected_typemsgr   r)   r*   rn     s    zPrimitiveProperty.validateNr_   r   r%   ra   c                C  sP   t || jr|S tdd | jD }|  d| d| dt|j }t|d S )Nc                 S  s   g | ]
}|j qS r)   r   r   r)   r)   r*   r     s     z/PrimitiveProperty.from_json.<locals>.<listcomp>z
 expected r   r   )rG   r   r   r   r&   r!   )r<   rb   r`   r   r   r)   r)   r*   rd     s
     zPrimitiveProperty.from_json)T)r&   r'   r(   r;   r   rn   rd   r   r)   r)   r   r*   r"     s   
r"   c                   @  s   e Zd ZdZddddZdS )r    z7 A base class for Container-like type properties.

    r,   r?   c                 C  s   dS )NTr)   rC   r)   r)   r*   rJ     s    z,ContainerProperty._may_have_unstable_defaultN)r&   r'   r(   r;   rJ   r)   r)   r)   r*   r      s   r    r,   r?   c                   C  s   t jS )zR Check if property validation is currently active

    Returns:
        bool

    )r#   r+   r)   r)   r)   r*   r$   "  s    r$   c                 C  s   t |  dt| j dS r   )r   r   r   )rz   r)   r)   r*   _sphinx_type3  s    r   );r;   
__future__r   logging	getLoggerr&   logrH   r   typingr   r   r   r   r   r	   r
   r   r   r   ZnumpyrX   Zutil.dependenciesr   Zutil.stringr   Z	has_propsr   Z_sphinxr   r   r   Zdescriptor_factoryr   Zdescriptorsr   Z
singletonsr   r   r   r   Zdocument.eventsr   r   rY   __all__r%   Z
TypeOrInstZInit	Exceptionr!   r#   r   r   r   r"   r    r$   r   r)   r)   r)   r*   <module>   sD   
0  U '	