B
    ê¹`1  ã               @   s†   d dl mZ d dl mZ d dl mZ d dl mZ d dl mZ d dl mZ ddgZed	ƒZed
ƒZ	G dd„ dee ƒZ
G dd„ dƒZdS )é    )ÚAny)Úcast)ÚDict)ÚGeneric)ÚTypeVar)ÚUnionÚStoreÚStoreKeyÚTÚDc               @   s   e Zd ZdZdZdS )r	   z»StoreKey is an object used as a key to a Store.

    A StoreKey is associated with the type T of the value of the key.

    A StoreKey is unique and cannot conflict with another key.
    © N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   úU/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/_pytest/store.pyr	      s   c               @   s¨   e Zd ZdZdZddœdd„Zee eddœdd	„Zee ed
œdd„Z	ee e
eee
f dœdd„Zee eedœdd„Zee dd
œdd„Zee ed
œdd„ZdS )r   a  Store is a type-safe heterogenous mutable mapping that
    allows keys and value types to be defined separately from
    where it (the Store) is created.

    Usually you will be given an object which has a ``Store``:

    .. code-block:: python

        store: Store = some_object.store

    If a module wants to store data in this Store, it creates StoreKeys
    for its keys (at the module level):

    .. code-block:: python

        some_str_key = StoreKey[str]()
        some_bool_key = StoreKey[bool]()

    To store information:

    .. code-block:: python

        # Value type must match the key.
        store[some_str_key] = "value"
        store[some_bool_key] = True

    To retrieve the information:

    .. code-block:: python

        # The static type of some_str is str.
        some_str = store[some_str_key]
        # The static type of some_bool is bool.
        some_bool = store[some_bool_key]

    Why use this?
    -------------

    Problem: module Internal defines an object. Module External, which
    module Internal doesn't know about, receives the object and wants to
    attach information to it, to be retrieved later given the object.

    Bad solution 1: Module External assigns private attributes directly on
    the object. This doesn't work well because the type checker doesn't
    know about these attributes and it complains about undefined attributes.

    Bad solution 2: module Internal adds a ``Dict[str, Any]`` attribute to
    the object. Module External stores its data in private keys of this dict.
    This doesn't work well because retrieved values are untyped.

    Good solution: module Internal adds a ``Store`` to the object. Module
    External mints StoreKeys for its own keys. Module External stores and
    retrieves its data using these keys.
    )Ú_storeN)Úreturnc             C   s
   i | _ d S )N)r   )Úselfr   r   r   Ú__init__U   s    zStore.__init__)ÚkeyÚvaluer   c             C   s   || j |< dS )zSet a value for key.N)r   )r   r   r   r   r   r   Ú__setitem__X   s    zStore.__setitem__)r   r   c             C   s   t t| j| ƒS )zZGet the value for key.

        Raises ``KeyError`` if the key wasn't set before.
        )r   r
   r   )r   r   r   r   r   Ú__getitem__\   s    zStore.__getitem__)r   Údefaultr   c             C   s"   y| | S  t k
r   |S X dS )zNGet the value for key, or return default if the key wasn't set
        before.N)ÚKeyError)r   r   r   r   r   r   Úgetc   s    z	Store.getc             C   s*   y| | S  t k
r$   || |< |S X dS )zmReturn the value of key if already set, otherwise set the value
        of key to default and return default.N)r   )r   r   r   r   r   r   Ú
setdefaultk   s
    zStore.setdefaultc             C   s   | j |= dS )z]Delete the value for key.

        Raises ``KeyError`` if the key wasn't set before.
        N)r   )r   r   r   r   r   Ú__delitem__t   s    zStore.__delitem__c             C   s
   || j kS )zReturn whether key was set.)r   )r   r   r   r   r   Ú__contains__{   s    zStore.__contains__)r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Úboolr    r   r   r   r   r      s   6	N)Útypingr   r   r   r   r   r   Ú__all__r
   r   r	   r   r   r   r   r   Ú<module>   s   