Skip to content

Commit

Permalink
Adjust docs and add test for TypeError on TypeConversionDict.get()
Browse files Browse the repository at this point in the history
  • Loading branch information
Sympatron committed Jan 25, 2024
1 parent cc54be9 commit bad6f5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/werkzeug/datastructures/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ def get(self, key, default=None, type=None):
be looked up. If not further specified `None` is
returned.
: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 default value is returned.
:class:`MultiDict`. If a :exc:`ValueError` or a
:exc:`TypeError` is raised by this callable the default
value is returned.
.. versionchanged:: 3.0.2
Returns the default value on :exc:`TypeError`, too.
"""
try:
rv = self[key]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,9 @@ def test_value_conversion(self):
assert d.get("foo", type=int) == 1

def test_return_default_when_conversion_is_not_possible(self):
d = self.storage_class(foo="bar")
d = self.storage_class(foo="bar", baz=None)
assert d.get("foo", default=-1, type=int) == -1
assert d.get("baz", default=-1, type=int) == -1

def test_propagate_exceptions_in_conversion(self):
d = self.storage_class(foo="bar")
Expand Down

0 comments on commit bad6f5e

Please sign in to comment.