
    oi                    :   d Z ddlmZ ddlZddlZddlZddlmZ ddl	m
Z
 ddlmZmZ ej                  rddlmZ ddl	mZ  G d	 d
eeej$                  f         Z ej(                  d      Z ej(                  d      Z ej(                  d      Z	 d	 	 	 	 	 ddZy)	Utilities    )annotationsN)Mappingcopy_context)partialwraps)Callable)Contextc                  (    e Zd ZdZd Zd Zd Zd Zy)LazyDictzLazy evaluated read-only dictionary.

    Initialised with a dictionary of key-value pairs where the values are either
    constants or callables. Callables are evaluated each time the respective item is
    read.
    c                    || _         y N)_dict)selfdicts     W/home/ubuntu/docker-apps/notebooks/venv/lib/python3.12/site-packages/ipykernel/utils.py__init__zLazyDict.__init__   s	    
    c                `    | j                   j                  |      }t        |      r |       S |S r   )r   getcallable)r   keyitems      r   __getitem__zLazyDict.__getitem__   s'    zz~~c"!$tv1T1r   c                ,    t        | j                        S r   )lenr   r   s    r   __len__zLazyDict.__len__    s    4::r   c                ,    t        | j                        S r   )iterr   r   s    r   __iter__zLazyDict.__iter__#   s    DJJr   N)__name__
__module____qualname____doc__r   r   r   r"    r   r   r   r      s    2 r   r   TUVc                     
t               t        j                  dk\  rt                fd       }|S gfdt                fd       }|S )z
    Wrapper to run a coroutine in a persistent ContextVar Context.

    Backports asyncio.create_task(context=...) behavior from Python 3.11
    )      c                 ^   K    | i |}t        j                  |       d {   S 7 w)N)context)asynciocreate_task)argskwargscoror/   fs      r   run_in_contextz)_async_in_context.<locals>.run_in_context9   s/     d%f%D ,,T7CCCCs   #-+-c                p   K   	  | |i | d{   t               d<   S 7 # t               d<   w xY ww)z;call a coroutine, preserving the context after it is calledNr   r   )r5   r2   r3   context_holders      r   preserve_contextz+_async_in_context.<locals>.preserve_contextD   s>     	/D+F++ !-N1 , !-N1s    6$ "$ 6$ 36c            
        K   d   }|j                  t        t        j                   g| i |             d {   S 7 w)Nr   )runr   r0   r1   )r2   r3   ctxr8   r5   r9   s      r   run_in_context_pre311z0_async_in_context.<locals>.run_in_context_pre311L   sD     QWWWW%8%8:J1:^t:^W]:^_````s   <AA A)r   sysversion_infor	   )r5   r/   r6   r=   r8   r9   s   ``  @@r   _async_in_contextr@   ,   so     .
7"	q	D 
	D  YN/ 1Xa a ! r   r   )r5   #Callable[..., t.Coroutine[T, U, V]]r/   zContext | NonereturnrA   )r&   
__future__r   r0   r>   typingtcollections.abcr   contextvarsr   	functoolsr   r	   TYPE_CHECKINGr
   r   strAnyr   TypeVarr(   r)   r*   r@   r'   r   r   <module>rM      s     "  
  # $ $??(# wsAEEz"  , AIIcNAIIcNAIIcN GK%!*%!5C%!(%!r   