Skip to content

Commit

Permalink
getlist catches TypeError (#2978)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Oct 30, 2024
2 parents e9f87fd + e550ef8 commit 862cb19
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Unreleased
:issue:`2969`
- Inline annotations for ``datastructures``, removing stub files.
:issue:`2970`

- ``MultiDict.getlist`` catches ``TypeError`` in addition to ``ValueError``
when doing type conversion. :issue:`2976`


Version 3.0.6
Expand Down
14 changes: 8 additions & 6 deletions src/werkzeug/datastructures/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,12 @@ def getlist(self, key: K, type: type[T] | None = None) -> list[V] | list[T]:
with the callable defined there.
:param key: The key to be looked up.
:param type: A callable that is used to cast the value in the
:class:`MultiDict`. If a :exc:`ValueError` is raised
by this callable the value will be removed from the list.
:param type: Callable to convert each value. If a ``ValueError`` or
``TypeError`` is raised, the value is omitted.
:return: a :class:`list` of all the values for the key.
.. versionchanged:: 3.1
Catches ``TypeError`` in addition to ``ValueError``.
"""
try:
rv: list[V] = super().__getitem__(key) # type: ignore[assignment]
Expand All @@ -275,7 +277,7 @@ def getlist(self, key: K, type: type[T] | None = None) -> list[V] | list[T]:
for item in rv:
try:
result.append(type(item)) # type: ignore[call-arg]
except ValueError:
except (ValueError, TypeError):
pass
return result

Expand Down Expand Up @@ -694,7 +696,7 @@ def getlist(self, key: K, type: type[T] | None = None) -> list[V] | list[T]:
for item in rv:
try:
result.append(type(item.value)) # type: ignore[call-arg]
except ValueError:
except (ValueError, TypeError):
pass
return result

Expand Down Expand Up @@ -836,7 +838,7 @@ def get( # type: ignore[misc]
if type is not None:
try:
return type(d[key]) # type: ignore[call-arg]
except ValueError:
except (ValueError, TypeError):
continue
return d[key]
return default
Expand Down

0 comments on commit 862cb19

Please sign in to comment.