Skip to content

Commit

Permalink
Use str.partition() instead of .split() with a limit and a check (#6345)
Browse files Browse the repository at this point in the history
It's shorter, more idiomatic, and doesn't need noisy type casting.
  • Loading branch information
wbolster authored Dec 1, 2021
1 parent b446af7 commit 5f9a16d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
twine check --strict dist/*
- name: Making sure that CONTRIBUTORS.txt remains sorted
run: |
LC_ALL=C sort -c CONTRIBUTORS.txt
LC_ALL=C sort --check --ignore-case CONTRIBUTORS.txt
gen_llhttp:
name: Generate llhttp sources
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ repos:
^requirements/constraints[.]txt$
- id: trailing-whitespace
- id: file-contents-sorter
args: ['--ignore-case']
files: |
CONTRIBUTORS.txt|
docs/spelling_wordlist.txt|
Expand Down
1 change: 1 addition & 0 deletions CHANGES/6345.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use str.partition() instead of .split() with a limit and a check.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ Willem de Groot
William Grzybowski
William S.
Wilson Ong
wouter bolsterlee
Yang Zhou
Yannick Koechlin
Yannick Péroux
Expand Down
14 changes: 3 additions & 11 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,15 @@ def parse_mimetype(mimetype: str) -> MimeType:
for item in parts[1:]:
if not item:
continue
key, value = cast(
Tuple[str, str], item.split("=", 1) if "=" in item else (item, "")
)
key, _, value = item.partition("=")
params.add(key.lower().strip(), value.strip(' "'))

fulltype = parts[0].strip().lower()
if fulltype == "*":
fulltype = "*/*"

mtype, stype = (
cast(Tuple[str, str], fulltype.split("/", 1))
if "/" in fulltype
else (fulltype, "")
)
stype, suffix = (
cast(Tuple[str, str], stype.split("+", 1)) if "+" in stype else (stype, "")
)
mtype, _, stype = fulltype.partition("/")
stype, _, suffix = stype.partition("+")

return MimeType(
type=mtype, subtype=stype, suffix=suffix, parameters=MultiDictProxy(params)
Expand Down

0 comments on commit 5f9a16d

Please sign in to comment.