Dictionary-like collection where keys can occur multiple times, optimized for HTTP headers and URL query strings
npx @tessl/cli install tessl/pypi-multidict@6.6.0A high-performance dictionary-like collection where keys can occur multiple times, specifically designed for HTTP headers, URL query strings, and other web development scenarios. Multidict preserves insertion order and provides both mutable and immutable variants with case-sensitive and case-insensitive key handling.
pip install multidictfrom multidict import MultiDict, CIMultiDict, MultiDictProxy, CIMultiDictProxyImport specific types and utilities:
from multidict import MultiMapping, MutableMultiMapping, istr, upstr, getversionAccess version information:
from multidict import __version__
print(__version__) # '6.6.4'from multidict import MultiDict, CIMultiDict
# Create a mutable multidict for HTTP headers
headers = MultiDict([
('Accept', 'text/html'),
('Accept', 'application/json'),
('User-Agent', 'MyApp/1.0')
])
# Access first value (dict-like behavior)
print(headers['Accept']) # 'text/html'
# Get all values for a key
print(headers.getall('Accept')) # ['text/html', 'application/json']
# Add more values
headers.add('Accept', 'text/plain')
print(headers.getall('Accept')) # ['text/html', 'application/json', 'text/plain']
# Case-insensitive multidict for HTTP headers
ci_headers = CIMultiDict([('content-type', 'text/html')])
print(ci_headers['Content-Type']) # 'text/html' (case-insensitive access)
# Create immutable proxy
proxy = headers.copy() # Returns MultiDict
readonly_proxy = MultiDictProxy(headers) # Immutable viewMultidict provides a four-class hierarchy optimized for web development:
MultiDict (case-sensitive) and CIMultiDict (case-insensitive) for modifiable collectionsMultiDictProxy and CIMultiDictProxy for read-only viewsMultiMapping and MutableMultiMapping define the protocolistr class provides case-insensitive string handlingThe design maintains standard dict-like interfaces while extending functionality for multi-valued keys, making it ideal for HTTP headers, form data, and URL parameters where duplicate keys are meaningful.
Core functionality for creating and modifying multidict collections with support for multiple values per key, including specialized methods for web development scenarios.
class MultiDict(MutableMultiMapping[_V]):
def __init__(self, arg: MDArg[_V] = None, /, **kwargs: _V): ...
def add(self, key: str, value: _V) -> None: ...
def getall(self, key: str) -> List[_V]: ...
def getall(self, key: str, default: _T) -> Union[List[_V], _T]: ...
def getone(self, key: str) -> _V: ...
def getone(self, key: str, default: _T) -> Union[_V, _T]: ...
def extend(self, arg: MDArg[_V] = None, /, **kwargs: _V) -> None: ...
def merge(self, arg: MDArg[_V] = None, /, **kwargs: _V) -> None: ...
def popall(self, key: str) -> List[_V]: ...
def popall(self, key: str, default: _T) -> Union[List[_V], _T]: ...
def popone(self, key: str) -> _V: ...
def popone(self, key: str, default: _T) -> Union[_V, _T]: ...
def copy(self) -> 'MultiDict[_V]': ...Case-insensitive multidict functionality optimized for HTTP headers and similar protocols where key casing should be ignored.
class CIMultiDict(MultiDict[_V]):
def __init__(self, arg: MDArg[_V] = None, /, **kwargs: _V): ...
# Inherits all MultiDict methods with case-insensitive key handling
def copy(self) -> 'CIMultiDict[_V]': ...Read-only proxy interfaces that provide safe access to multidict data without allowing modifications, useful for exposing internal state safely.
class MultiDictProxy(MultiMapping[_V]):
def __init__(self, multidict: Union[MultiDict[_V], MultiDictProxy[_V]]): ...
def getall(self, key: str) -> List[_V]: ...
def getall(self, key: str, default: _T) -> Union[List[_V], _T]: ...
def getone(self, key: str) -> _V: ...
def getone(self, key: str, default: _T) -> Union[_V, _T]: ...
def copy(self) -> MultiDict[_V]: ...
class CIMultiDictProxy(MultiDictProxy[_V]):
def __init__(self, ci_multidict: Union[CIMultiDict[_V], CIMultiDictProxy[_V]]): ...
def copy(self) -> CIMultiDict[_V]: ...Specialized string handling and utility functions for multidict operations, including case-insensitive strings and version tracking.
class istr(str):
"""Case-insensitive string type"""
def __new__(cls, val=''): ...
# Legacy alias for istr
upstr = istr
def getversion(md) -> int:
"""Get internal version number for change detection"""from typing import Union, List, Optional, TypeVar, Iterable, Mapping, Protocol
from collections.abc import MutableMapping, Iterator, KeysView, ItemsView, ValuesView
_V = TypeVar('_V')
_V_co = TypeVar('_V_co', covariant=True)
_T = TypeVar('_T')
# Protocol types for constructor arguments
class SupportsKeys(Protocol[_V_co]):
def keys(self) -> Iterable[str]: ...
def __getitem__(self, key: str, /) -> _V_co: ...
class SupportsIKeys(Protocol[_V_co]):
def keys(self) -> Iterable[istr]: ...
def __getitem__(self, key: istr, /) -> _V_co: ...
# Constructor argument types
MDArg = Union[
SupportsKeys[_V],
SupportsIKeys[_V],
Iterable[tuple[str, _V]],
None
]
# Abstract base classes
class MultiMapping(Mapping[str, _V]):
def getall(self, key: str) -> List[_V]: ...
def getall(self, key: str, default: _T) -> Union[List[_V], _T]: ...
def getone(self, key: str) -> _V: ...
def getone(self, key: str, default: _T) -> Union[_V, _T]: ...
class MutableMultiMapping(MultiMapping[_V], MutableMapping[str, _V]):
def add(self, key: str, value: _V) -> None: ...
def extend(self, arg: MDArg[_V] = None, /, **kwargs: _V) -> None: ...
def merge(self, arg: MDArg[_V] = None, /, **kwargs: _V) -> None: ...
def popone(self, key: str) -> _V: ...
def popone(self, key: str, default: _T) -> Union[_V, _T]: ...
def popall(self, key: str) -> List[_V]: ...
def popall(self, key: str, default: _T) -> Union[List[_V], _T]: ...