Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Fix bug in TypeConversionDict #1106

wants to merge 2 commits into from

Conversation

aaossa
Copy link
Contributor

@aaossa aaossa commented Apr 15, 2017

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

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]>
@ThiefMaster
Copy link
Member

ThiefMaster commented Apr 15, 2017

TypeConversionDict should raise KeyError during conversion when key is not present

No, it shouldn't. .get() is never supposed to raise a KeyError due to the key not existing.

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 rv = self[key] and then catch a ValueError from rv = type(rv)

@aaossa
Copy link
Contributor Author

aaossa commented Apr 15, 2017

Mmm I see. So, what's the expected value of out in this code from the issue?

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 ('bad'), so the default value ('default') should not be used. type is provided and is a callable (lambda v: switch[v]), so it should try to convert the value (but won't be possible, so...), return the value ('b') or raise ValueError if conversion is not possible (in this case is not possible). What should be the value of out? When should ValueError be raised?

Thanks for your feedback @ThiefMaster , really fast 😁 👌

@untitaker
Copy link
Contributor

Since your type raises a KeyError (or anything other than ValueError), it should bubble up.

ValueError coming from type is caught though.

@aaossa
Copy link
Contributor Author

aaossa commented Apr 15, 2017

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?

@untitaker
Copy link
Contributor

untitaker commented Apr 15, 2017 via email

@aaossa
Copy link
Contributor Author

aaossa commented Apr 15, 2017

Nice, I'll fix this in a few hours. Thanks for your comments!

@aaossa
Copy link
Contributor Author

aaossa commented Apr 15, 2017

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?

@untitaker
Copy link
Contributor

untitaker commented Apr 15, 2017 via email

@aaossa
Copy link
Contributor Author

aaossa commented Apr 15, 2017

Added changes to changelog. The tests were already there in the previous commit 👌 Should use a new PR or re-open this one?

@untitaker
Copy link
Contributor

It appears that I can't reopen this one, so please make a new one :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeConversionDict should not return default for KeyError during conversion
3 participants