Replace for loops with comprehensions (#1759)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Almaz · committed by GitHub · parent b169013 · Browse files · 3 files·+27 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+27 added−26 removed
- tests:+5 added−8 removed
- other:+22 added−18 removed
Provenance
- Commit
b839478- Tree
e00f88e- Author
Almaz <hsyqixco@protonmail.com>- Committer
GitHub <noreply@github.com>- Parent
b169013
3 files changed+27 added−26 removed
modified:httpx/_models.py+22 added-18 removed
975 unmodified lines976 them with commas. See: https://tools.ietf.org/html/rfc7230#section-3.2.2977 """978 normalized_key = key.lower().encode(self.encoding)979 980- items = []981- for _, header_key, header_value in self._list:982- if header_key == normalized_key:983- items.append(header_value.decode(self.encoding))980+ items = [981+ header_value.decode(self.encoding)982+ for _, header_key, header_value in self._list983+ if header_key == normalized_key984+ ]985 986 if items:987 return ", ".join(items)988 7 unmodified lines996 set_key = key.encode(self._encoding or "utf-8")997 set_value = value.encode(self._encoding or "utf-8")998 lookup_key = set_key.lower()999 999- found_indexes = []1000- for idx, (_, item_key, _) in enumerate(self._list):1001- if item_key == lookup_key:1002- found_indexes.append(idx)1000+ found_indexes = [1001+ idx1002+ for idx, (_, item_key, _) in enumerate(self._list)1003+ if item_key == lookup_key1004+ ]1005 1006 for idx in reversed(found_indexes[1:]):1007 del self._list[idx]1008 8 unmodified lines1017 Remove the header `key`.1018 """1019 del_key = key.lower().encode(self.encoding)1020 1019- pop_indexes = []1020- for idx, (_, item_key, _) in enumerate(self._list):1021- if item_key.lower() == del_key:1022- pop_indexes.append(idx)1021+ pop_indexes = [1022+ idx1023+ for idx, (_, item_key, _) in enumerate(self._list)1024+ if item_key.lower() == del_key1025+ ]1026 1027 if not pop_indexes:1028 raise KeyError(key)1029 717 unmodified lines1747 """1748 if domain is not None and path is not None:1749 return self.jar.clear(domain, path, name)1750 1748- remove = []1749- for cookie in self.jar:1750- if cookie.name == name:1751- if domain is None or cookie.domain == domain:1752- if path is None or cookie.path == path:1753- remove.append(cookie)1751+ remove = [1752+ cookie1753+ for cookie in self.jar1754+ if cookie.name == name1755+ and (domain is None or cookie.domain == domain)1756+ and (path is None or cookie.path == path)1757+ ]1758 1759 for cookie in remove:1760 self.jar.clear(cookie.domain, cookie.path, cookie.name)1761 86 unmodified linesmodified:tests/models/test_responses.py+1 added-4 removed
570 unmodified lines571 response = httpx.Response(572 200,573 content=b"Hello,\nworld!",574 )575-576- content = []577- for line in response.iter_lines():578- content.append(line)575+ content = [line for line in response.iter_lines()]576 assert content == ["Hello,\n", "world!"]577 578 579 @pytest.mark.asyncio312 unmodified linesmodified:tests/conftest.py+4 added-4 removed
142 unmodified lines143 await send({"type": "http.response.body", "body": body})144 145 146 async def echo_headers(scope, receive, send):147- body = {}148- for name, value in scope.get("headers", []):149- body[name.capitalize().decode()] = value.decode()150-147+ body = {148+ name.capitalize().decode(): value.decode()149+ for name, value in scope.get("headers", [])150+ }151 await send(152 {153 "type": "http.response.start",154 "status": 200,139 unmodified lines