diff options
author | David Robertson <davidr@element.io> | 2022-10-28 17:04:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-28 16:04:02 +0000 |
commit | 730b13dbc9e48181b1aaf38be870ec21364b1e9c (patch) | |
tree | f4ddcbec8e5e68a2780318985eafd4891b737fae /synapse/http | |
parent | Switch search SQL to triple-quote strings. (#14311) (diff) | |
download | synapse-730b13dbc9e48181b1aaf38be870ec21364b1e9c.tar.xz |
Improve `RawHeaders` type hints (#14303)
Diffstat (limited to 'synapse/http')
-rw-r--r-- | synapse/http/client.py | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/synapse/http/client.py b/synapse/http/client.py index 084d0a5b84..4eb740c040 100644 --- a/synapse/http/client.py +++ b/synapse/http/client.py @@ -25,7 +25,6 @@ from typing import ( List, Mapping, Optional, - Sequence, Tuple, Union, ) @@ -90,14 +89,29 @@ incoming_responses_counter = Counter( "synapse_http_client_responses", "", ["method", "code"] ) -# the type of the headers list, to be passed to the t.w.h.Headers. -# Actually we can mix str and bytes keys, but Mapping treats 'key' as invariant so -# we simplify. +# the type of the headers map, to be passed to the t.w.h.Headers. +# +# The actual type accepted by Twisted is +# Mapping[Union[str, bytes], Sequence[Union[str, bytes]] , +# allowing us to mix and match str and bytes freely. However: any str is also a +# Sequence[str]; passing a header string value which is a +# standalone str is interpreted as a sequence of 1-codepoint strings. This is a disastrous footgun. +# We use a narrower value type (RawHeaderValue) to avoid this footgun. +# +# We also simplify the keys to be either all str or all bytes. This helps because +# Dict[K, V] is invariant in K (and indeed V). RawHeaders = Union[Mapping[str, "RawHeaderValue"], Mapping[bytes, "RawHeaderValue"]] # the value actually has to be a List, but List is invariant so we can't specify that # the entries can either be Lists or bytes. -RawHeaderValue = Sequence[Union[str, bytes]] +RawHeaderValue = Union[ + List[str], + List[bytes], + List[Union[str, bytes]], + Tuple[str, ...], + Tuple[bytes, ...], + Tuple[Union[str, bytes], ...], +] def check_against_blacklist( |