From e550ef8b313324d2efc0b9385be4c8c5739df36b Mon Sep 17 00:00:00 2001 From: David Lord Date: Tue, 29 Oct 2024 17:42:31 -0700 Subject: [PATCH] getlist catches TypeError --- CHANGES.rst | 3 ++- src/werkzeug/datastructures/structures.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 7dc7f0bd9..c76cce4c5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/src/werkzeug/datastructures/structures.py b/src/werkzeug/datastructures/structures.py index 34863ccc3..a48d504e4 100644 --- a/src/werkzeug/datastructures/structures.py +++ b/src/werkzeug/datastructures/structures.py @@ -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] @@ -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 @@ -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 @@ -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