Skip to content

Commit

Permalink
Widen HeadersLike type.
Browse files Browse the repository at this point in the history
Restore compatibility with the latest mypy.

Refs python/typeshed#6653.
  • Loading branch information
aaugustin committed Mar 23, 2022
1 parent db600e5 commit 747df3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/reference/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ Types
.. autodata:: websockets.connection.Event

.. autodata:: websockets.datastructures.HeadersLike

.. autodata:: websockets.datastructures.SupportsKeysAndGetItem
32 changes: 29 additions & 3 deletions src/websockets/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
List,
Mapping,
MutableMapping,
Protocol,
Tuple,
Union,
)
Expand Down Expand Up @@ -134,7 +135,7 @@ def clear(self) -> None:
self._dict = {}
self._list = []

def update(self, *args: HeadersLike, **kwargs: str) -> None: # type: ignore
def update(self, *args: HeadersLike, **kwargs: str) -> None:
"""
Update from a :class:`Headers` instance and/or keyword arguments.
Expand Down Expand Up @@ -164,5 +165,30 @@ def raw_items(self) -> Iterator[Tuple[str, str]]:
return iter(self._list)


HeadersLike = Union[Headers, Mapping[str, str], Iterable[Tuple[str, str]]]
"""Types accepted where :class:`Headers` is expected."""
# copy of _typeshed.SupportsKeysAndGetItem.
class SupportsKeysAndGetItem(Protocol): # pragma: no cover
"""
Dict-like types with ``keys() -> str`` and ``__getitem__(key: str) -> str`` methods.
"""

def keys(self) -> Iterable[str]:
...

def __getitem__(self, key: str) -> str:
...


HeadersLike = Union[
Headers,
Mapping[str, str],
Iterable[Tuple[str, str]],
SupportsKeysAndGetItem,
]
"""
Types accepted where :class:`Headers` is expected.
In addition to :class:`Headers` itself, this includes dict-like types where both
keys and values are :class:`str`.
"""

0 comments on commit 747df3d

Please sign in to comment.