structures.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. from __future__ import annotations
  2. import collections.abc as cabc
  3. import typing as t
  4. from copy import deepcopy
  5. from .. import exceptions
  6. from .._internal import _missing
  7. from .mixins import ImmutableDictMixin
  8. from .mixins import ImmutableListMixin
  9. from .mixins import ImmutableMultiDictMixin
  10. from .mixins import UpdateDictMixin
  11. if t.TYPE_CHECKING:
  12. import typing_extensions as te
  13. K = t.TypeVar("K")
  14. V = t.TypeVar("V")
  15. T = t.TypeVar("T")
  16. def iter_multi_items(
  17. mapping: (
  18. MultiDict[K, V]
  19. | cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  20. | cabc.Iterable[tuple[K, V]]
  21. ),
  22. ) -> cabc.Iterator[tuple[K, V]]:
  23. """Iterates over the items of a mapping yielding keys and values
  24. without dropping any from more complex structures.
  25. """
  26. if isinstance(mapping, MultiDict):
  27. yield from mapping.items(multi=True)
  28. elif isinstance(mapping, cabc.Mapping):
  29. for key, value in mapping.items():
  30. if isinstance(value, (list, tuple, set)):
  31. for v in value:
  32. yield key, v
  33. else:
  34. yield key, value
  35. else:
  36. yield from mapping
  37. class ImmutableList(ImmutableListMixin, list[V]): # type: ignore[misc]
  38. """An immutable :class:`list`.
  39. .. versionadded:: 0.5
  40. :private:
  41. """
  42. def __repr__(self) -> str:
  43. return f"{type(self).__name__}({list.__repr__(self)})"
  44. class TypeConversionDict(dict[K, V]):
  45. """Works like a regular dict but the :meth:`get` method can perform
  46. type conversions. :class:`MultiDict` and :class:`CombinedMultiDict`
  47. are subclasses of this class and provide the same feature.
  48. .. versionadded:: 0.5
  49. """
  50. @t.overload # type: ignore[override]
  51. def get(self, key: K) -> V | None: ...
  52. @t.overload
  53. def get(self, key: K, default: V) -> V: ...
  54. @t.overload
  55. def get(self, key: K, default: T) -> V | T: ...
  56. @t.overload
  57. def get(self, key: str, type: cabc.Callable[[V], T]) -> T | None: ...
  58. @t.overload
  59. def get(self, key: str, default: T, type: cabc.Callable[[V], T]) -> T: ...
  60. def get( # type: ignore[misc]
  61. self,
  62. key: K,
  63. default: V | T | None = None,
  64. type: cabc.Callable[[V], T] | None = None,
  65. ) -> V | T | None:
  66. """Return the default value if the requested data doesn't exist.
  67. If `type` is provided and is a callable it should convert the value,
  68. return it or raise a :exc:`ValueError` if that is not possible. In
  69. this case the function will return the default as if the value was not
  70. found:
  71. >>> d = TypeConversionDict(foo='42', bar='blub')
  72. >>> d.get('foo', type=int)
  73. 42
  74. >>> d.get('bar', -1, type=int)
  75. -1
  76. :param key: The key to be looked up.
  77. :param default: The default value to be returned if the key can't
  78. be looked up. If not further specified `None` is
  79. returned.
  80. :param type: A callable that is used to cast the value in the
  81. :class:`MultiDict`. If a :exc:`ValueError` or a
  82. :exc:`TypeError` is raised by this callable the default
  83. value is returned.
  84. .. versionchanged:: 3.0.2
  85. Returns the default value on :exc:`TypeError`, too.
  86. """
  87. try:
  88. rv = self[key]
  89. except KeyError:
  90. return default
  91. if type is None:
  92. return rv
  93. try:
  94. return type(rv)
  95. except (ValueError, TypeError):
  96. return default
  97. class ImmutableTypeConversionDict(ImmutableDictMixin[K, V], TypeConversionDict[K, V]): # type: ignore[misc]
  98. """Works like a :class:`TypeConversionDict` but does not support
  99. modifications.
  100. .. versionadded:: 0.5
  101. """
  102. def copy(self) -> TypeConversionDict[K, V]:
  103. """Return a shallow mutable copy of this object. Keep in mind that
  104. the standard library's :func:`copy` function is a no-op for this class
  105. like for any other python immutable type (eg: :class:`tuple`).
  106. """
  107. return TypeConversionDict(self)
  108. def __copy__(self) -> te.Self:
  109. return self
  110. class MultiDict(TypeConversionDict[K, V]):
  111. """A :class:`MultiDict` is a dictionary subclass customized to deal with
  112. multiple values for the same key which is for example used by the parsing
  113. functions in the wrappers. This is necessary because some HTML form
  114. elements pass multiple values for the same key.
  115. :class:`MultiDict` implements all standard dictionary methods.
  116. Internally, it saves all values for a key as a list, but the standard dict
  117. access methods will only return the first value for a key. If you want to
  118. gain access to the other values, too, you have to use the `list` methods as
  119. explained below.
  120. Basic Usage:
  121. >>> d = MultiDict([('a', 'b'), ('a', 'c')])
  122. >>> d
  123. MultiDict([('a', 'b'), ('a', 'c')])
  124. >>> d['a']
  125. 'b'
  126. >>> d.getlist('a')
  127. ['b', 'c']
  128. >>> 'a' in d
  129. True
  130. It behaves like a normal dict thus all dict functions will only return the
  131. first value when multiple values for one key are found.
  132. From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
  133. subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
  134. render a page for a ``400 BAD REQUEST`` if caught in a catch-all for HTTP
  135. exceptions.
  136. A :class:`MultiDict` can be constructed from an iterable of
  137. ``(key, value)`` tuples, a dict, a :class:`MultiDict` or from Werkzeug 0.2
  138. onwards some keyword parameters.
  139. :param mapping: the initial value for the :class:`MultiDict`. Either a
  140. regular dict, an iterable of ``(key, value)`` tuples
  141. or `None`.
  142. .. versionchanged:: 3.1
  143. Implement ``|`` and ``|=`` operators.
  144. """
  145. def __init__(
  146. self,
  147. mapping: (
  148. MultiDict[K, V]
  149. | cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  150. | cabc.Iterable[tuple[K, V]]
  151. | None
  152. ) = None,
  153. ) -> None:
  154. if mapping is None:
  155. super().__init__()
  156. elif isinstance(mapping, MultiDict):
  157. super().__init__((k, vs[:]) for k, vs in mapping.lists())
  158. elif isinstance(mapping, cabc.Mapping):
  159. tmp = {}
  160. for key, value in mapping.items():
  161. if isinstance(value, (list, tuple, set)):
  162. value = list(value)
  163. if not value:
  164. continue
  165. else:
  166. value = [value]
  167. tmp[key] = value
  168. super().__init__(tmp) # type: ignore[arg-type]
  169. else:
  170. tmp = {}
  171. for key, value in mapping:
  172. tmp.setdefault(key, []).append(value)
  173. super().__init__(tmp) # type: ignore[arg-type]
  174. def __getstate__(self) -> t.Any:
  175. return dict(self.lists())
  176. def __setstate__(self, value: t.Any) -> None:
  177. super().clear()
  178. super().update(value)
  179. def __iter__(self) -> cabc.Iterator[K]:
  180. # https://github.com/python/cpython/issues/87412
  181. # If __iter__ is not overridden, Python uses a fast path for dict(md),
  182. # taking the data directly and getting lists of values, rather than
  183. # calling __getitem__ and getting only the first value.
  184. return super().__iter__()
  185. def __getitem__(self, key: K) -> V:
  186. """Return the first data value for this key;
  187. raises KeyError if not found.
  188. :param key: The key to be looked up.
  189. :raise KeyError: if the key does not exist.
  190. """
  191. if key in self:
  192. lst = super().__getitem__(key)
  193. if len(lst) > 0: # type: ignore[arg-type]
  194. return lst[0] # type: ignore[index,no-any-return]
  195. raise exceptions.BadRequestKeyError(key)
  196. def __setitem__(self, key: K, value: V) -> None:
  197. """Like :meth:`add` but removes an existing key first.
  198. :param key: the key for the value.
  199. :param value: the value to set.
  200. """
  201. super().__setitem__(key, [value]) # type: ignore[assignment]
  202. def add(self, key: K, value: V) -> None:
  203. """Adds a new value for the key.
  204. .. versionadded:: 0.6
  205. :param key: the key for the value.
  206. :param value: the value to add.
  207. """
  208. super().setdefault(key, []).append(value) # type: ignore[arg-type,attr-defined]
  209. @t.overload
  210. def getlist(self, key: K) -> list[V]: ...
  211. @t.overload
  212. def getlist(self, key: K, type: cabc.Callable[[V], T]) -> list[T]: ...
  213. def getlist(
  214. self, key: K, type: cabc.Callable[[V], T] | None = None
  215. ) -> list[V] | list[T]:
  216. """Return the list of items for a given key. If that key is not in the
  217. `MultiDict`, the return value will be an empty list. Just like `get`,
  218. `getlist` accepts a `type` parameter. All items will be converted
  219. with the callable defined there.
  220. :param key: The key to be looked up.
  221. :param type: Callable to convert each value. If a ``ValueError`` or
  222. ``TypeError`` is raised, the value is omitted.
  223. :return: a :class:`list` of all the values for the key.
  224. .. versionchanged:: 3.1
  225. Catches ``TypeError`` in addition to ``ValueError``.
  226. """
  227. try:
  228. rv: list[V] = super().__getitem__(key) # type: ignore[assignment]
  229. except KeyError:
  230. return []
  231. if type is None:
  232. return list(rv)
  233. result = []
  234. for item in rv:
  235. try:
  236. result.append(type(item))
  237. except (ValueError, TypeError):
  238. pass
  239. return result
  240. def setlist(self, key: K, new_list: cabc.Iterable[V]) -> None:
  241. """Remove the old values for a key and add new ones. Note that the list
  242. you pass the values in will be shallow-copied before it is inserted in
  243. the dictionary.
  244. >>> d = MultiDict()
  245. >>> d.setlist('foo', ['1', '2'])
  246. >>> d['foo']
  247. '1'
  248. >>> d.getlist('foo')
  249. ['1', '2']
  250. :param key: The key for which the values are set.
  251. :param new_list: An iterable with the new values for the key. Old values
  252. are removed first.
  253. """
  254. super().__setitem__(key, list(new_list)) # type: ignore[assignment]
  255. @t.overload
  256. def setdefault(self, key: K) -> None: ...
  257. @t.overload
  258. def setdefault(self, key: K, default: V) -> V: ...
  259. def setdefault(self, key: K, default: V | None = None) -> V | None:
  260. """Returns the value for the key if it is in the dict, otherwise it
  261. returns `default` and sets that value for `key`.
  262. :param key: The key to be looked up.
  263. :param default: The default value to be returned if the key is not
  264. in the dict. If not further specified it's `None`.
  265. """
  266. if key not in self:
  267. self[key] = default # type: ignore[assignment]
  268. return self[key]
  269. def setlistdefault(
  270. self, key: K, default_list: cabc.Iterable[V] | None = None
  271. ) -> list[V]:
  272. """Like `setdefault` but sets multiple values. The list returned
  273. is not a copy, but the list that is actually used internally. This
  274. means that you can put new values into the dict by appending items
  275. to the list:
  276. >>> d = MultiDict({"foo": 1})
  277. >>> d.setlistdefault("foo").extend([2, 3])
  278. >>> d.getlist("foo")
  279. [1, 2, 3]
  280. :param key: The key to be looked up.
  281. :param default_list: An iterable of default values. It is either copied
  282. (in case it was a list) or converted into a list
  283. before returned.
  284. :return: a :class:`list`
  285. """
  286. if key not in self:
  287. super().__setitem__(key, list(default_list or ())) # type: ignore[assignment]
  288. return super().__getitem__(key) # type: ignore[return-value]
  289. def items(self, multi: bool = False) -> cabc.Iterable[tuple[K, V]]: # type: ignore[override]
  290. """Return an iterator of ``(key, value)`` pairs.
  291. :param multi: If set to `True` the iterator returned will have a pair
  292. for each value of each key. Otherwise it will only
  293. contain pairs for the first value of each key.
  294. """
  295. values: list[V]
  296. for key, values in super().items(): # type: ignore[assignment]
  297. if multi:
  298. for value in values:
  299. yield key, value
  300. else:
  301. yield key, values[0]
  302. def lists(self) -> cabc.Iterable[tuple[K, list[V]]]:
  303. """Return a iterator of ``(key, values)`` pairs, where values is the list
  304. of all values associated with the key."""
  305. values: list[V]
  306. for key, values in super().items(): # type: ignore[assignment]
  307. yield key, list(values)
  308. def values(self) -> cabc.Iterable[V]: # type: ignore[override]
  309. """Returns an iterator of the first value on every key's value list."""
  310. values: list[V]
  311. for values in super().values(): # type: ignore[assignment]
  312. yield values[0]
  313. def listvalues(self) -> cabc.Iterable[list[V]]:
  314. """Return an iterator of all values associated with a key. Zipping
  315. :meth:`keys` and this is the same as calling :meth:`lists`:
  316. >>> d = MultiDict({"foo": [1, 2, 3]})
  317. >>> zip(d.keys(), d.listvalues()) == d.lists()
  318. True
  319. """
  320. return super().values() # type: ignore[return-value]
  321. def copy(self) -> te.Self:
  322. """Return a shallow copy of this object."""
  323. return self.__class__(self)
  324. def deepcopy(self, memo: t.Any = None) -> te.Self:
  325. """Return a deep copy of this object."""
  326. return self.__class__(deepcopy(self.to_dict(flat=False), memo))
  327. @t.overload
  328. def to_dict(self) -> dict[K, V]: ...
  329. @t.overload
  330. def to_dict(self, flat: t.Literal[False]) -> dict[K, list[V]]: ...
  331. def to_dict(self, flat: bool = True) -> dict[K, V] | dict[K, list[V]]:
  332. """Return the contents as regular dict. If `flat` is `True` the
  333. returned dict will only have the first item present, if `flat` is
  334. `False` all values will be returned as lists.
  335. :param flat: If set to `False` the dict returned will have lists
  336. with all the values in it. Otherwise it will only
  337. contain the first value for each key.
  338. :return: a :class:`dict`
  339. """
  340. if flat:
  341. return dict(self.items())
  342. return dict(self.lists())
  343. def update( # type: ignore[override]
  344. self,
  345. mapping: (
  346. MultiDict[K, V]
  347. | cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  348. | cabc.Iterable[tuple[K, V]]
  349. ),
  350. ) -> None:
  351. """update() extends rather than replaces existing key lists:
  352. >>> a = MultiDict({'x': 1})
  353. >>> b = MultiDict({'x': 2, 'y': 3})
  354. >>> a.update(b)
  355. >>> a
  356. MultiDict([('y', 3), ('x', 1), ('x', 2)])
  357. If the value list for a key in ``other_dict`` is empty, no new values
  358. will be added to the dict and the key will not be created:
  359. >>> x = {'empty_list': []}
  360. >>> y = MultiDict()
  361. >>> y.update(x)
  362. >>> y
  363. MultiDict([])
  364. """
  365. for key, value in iter_multi_items(mapping):
  366. self.add(key, value)
  367. def __or__( # type: ignore[override]
  368. self, other: cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  369. ) -> MultiDict[K, V]:
  370. if not isinstance(other, cabc.Mapping):
  371. return NotImplemented
  372. rv = self.copy()
  373. rv.update(other)
  374. return rv
  375. def __ior__( # type: ignore[override]
  376. self,
  377. other: (
  378. cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  379. | cabc.Iterable[tuple[K, V]]
  380. ),
  381. ) -> te.Self:
  382. if not isinstance(other, (cabc.Mapping, cabc.Iterable)):
  383. return NotImplemented
  384. self.update(other)
  385. return self
  386. @t.overload
  387. def pop(self, key: K) -> V: ...
  388. @t.overload
  389. def pop(self, key: K, default: V) -> V: ...
  390. @t.overload
  391. def pop(self, key: K, default: T) -> V | T: ...
  392. def pop(
  393. self,
  394. key: K,
  395. default: V | T = _missing, # type: ignore[assignment]
  396. ) -> V | T:
  397. """Pop the first item for a list on the dict. Afterwards the
  398. key is removed from the dict, so additional values are discarded:
  399. >>> d = MultiDict({"foo": [1, 2, 3]})
  400. >>> d.pop("foo")
  401. 1
  402. >>> "foo" in d
  403. False
  404. :param key: the key to pop.
  405. :param default: if provided the value to return if the key was
  406. not in the dictionary.
  407. """
  408. lst: list[V]
  409. try:
  410. lst = super().pop(key) # type: ignore[assignment]
  411. if len(lst) == 0:
  412. raise exceptions.BadRequestKeyError(key)
  413. return lst[0]
  414. except KeyError:
  415. if default is not _missing:
  416. return default
  417. raise exceptions.BadRequestKeyError(key) from None
  418. def popitem(self) -> tuple[K, V]:
  419. """Pop an item from the dict."""
  420. item: tuple[K, list[V]]
  421. try:
  422. item = super().popitem() # type: ignore[assignment]
  423. if len(item[1]) == 0:
  424. raise exceptions.BadRequestKeyError(item[0])
  425. return item[0], item[1][0]
  426. except KeyError as e:
  427. raise exceptions.BadRequestKeyError(e.args[0]) from None
  428. def poplist(self, key: K) -> list[V]:
  429. """Pop the list for a key from the dict. If the key is not in the dict
  430. an empty list is returned.
  431. .. versionchanged:: 0.5
  432. If the key does no longer exist a list is returned instead of
  433. raising an error.
  434. """
  435. return super().pop(key, []) # type: ignore[return-value]
  436. def popitemlist(self) -> tuple[K, list[V]]:
  437. """Pop a ``(key, list)`` tuple from the dict."""
  438. try:
  439. return super().popitem() # type: ignore[return-value]
  440. except KeyError as e:
  441. raise exceptions.BadRequestKeyError(e.args[0]) from None
  442. def __copy__(self) -> te.Self:
  443. return self.copy()
  444. def __deepcopy__(self, memo: t.Any) -> te.Self:
  445. return self.deepcopy(memo=memo)
  446. def __repr__(self) -> str:
  447. return f"{type(self).__name__}({list(self.items(multi=True))!r})"
  448. class _omd_bucket(t.Generic[K, V]):
  449. """Wraps values in the :class:`OrderedMultiDict`. This makes it
  450. possible to keep an order over multiple different keys. It requires
  451. a lot of extra memory and slows down access a lot, but makes it
  452. possible to access elements in O(1) and iterate in O(n).
  453. """
  454. __slots__ = ("prev", "key", "value", "next")
  455. def __init__(self, omd: _OrderedMultiDict[K, V], key: K, value: V) -> None:
  456. self.prev: _omd_bucket[K, V] | None = omd._last_bucket
  457. self.key: K = key
  458. self.value: V = value
  459. self.next: _omd_bucket[K, V] | None = None
  460. if omd._first_bucket is None:
  461. omd._first_bucket = self
  462. if omd._last_bucket is not None:
  463. omd._last_bucket.next = self
  464. omd._last_bucket = self
  465. def unlink(self, omd: _OrderedMultiDict[K, V]) -> None:
  466. if self.prev:
  467. self.prev.next = self.next
  468. if self.next:
  469. self.next.prev = self.prev
  470. if omd._first_bucket is self:
  471. omd._first_bucket = self.next
  472. if omd._last_bucket is self:
  473. omd._last_bucket = self.prev
  474. class _OrderedMultiDict(MultiDict[K, V]):
  475. """Works like a regular :class:`MultiDict` but preserves the
  476. order of the fields. To convert the ordered multi dict into a
  477. list you can use the :meth:`items` method and pass it ``multi=True``.
  478. In general an :class:`OrderedMultiDict` is an order of magnitude
  479. slower than a :class:`MultiDict`.
  480. .. admonition:: note
  481. Due to a limitation in Python you cannot convert an ordered
  482. multi dict into a regular dict by using ``dict(multidict)``.
  483. Instead you have to use the :meth:`to_dict` method, otherwise
  484. the internal bucket objects are exposed.
  485. .. deprecated:: 3.1
  486. Will be removed in Werkzeug 3.2. Use ``MultiDict`` instead.
  487. """
  488. def __init__(
  489. self,
  490. mapping: (
  491. MultiDict[K, V]
  492. | cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  493. | cabc.Iterable[tuple[K, V]]
  494. | None
  495. ) = None,
  496. ) -> None:
  497. import warnings
  498. warnings.warn(
  499. "'OrderedMultiDict' is deprecated and will be removed in Werkzeug"
  500. " 3.2. Use 'MultiDict' instead.",
  501. DeprecationWarning,
  502. stacklevel=2,
  503. )
  504. super().__init__()
  505. self._first_bucket: _omd_bucket[K, V] | None = None
  506. self._last_bucket: _omd_bucket[K, V] | None = None
  507. if mapping is not None:
  508. self.update(mapping)
  509. def __eq__(self, other: object) -> bool:
  510. if not isinstance(other, MultiDict):
  511. return NotImplemented
  512. if isinstance(other, _OrderedMultiDict):
  513. iter1 = iter(self.items(multi=True))
  514. iter2 = iter(other.items(multi=True))
  515. try:
  516. for k1, v1 in iter1:
  517. k2, v2 = next(iter2)
  518. if k1 != k2 or v1 != v2:
  519. return False
  520. except StopIteration:
  521. return False
  522. try:
  523. next(iter2)
  524. except StopIteration:
  525. return True
  526. return False
  527. if len(self) != len(other):
  528. return False
  529. for key, values in self.lists():
  530. if other.getlist(key) != values:
  531. return False
  532. return True
  533. __hash__ = None # type: ignore[assignment]
  534. def __reduce_ex__(self, protocol: t.SupportsIndex) -> t.Any:
  535. return type(self), (list(self.items(multi=True)),)
  536. def __getstate__(self) -> t.Any:
  537. return list(self.items(multi=True))
  538. def __setstate__(self, values: t.Any) -> None:
  539. self.clear()
  540. for key, value in values:
  541. self.add(key, value)
  542. def __getitem__(self, key: K) -> V:
  543. if key in self:
  544. return dict.__getitem__(self, key)[0].value # type: ignore[index,no-any-return]
  545. raise exceptions.BadRequestKeyError(key)
  546. def __setitem__(self, key: K, value: V) -> None:
  547. self.poplist(key)
  548. self.add(key, value)
  549. def __delitem__(self, key: K) -> None:
  550. self.pop(key)
  551. def keys(self) -> cabc.Iterable[K]: # type: ignore[override]
  552. return (key for key, _ in self.items())
  553. def __iter__(self) -> cabc.Iterator[K]:
  554. return iter(self.keys())
  555. def values(self) -> cabc.Iterable[V]: # type: ignore[override]
  556. return (value for key, value in self.items())
  557. def items(self, multi: bool = False) -> cabc.Iterable[tuple[K, V]]: # type: ignore[override]
  558. ptr = self._first_bucket
  559. if multi:
  560. while ptr is not None:
  561. yield ptr.key, ptr.value
  562. ptr = ptr.next
  563. else:
  564. returned_keys = set()
  565. while ptr is not None:
  566. if ptr.key not in returned_keys:
  567. returned_keys.add(ptr.key)
  568. yield ptr.key, ptr.value
  569. ptr = ptr.next
  570. def lists(self) -> cabc.Iterable[tuple[K, list[V]]]:
  571. returned_keys = set()
  572. ptr = self._first_bucket
  573. while ptr is not None:
  574. if ptr.key not in returned_keys:
  575. yield ptr.key, self.getlist(ptr.key)
  576. returned_keys.add(ptr.key)
  577. ptr = ptr.next
  578. def listvalues(self) -> cabc.Iterable[list[V]]:
  579. for _key, values in self.lists():
  580. yield values
  581. def add(self, key: K, value: V) -> None:
  582. dict.setdefault(self, key, []).append(_omd_bucket(self, key, value)) # type: ignore[arg-type,attr-defined]
  583. @t.overload
  584. def getlist(self, key: K) -> list[V]: ...
  585. @t.overload
  586. def getlist(self, key: K, type: cabc.Callable[[V], T]) -> list[T]: ...
  587. def getlist(
  588. self, key: K, type: cabc.Callable[[V], T] | None = None
  589. ) -> list[V] | list[T]:
  590. rv: list[_omd_bucket[K, V]]
  591. try:
  592. rv = dict.__getitem__(self, key) # type: ignore[index]
  593. except KeyError:
  594. return []
  595. if type is None:
  596. return [x.value for x in rv]
  597. result = []
  598. for item in rv:
  599. try:
  600. result.append(type(item.value))
  601. except (ValueError, TypeError):
  602. pass
  603. return result
  604. def setlist(self, key: K, new_list: cabc.Iterable[V]) -> None:
  605. self.poplist(key)
  606. for value in new_list:
  607. self.add(key, value)
  608. def setlistdefault(self, key: t.Any, default_list: t.Any = None) -> t.NoReturn:
  609. raise TypeError("setlistdefault is unsupported for ordered multi dicts")
  610. def update( # type: ignore[override]
  611. self,
  612. mapping: (
  613. MultiDict[K, V]
  614. | cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  615. | cabc.Iterable[tuple[K, V]]
  616. ),
  617. ) -> None:
  618. for key, value in iter_multi_items(mapping):
  619. self.add(key, value)
  620. def poplist(self, key: K) -> list[V]:
  621. buckets: cabc.Iterable[_omd_bucket[K, V]] = dict.pop(self, key, ()) # type: ignore[arg-type]
  622. for bucket in buckets:
  623. bucket.unlink(self)
  624. return [x.value for x in buckets]
  625. @t.overload
  626. def pop(self, key: K) -> V: ...
  627. @t.overload
  628. def pop(self, key: K, default: V) -> V: ...
  629. @t.overload
  630. def pop(self, key: K, default: T) -> V | T: ...
  631. def pop(
  632. self,
  633. key: K,
  634. default: V | T = _missing, # type: ignore[assignment]
  635. ) -> V | T:
  636. buckets: list[_omd_bucket[K, V]]
  637. try:
  638. buckets = dict.pop(self, key) # type: ignore[arg-type]
  639. except KeyError:
  640. if default is not _missing:
  641. return default
  642. raise exceptions.BadRequestKeyError(key) from None
  643. for bucket in buckets:
  644. bucket.unlink(self)
  645. return buckets[0].value
  646. def popitem(self) -> tuple[K, V]:
  647. key: K
  648. buckets: list[_omd_bucket[K, V]]
  649. try:
  650. key, buckets = dict.popitem(self) # type: ignore[arg-type,assignment]
  651. except KeyError as e:
  652. raise exceptions.BadRequestKeyError(e.args[0]) from None
  653. for bucket in buckets:
  654. bucket.unlink(self)
  655. return key, buckets[0].value
  656. def popitemlist(self) -> tuple[K, list[V]]:
  657. key: K
  658. buckets: list[_omd_bucket[K, V]]
  659. try:
  660. key, buckets = dict.popitem(self) # type: ignore[arg-type,assignment]
  661. except KeyError as e:
  662. raise exceptions.BadRequestKeyError(e.args[0]) from None
  663. for bucket in buckets:
  664. bucket.unlink(self)
  665. return key, [x.value for x in buckets]
  666. class CombinedMultiDict(ImmutableMultiDictMixin[K, V], MultiDict[K, V]): # type: ignore[misc]
  667. """A read only :class:`MultiDict` that you can pass multiple :class:`MultiDict`
  668. instances as sequence and it will combine the return values of all wrapped
  669. dicts:
  670. >>> from werkzeug.datastructures import CombinedMultiDict, MultiDict
  671. >>> post = MultiDict([('foo', 'bar')])
  672. >>> get = MultiDict([('blub', 'blah')])
  673. >>> combined = CombinedMultiDict([get, post])
  674. >>> combined['foo']
  675. 'bar'
  676. >>> combined['blub']
  677. 'blah'
  678. This works for all read operations and will raise a `TypeError` for
  679. methods that usually change data which isn't possible.
  680. From Werkzeug 0.3 onwards, the `KeyError` raised by this class is also a
  681. subclass of the :exc:`~exceptions.BadRequest` HTTP exception and will
  682. render a page for a ``400 BAD REQUEST`` if caught in a catch-all for HTTP
  683. exceptions.
  684. """
  685. def __reduce_ex__(self, protocol: t.SupportsIndex) -> t.Any:
  686. return type(self), (self.dicts,)
  687. def __init__(self, dicts: cabc.Iterable[MultiDict[K, V]] | None = None) -> None:
  688. super().__init__()
  689. self.dicts: list[MultiDict[K, V]] = list(dicts or ())
  690. @classmethod
  691. def fromkeys(cls, keys: t.Any, value: t.Any = None) -> t.NoReturn:
  692. raise TypeError(f"cannot create {cls.__name__!r} instances by fromkeys")
  693. def __getitem__(self, key: K) -> V:
  694. for d in self.dicts:
  695. if key in d:
  696. return d[key]
  697. raise exceptions.BadRequestKeyError(key)
  698. @t.overload # type: ignore[override]
  699. def get(self, key: K) -> V | None: ...
  700. @t.overload
  701. def get(self, key: K, default: V) -> V: ...
  702. @t.overload
  703. def get(self, key: K, default: T) -> V | T: ...
  704. @t.overload
  705. def get(self, key: str, type: cabc.Callable[[V], T]) -> T | None: ...
  706. @t.overload
  707. def get(self, key: str, default: T, type: cabc.Callable[[V], T]) -> T: ...
  708. def get( # type: ignore[misc]
  709. self,
  710. key: K,
  711. default: V | T | None = None,
  712. type: cabc.Callable[[V], T] | None = None,
  713. ) -> V | T | None:
  714. for d in self.dicts:
  715. if key in d:
  716. if type is not None:
  717. try:
  718. return type(d[key])
  719. except (ValueError, TypeError):
  720. continue
  721. return d[key]
  722. return default
  723. @t.overload
  724. def getlist(self, key: K) -> list[V]: ...
  725. @t.overload
  726. def getlist(self, key: K, type: cabc.Callable[[V], T]) -> list[T]: ...
  727. def getlist(
  728. self, key: K, type: cabc.Callable[[V], T] | None = None
  729. ) -> list[V] | list[T]:
  730. rv = []
  731. for d in self.dicts:
  732. rv.extend(d.getlist(key, type)) # type: ignore[arg-type]
  733. return rv
  734. def _keys_impl(self) -> set[K]:
  735. """This function exists so __len__ can be implemented more efficiently,
  736. saving one list creation from an iterator.
  737. """
  738. return set(k for d in self.dicts for k in d)
  739. def keys(self) -> cabc.Iterable[K]: # type: ignore[override]
  740. return self._keys_impl()
  741. def __iter__(self) -> cabc.Iterator[K]:
  742. return iter(self._keys_impl())
  743. @t.overload # type: ignore[override]
  744. def items(self) -> cabc.Iterable[tuple[K, V]]: ...
  745. @t.overload
  746. def items(self, multi: t.Literal[True]) -> cabc.Iterable[tuple[K, list[V]]]: ...
  747. def items(
  748. self, multi: bool = False
  749. ) -> cabc.Iterable[tuple[K, V]] | cabc.Iterable[tuple[K, list[V]]]:
  750. found = set()
  751. for d in self.dicts:
  752. for key, value in d.items(multi):
  753. if multi:
  754. yield key, value
  755. elif key not in found:
  756. found.add(key)
  757. yield key, value
  758. def values(self) -> cabc.Iterable[V]: # type: ignore[override]
  759. for _, value in self.items():
  760. yield value
  761. def lists(self) -> cabc.Iterable[tuple[K, list[V]]]:
  762. rv: dict[K, list[V]] = {}
  763. for d in self.dicts:
  764. for key, values in d.lists():
  765. rv.setdefault(key, []).extend(values)
  766. return rv.items()
  767. def listvalues(self) -> cabc.Iterable[list[V]]:
  768. return (x[1] for x in self.lists())
  769. def copy(self) -> MultiDict[K, V]: # type: ignore[override]
  770. """Return a shallow mutable copy of this object.
  771. This returns a :class:`MultiDict` representing the data at the
  772. time of copying. The copy will no longer reflect changes to the
  773. wrapped dicts.
  774. .. versionchanged:: 0.15
  775. Return a mutable :class:`MultiDict`.
  776. """
  777. return MultiDict(self)
  778. def __len__(self) -> int:
  779. return len(self._keys_impl())
  780. def __contains__(self, key: K) -> bool: # type: ignore[override]
  781. for d in self.dicts:
  782. if key in d:
  783. return True
  784. return False
  785. def __repr__(self) -> str:
  786. return f"{type(self).__name__}({self.dicts!r})"
  787. class ImmutableDict(ImmutableDictMixin[K, V], dict[K, V]): # type: ignore[misc]
  788. """An immutable :class:`dict`.
  789. .. versionadded:: 0.5
  790. """
  791. def __repr__(self) -> str:
  792. return f"{type(self).__name__}({dict.__repr__(self)})"
  793. def copy(self) -> dict[K, V]:
  794. """Return a shallow mutable copy of this object. Keep in mind that
  795. the standard library's :func:`copy` function is a no-op for this class
  796. like for any other python immutable type (eg: :class:`tuple`).
  797. """
  798. return dict(self)
  799. def __copy__(self) -> te.Self:
  800. return self
  801. class ImmutableMultiDict(ImmutableMultiDictMixin[K, V], MultiDict[K, V]): # type: ignore[misc]
  802. """An immutable :class:`MultiDict`.
  803. .. versionadded:: 0.5
  804. """
  805. def copy(self) -> MultiDict[K, V]: # type: ignore[override]
  806. """Return a shallow mutable copy of this object. Keep in mind that
  807. the standard library's :func:`copy` function is a no-op for this class
  808. like for any other python immutable type (eg: :class:`tuple`).
  809. """
  810. return MultiDict(self)
  811. def __copy__(self) -> te.Self:
  812. return self
  813. class _ImmutableOrderedMultiDict( # type: ignore[misc]
  814. ImmutableMultiDictMixin[K, V], _OrderedMultiDict[K, V]
  815. ):
  816. """An immutable :class:`OrderedMultiDict`.
  817. .. deprecated:: 3.1
  818. Will be removed in Werkzeug 3.2. Use ``ImmutableMultiDict`` instead.
  819. .. versionadded:: 0.6
  820. """
  821. def __init__(
  822. self,
  823. mapping: (
  824. MultiDict[K, V]
  825. | cabc.Mapping[K, V | list[V] | tuple[V, ...] | set[V]]
  826. | cabc.Iterable[tuple[K, V]]
  827. | None
  828. ) = None,
  829. ) -> None:
  830. super().__init__()
  831. if mapping is not None:
  832. for k, v in iter_multi_items(mapping):
  833. _OrderedMultiDict.add(self, k, v)
  834. def _iter_hashitems(self) -> cabc.Iterable[t.Any]:
  835. return enumerate(self.items(multi=True))
  836. def copy(self) -> _OrderedMultiDict[K, V]: # type: ignore[override]
  837. """Return a shallow mutable copy of this object. Keep in mind that
  838. the standard library's :func:`copy` function is a no-op for this class
  839. like for any other python immutable type (eg: :class:`tuple`).
  840. """
  841. return _OrderedMultiDict(self)
  842. def __copy__(self) -> te.Self:
  843. return self
  844. class CallbackDict(UpdateDictMixin[K, V], dict[K, V]):
  845. """A dict that calls a function passed every time something is changed.
  846. The function is passed the dict instance.
  847. """
  848. def __init__(
  849. self,
  850. initial: cabc.Mapping[K, V] | cabc.Iterable[tuple[K, V]] | None = None,
  851. on_update: cabc.Callable[[te.Self], None] | None = None,
  852. ) -> None:
  853. if initial is None:
  854. super().__init__()
  855. else:
  856. super().__init__(initial)
  857. self.on_update = on_update
  858. def __repr__(self) -> str:
  859. return f"<{type(self).__name__} {super().__repr__()}>"
  860. class HeaderSet(cabc.MutableSet[str]):
  861. """Similar to the :class:`ETags` class this implements a set-like structure.
  862. Unlike :class:`ETags` this is case insensitive and used for vary, allow, and
  863. content-language headers.
  864. If not constructed using the :func:`parse_set_header` function the
  865. instantiation works like this:
  866. >>> hs = HeaderSet(['foo', 'bar', 'baz'])
  867. >>> hs
  868. HeaderSet(['foo', 'bar', 'baz'])
  869. """
  870. def __init__(
  871. self,
  872. headers: cabc.Iterable[str] | None = None,
  873. on_update: cabc.Callable[[te.Self], None] | None = None,
  874. ) -> None:
  875. self._headers = list(headers or ())
  876. self._set = {x.lower() for x in self._headers}
  877. self.on_update = on_update
  878. def add(self, header: str) -> None:
  879. """Add a new header to the set."""
  880. self.update((header,))
  881. def remove(self: te.Self, header: str) -> None:
  882. """Remove a header from the set. This raises an :exc:`KeyError` if the
  883. header is not in the set.
  884. .. versionchanged:: 0.5
  885. In older versions a :exc:`IndexError` was raised instead of a
  886. :exc:`KeyError` if the object was missing.
  887. :param header: the header to be removed.
  888. """
  889. key = header.lower()
  890. if key not in self._set:
  891. raise KeyError(header)
  892. self._set.remove(key)
  893. for idx, key in enumerate(self._headers):
  894. if key.lower() == header:
  895. del self._headers[idx]
  896. break
  897. if self.on_update is not None:
  898. self.on_update(self)
  899. def update(self: te.Self, iterable: cabc.Iterable[str]) -> None:
  900. """Add all the headers from the iterable to the set.
  901. :param iterable: updates the set with the items from the iterable.
  902. """
  903. inserted_any = False
  904. for header in iterable:
  905. key = header.lower()
  906. if key not in self._set:
  907. self._headers.append(header)
  908. self._set.add(key)
  909. inserted_any = True
  910. if inserted_any and self.on_update is not None:
  911. self.on_update(self)
  912. def discard(self, header: str) -> None:
  913. """Like :meth:`remove` but ignores errors.
  914. :param header: the header to be discarded.
  915. """
  916. try:
  917. self.remove(header)
  918. except KeyError:
  919. pass
  920. def find(self, header: str) -> int:
  921. """Return the index of the header in the set or return -1 if not found.
  922. :param header: the header to be looked up.
  923. """
  924. header = header.lower()
  925. for idx, item in enumerate(self._headers):
  926. if item.lower() == header:
  927. return idx
  928. return -1
  929. def index(self, header: str) -> int:
  930. """Return the index of the header in the set or raise an
  931. :exc:`IndexError`.
  932. :param header: the header to be looked up.
  933. """
  934. rv = self.find(header)
  935. if rv < 0:
  936. raise IndexError(header)
  937. return rv
  938. def clear(self: te.Self) -> None:
  939. """Clear the set."""
  940. self._set.clear()
  941. self._headers.clear()
  942. if self.on_update is not None:
  943. self.on_update(self)
  944. def as_set(self, preserve_casing: bool = False) -> set[str]:
  945. """Return the set as real python set type. When calling this, all
  946. the items are converted to lowercase and the ordering is lost.
  947. :param preserve_casing: if set to `True` the items in the set returned
  948. will have the original case like in the
  949. :class:`HeaderSet`, otherwise they will
  950. be lowercase.
  951. """
  952. if preserve_casing:
  953. return set(self._headers)
  954. return set(self._set)
  955. def to_header(self) -> str:
  956. """Convert the header set into an HTTP header string."""
  957. return ", ".join(map(http.quote_header_value, self._headers))
  958. def __getitem__(self, idx: t.SupportsIndex) -> str:
  959. return self._headers[idx]
  960. def __delitem__(self: te.Self, idx: t.SupportsIndex) -> None:
  961. rv = self._headers.pop(idx)
  962. self._set.remove(rv.lower())
  963. if self.on_update is not None:
  964. self.on_update(self)
  965. def __setitem__(self: te.Self, idx: t.SupportsIndex, value: str) -> None:
  966. old = self._headers[idx]
  967. self._set.remove(old.lower())
  968. self._headers[idx] = value
  969. self._set.add(value.lower())
  970. if self.on_update is not None:
  971. self.on_update(self)
  972. def __contains__(self, header: str) -> bool: # type: ignore[override]
  973. return header.lower() in self._set
  974. def __len__(self) -> int:
  975. return len(self._set)
  976. def __iter__(self) -> cabc.Iterator[str]:
  977. return iter(self._headers)
  978. def __bool__(self) -> bool:
  979. return bool(self._set)
  980. def __str__(self) -> str:
  981. return self.to_header()
  982. def __repr__(self) -> str:
  983. return f"{type(self).__name__}({self._headers!r})"
  984. # circular dependencies
  985. from .. import http
  986. def __getattr__(name: str) -> t.Any:
  987. import warnings
  988. if name == "OrderedMultiDict":
  989. warnings.warn(
  990. "'OrderedMultiDict' is deprecated and will be removed in Werkzeug"
  991. " 3.2. Use 'MultiDict' instead.",
  992. DeprecationWarning,
  993. stacklevel=2,
  994. )
  995. return _OrderedMultiDict
  996. if name == "ImmutableOrderedMultiDict":
  997. warnings.warn(
  998. "'ImmutableOrderedMultiDict' is deprecated and will be removed in"
  999. " Werkzeug 3.2. Use 'ImmutableMultiDict' instead.",
  1000. DeprecationWarning,
  1001. stacklevel=2,
  1002. )
  1003. return _ImmutableOrderedMultiDict
  1004. raise AttributeError(name)