-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bug in TypeConversionDict #1106
Conversation
During conversion, TypeConversionDict returns the default value when conversion is not possible. Now, KeyError is propagated instead. This bug was reported in [1] [1]: #1102 Signed-off-by: Antonio Ossa <[email protected]>
This new test class includes two tescases: 1) Tests that the default value is returned when conversion is not possible, and 2) tests that KeyError is raised during conversion when key is not present Signed-off-by: Antonio Ossa <[email protected]>
No, it shouldn't. The idea behind #1102 is that the conversion callback raising a KeyError does not result in the default value being used, i.e. catch a KeyError from |
Mmm I see. So, what's the expected value of switch = {'a': 1}
data = TypeConversionDict(good='a', bad='b')
out = data.get('bad', 'default', lambda v: switch[v])
assert out != 'default' (Reading the docstring) The requested data exists ( Thanks for your feedback @ThiefMaster , really fast 😁 👌 |
Since your
|
So something like: try:
rv = self[key]
except KeyError:
rv = default
if type is not None:
try:
rv = type(rv)
except ValueError:
rv = default
return rv should work? |
Right, though in line 4 I would return directly.
…On Sat, Apr 15, 2017 at 09:05:25AM -0700, Antonio Ossa wrote:
So something like:
```python
try:
rv = self[key]
except KeyError:
rv = default
if type is not None:
try:
rv = type(rv)
except ValueError:
rv = default
return rv
```
should work?
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#1106 (comment)
|
Nice, I'll fix this in a few hours. Thanks for your comments! |
Already fixed it in my fork. Can you check if everything is ok now? If you aprove the changes, agains which branch should I make the PR? |
Yes this looks good. I think it would be better to do this in master. Please
also add a changelog and a small testcase.
…On Sat, Apr 15, 2017 at 10:01:46AM -0700, Antonio Ossa wrote:
Already fixed it in [my fork](https://github.com/aaossa/werkzeug). Can you check if everything is ok now? If you aprove the changes, agains which branch should I make the PR?
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#1106 (comment)
|
Added changes to changelog. The tests were already there in the previous commit 👌 Should use a new PR or re-open this one? |
It appears that I can't reopen this one, so please make a new one :) |
TypeConversionDict should raise KeyError during conversion when key is not present, but now is returning the default value. This PR fixes this bug and adds the corresponding test cases.
Fixes #1102