o
    ×•`ß  ã                   @   sn   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
 es&ddlZddlZg d¢Zdd	„ Zedfd
d„ZdS )z
Commonly useful converters.
é    )Úabsolute_importÚdivisionÚprint_functioné   )ÚPY2)ÚNOTHINGÚFactoryÚpipeN)r	   ÚoptionalÚdefault_if_nonec              	      sœ   ‡ fdd„}t sLd}zt ˆ ¡}W n ttfy   Y nw |rLt|j ¡ ƒ}|r<|d jtj	j
ur<tj|d j |jd< |jtjj
urLtj|j |jd< |S )aO  
    A converter that allows an attribute to be optional. An optional attribute
    is one which can be set to ``None``.

    Type annotations will be inferred from the wrapped converter's, if it
    has any.

    :param callable converter: the converter that is used for non-``None``
        values.

    .. versionadded:: 17.1.0
    c                    s   | d u rd S ˆ | ƒS ©N© ©Úval©Ú	converterr   ú1/usr/lib/python3/dist-packages/attr/converters.pyÚoptional_converter%   s   z$optional.<locals>.optional_converterNr   r   Úreturn)r   ÚinspectÚ	signatureÚ
ValueErrorÚ	TypeErrorÚlistÚ
parametersÚvaluesÚ
annotationÚ	ParameterÚemptyÚtypingÚOptionalÚ__annotations__Úreturn_annotationÚ	Signature)r   r   ÚsigÚparamsr   r   r   r
      s&   ÿ
ÿ
ÿr
   c                    sx   ˆ t u r|du rtdƒ‚ˆ t ur|durtdƒ‚|dur t|ƒ‰ tˆ tƒr4ˆ jr,tdƒ‚‡ fdd„}|S ‡ fdd„}|S )aš  
    A converter that allows to replace ``None`` values by *default* or the
    result of *factory*.

    :param default: Value to be used if ``None`` is passed. Passing an instance
       of `attr.Factory` is supported, however the ``takes_self`` option
       is *not*.
    :param callable factory: A callable that takes no parameters whose result
       is used if ``None`` is passed.

    :raises TypeError: If **neither** *default* or *factory* is passed.
    :raises TypeError: If **both** *default* and *factory* are passed.
    :raises ValueError: If an instance of `attr.Factory` is passed with
       ``takes_self=True``.

    .. versionadded:: 18.2.0
    Nz(Must pass either `default` or `factory`.z5Must pass either `default` or `factory` but not both.z1`takes_self` is not supported by default_if_none.c                    s   | d ur| S ˆ   ¡ S r   )Úfactoryr   ©Údefaultr   r   Údefault_if_none_convertera   s   z2default_if_none.<locals>.default_if_none_converterc                    s   | d ur| S ˆ S r   r   r   r'   r   r   r)   i   s   )r   r   r   Ú
isinstanceÚ
takes_selfr   )r(   r&   r)   r   r'   r   r   >   s"   ÿ
ÿúr   )Ú__doc__Ú
__future__r   r   r   Ú_compatr   Ú_maker   r   r	   r   r   Ú__all__r
   r   r   r   r   r   Ú<module>   s    '