o
    ;i                     @   sJ   d dl mZmZ ddlmZ ddlmZ erddlmZ G dd dZdS )	    )TYPE_CHECKINGOptional   )_Client)config)TaskContextc                   @   s4  e Zd ZU dZdZee ed< dZee	 ed< dZ
ee	 ed< dZed ed< dddddd	ee d
ee	 dee	 ded fddZedefddZede	fddZedee	 fddZed#ddZed$ddZd%ddZd$ddZd$dd Z				d&d	ee d
ee	 dee	 ded dd f
d!d"ZdS )'LoadContextzEncapsulates optional metadata values used during object loading.

    This metadata is set during object construction and propagated through
    parent-child relationships (e.g., App -> Function, Cls -> Obj -> bound methods).
    N_client_environment_name_app_idr   _task_contextclientenvironment_nameapp_idtask_contextr   r   r   r   c                C   s   || _ || _|| _|| _d S Nr	   r
   r   r   selfr   r   r   r    r   G/home/ubuntu/.local/lib/python3.10/site-packages/modal/_load_context.py__init__   s   
zLoadContext.__init__returnc                 C      | j d usJ | j S r   )r	   r   r   r   r   r   $      zLoadContext.clientc                 C   r   r   )r
   r   r   r   r   r   )   r   zLoadContext.environment_namec                 C   s   | j S r   )r   r   r   r   r   r   .   s   zLoadContext.app_idc                 C   s   | j d us	J d| j S )NzLoadContext has no TaskContext)r   r   r   r   r   r   2   s   zLoadContext.task_contextc                 C   s   | ddddS )zCreate an empty LoadContext with all fields set to None.

        Used when loading objects that don't have a parent context.
        N)r   r   r   r   )clsr   r   r   empty7   s   zLoadContext.emptyparentc                 C   s\   t | jdur	| jn|j| jdur| jn|j| jdur| jn|j| jdur)| jdS |jdS )zCreate a new LoadContext with parent values filling in None fields.

        Returns a new LoadContext without mutating self or parent.
        Values from self take precedence over values from parent.
        Nr   )r   r	   r
   r   r   )r   r   r   r   r   merged_with?   s   zLoadContext.merged_withc                    sN   | j duo
| j j }t|r| jnt I dH | jp tdp d| j	| j
dS )zoInfer default client and environment_name if not present

        Returns a new instance (no in place mutation)Nenvironment r   )r	   _snapshottedr   r   r   from_envr
   r   getr   r   )r   is_valid_clientr   r   r   apply_defaultsL   s   zLoadContext.apply_defaultsc                 C   s   d| _ d| _d| _d| _| S )aQ  In-place replace all values with None, such that any inferred values/upgrades
        will work. This is useful in cases where a load context reference may have leaked
        into objects and used/upgraded but you want to make a fresh re-load, e.g. when doing
        multiple `app.run()` calls in the same interpreter session.
        Nr   r   r   r   r   resetY   s
   zLoadContext.resetc                    sR   | j p|pt I dH | _ | jp|ptdpd| _| jp|| _| jp%|| _| S )a  In-place set values if they aren't already set, or set default values

        Intended for Function/Cls hydration specifically

        In those cases, it's important to in-place upgrade/apply_defaults since any "sibling" of the function/cls
        would share the load context with its parent, and the initial load context overrides may not be sufficient
        since an `app.deploy()` etc could get arguments that set a new client etc.

        E.g.
        @app.function()
        def f():
            ...

        f2 = Function.with_options(...)

        with app.run(client=...): # hydrates f and f2 at this point
            ...
        Nr!   r"   )r	   r   r$   r
   r   r%   r   r   r   r   r   r   in_place_upgradee   s   zLoadContext.in_place_upgrade)r   r   )r   r   )r   r   r   r   )NNNN)__name__
__module____qualname____doc__r	   r   r   __annotations__r
   strr   r   r   propertyr   r   r   r   classmethodr   r    r'   r(   r)   r   r   r   r   r      s^   
 



r   N)	typingr   r   r   r   r   _utils.async_utilsr   r   r   r   r   r   <module>   s   