o
    poi                     @   sB   d dl Z d dlmZ d dlmZ d dlmZ G dd deeZdS )    N)Enum)Optional)Literalc                	   @   s   e Zd ZdZeddeded dd fddZeddeded ded  fd	d
Z	edede
e fddZdedefddZdefddZdS )StrEnuma  Type of any enumerator with allowed comparison to string invariant to cases.

    >>> class MySE(StrEnum):
    ...     t1 = "T-1"
    ...     t2 = "T-2"
    >>> MySE("T-1") == MySE.t1
    True
    >>> MySE.from_str("t-2", source="value") == MySE.t2
    True
    >>> MySE.from_str("t-2", source="value")
    <MySE.t2: 'T-2'>
    >>> MySE.from_str("t-3", source="any")
    Traceback (most recent call last):
      ...
    ValueError: Invalid match: expected one of ['t1', 't2', 'T-1', 'T-2'], but got t-3.

    keyvaluesource)r   r   anyreturnc                 C   s|   |dv r| j D ]}| | kr| |   S q|dv r0| j  D ]\}}||kr/| |   S q!td| | d| d)a  Create ``StrEnum`` from a string matching the key or value.

        Args:
            value: matching string
            source: compare with:

                - ``"key"``: validates only from the enum keys, typical alphanumeric with "_"
                - ``"value"``: validates only from the values, could be any string
                - ``"any"``: validates with any key or value, but key has priority

        Raises:
            ValueError:
                if requested string does not match any option based on selected source.

        )r   r	   )r   r	   zInvalid match: expected one of 
, but got .)__members__loweritems
ValueError_allowed_matches)clsr   r   enum_keyenum_val r   R/home/ubuntu/.local/lib/python3.10/site-packages/lightning_utilities/core/enums.pyfrom_str   s   
zStrEnum.from_strc                 C   sF   z|  ||W S  ty"   ttd| | d| d Y dS w )z?Try to create emun and if it does not match any, return `None`.z Invalid string: expected one of r   r   N)r   r   warningswarnUserWarningr   )r   r   r   r   r   r   try_from_str:   s   zStrEnum.try_from_strc                 C   sT   g g }}| j  D ]\}}|| ||j q
|dkr |S |dkr&|S || S )Nr   r   )r   r   appendr   )r   r   keysvalsr   r   r   r   r   r   E   s   

zStrEnum._allowed_matchesotherc                 C   s&   t |tr|j}| j t| kS )zCompare two instances.)
isinstancer   r   r   str)selfr   r   r   r   __eq__Q   s   
zStrEnum.__eq__c                 C   s   t | j S )zReturn unique hash.)hashr   r   )r"   r   r   r   __hash__W   s   zStrEnum.__hash__N)r   )__name__
__module____qualname____doc__classmethodr!   r   r   r   r   listr   objectboolr#   intr%   r   r   r   r   r      s    "
r   )	r   enumr   typingr   typing_extensionsr   r!   r   r   r   r   r   <module>   s
   