# Stubs for weakref

# NOTE: These are incomplete!

from typing import (
    TypeVar, Generic, Any, Callable, overload, Mapping, Iterator, Dict, Tuple,
    Iterable, Optional
)

_T = TypeVar('_T')
_KT = TypeVar('_KT')
_VT = TypeVar('_VT')

class ReferenceType(Generic[_T]):
    # TODO rest of members
    def __init__(self, o: _T, callback: Callable[[ReferenceType[_T]],
                                           Any] = ...) -> None: ...
    def __call__(self) -> Optional[_T]: ...

ref = ReferenceType

# TODO callback
def proxy(object: _T) -> _T: ...

class WeakValueDictionary(Generic[_KT, _VT]):
    # TODO tuple iterable argument?
    @overload
    def __init__(self) -> None: ...
    @overload
    def __init__(self, map: Mapping[_KT, _VT]) -> None: ...

    def __len__(self) -> int: ...
    def __getitem__(self, k: _KT) -> _VT: ...
    def __setitem__(self, k: _KT, v: _VT) -> None: ...
    def __delitem__(self, v: _KT) -> None: ...
    def __contains__(self, o: object) -> bool: ...
    def __iter__(self) -> Iterator[_KT]: ...
    def __str__(self) -> str: ...

    def clear(self) -> None: ...
    def copy(self) -> Dict[_KT, _VT]: ...

    @overload
    def get(self, k: _KT) -> _VT: ...
    @overload
    def get(self, k: _KT, default: _VT) -> _VT: ...

    @overload
    def pop(self, k: _KT) -> _VT: ...
    @overload
    def pop(self, k: _KT, default: _VT) -> _VT: ...

    def popitem(self) -> Tuple[_KT, _VT]: ...

    @overload
    def setdefault(self, k: _KT) -> _VT: ...
    @overload
    def setdefault(self, k: _KT, default: _VT) -> _VT: ...

    @overload
    def update(self, m: Mapping[_KT, _VT]) -> None: ...
    @overload
    def update(self, m: Iterable[Tuple[_KT, _VT]]) -> None: ...

    # NOTE: incompatible with Mapping
    def keys(self) -> Iterator[_KT]: ...
    def values(self) -> Iterator[_VT]: ...
    def items(self) -> Iterator[Tuple[_KT, _VT]]: ...

    # TODO return type
    def valuerefs(self) -> Iterable[Any]: ...


class WeakKeyDictionary(Generic[_KT, _VT]):
    # TODO tuple iterable argument?
    @overload
    def __init__(self) -> None: ...
    @overload
    def __init__(self, map: Mapping[_KT, _VT]) -> None: ...

    def __len__(self) -> int: ...
    def __getitem__(self, k: _KT) -> _VT: ...
    def __setitem__(self, k: _KT, v: _VT) -> None: ...
    def __delitem__(self, v: _KT) -> None: ...
    def __contains__(self, o: object) -> bool: ...
    def __iter__(self) -> Iterator[_KT]: ...
    def __str__(self) -> str: ...

    def clear(self) -> None: ...
    def copy(self) -> Dict[_KT, _VT]: ...

    @overload
    def get(self, k: _KT) -> _VT: ...
    @overload
    def get(self, k: _KT, default: _VT) -> _VT: ...

    @overload
    def pop(self, k: _KT) -> _VT: ...
    @overload
    def pop(self, k: _KT, default: _VT) -> _VT: ...

    def popitem(self) -> Tuple[_KT, _VT]: ...

    @overload
    def setdefault(self, k: _KT) -> _VT: ...
    @overload
    def setdefault(self, k: _KT, default: _VT) -> _VT: ...

    @overload
    def update(self, m: Mapping[_KT, _VT]) -> None: ...
    @overload
    def update(self, m: Iterable[Tuple[_KT, _VT]]) -> None: ...

    # NOTE: incompatible with Mapping
    def keys(self) -> Iterator[_KT]: ...
    def values(self) -> Iterator[_VT]: ...
    def items(self) -> Iterator[Tuple[_KT, _VT]]: ...

    # TODO return type
    def valuerefs(self) -> Iterable[Any]: ...
