| 41 | |
| 42 | |
| 43 | def ensure_can_construct_http_header_dict( |
| 44 | potential: object, |
| 45 | ) -> ValidHTTPHeaderSource | None: |
| 46 | if isinstance(potential, HTTPHeaderDict): |
| 47 | return potential |
| 48 | elif isinstance(potential, typing.Mapping): |
| 49 | # Full runtime checking of the contents of a Mapping is expensive, so for the |
| 50 | # purposes of typechecking, we assume that any Mapping is the right shape. |
| 51 | return typing.cast(typing.Mapping[str, str], potential) |
| 52 | elif isinstance(potential, typing.Iterable): |
| 53 | # Similarly to Mapping, full runtime checking of the contents of an Iterable is |
| 54 | # expensive, so for the purposes of typechecking, we assume that any Iterable |
| 55 | # is the right shape. |
| 56 | return typing.cast(typing.Iterable[tuple[str, str]], potential) |
| 57 | elif hasattr(potential, "keys") and hasattr(potential, "__getitem__"): |
| 58 | return typing.cast("HasGettableStringKeys", potential) |
| 59 | else: |
| 60 | return None |
| 61 | |
| 62 | |
| 63 | class RecentlyUsedContainer(typing.Generic[_KT, _VT], typing.MutableMapping[_KT, _VT]): |