o
    <ir
                     @   s8   d Z ddlmZmZ dedefddZG dd dZd	S )
a  
This is a singleton metaclass that can be used to cache and reuse existing objects.

In the Iceberg codebase we have a lot of objects that are stateless (for example Types such as StringType,
BooleanType etc). FixedTypes have arguments (eg. Fixed[22]) that we also make part of the key when caching
the newly created object.

The Singleton uses a metaclass which essentially defines a new type. When the Type gets created, it will first
evaluate the `__call__` method with all the arguments. If we already initialized a class earlier, we'll just
return it.

More information on metaclasses: https://docs.python.org/3/reference/datamodel.html#metaclasses
    )AnyClassVarelementreturnc                 C   s<   t | trtdd |  D S t | trttt| S | S )Nc                 s   s$    | ]\}}t |t |fV  qd S N)_convert_to_hashable_type).0kv r   V/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/pyiceberg/utils/singleton.py	<genexpr>$   s   " z,_convert_to_hashable_type.<locals>.<genexpr>)
isinstancedicttupleitemslistmapr   )r   r   r   r   r   "   s
   

r   c                       sH   e Zd ZU i Zee ed<  fddZdeee	f de	fddZ
  ZS )	Singleton
_instancesc                    s8   | t |t|f}|| jvrt | | j|< | j| S r   )r   r   r   super__new__)clsargskwargskey	__class__r   r   r   -   s   

zSingleton.__new__memor   c                 C   s   | S )a  
        Prevent deep copy operations for singletons.

        The IcebergRootModel inherits from Pydantic RootModel,
        which has its own implementation of deepcopy. When deepcopy
        runs, it calls the RootModel __deepcopy__ method and ignores
        that it's a Singleton. To handle this, the order of inheritance
        is adjusted and a __deepcopy__ method is implemented for
        singletons that simply returns itself.
        r   )selfr   r   r   r   __deepcopy__3   s   zSingleton.__deepcopy__)__name__
__module____qualname__r   r   r   __annotations__r   intr   r    __classcell__r   r   r   r   r   *   s   
 "r   N)__doc__typingr   r   r   r   r   r   r   r   <module>   s   