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
AcceptedExtra not working as expected with dicts #47
Comments
This is working as intended (under the current API) but your expectation is entirely reasonable and I think datatest should be changed to use an I can resolve this in the upcoming 0.9.6 release when I clean up the acceptance API. Below, I'll walk through your example to explain what's going on and why it was intentional—those legacy reasons. Then I'll describe the fix I have in mind. Here's a version of your code that doesn't include an acceptance: from datatest import validate actual = {'a': 1, 'b': 2} expected = {'b': 2} validate(actual, expected) As you noted, this fails with: ValidationError: does not satisfy mapping requirements (1 difference): { 'a': Deviation(+1, None), } Because the difference type is a from datatest import validate, accepted actual = {'a': 1, 'b': 2} expected = {'b': 2} with accepted.deviation(1): validate(actual, expected) But look at this modified version of your example (using strings instead of integers): from datatest import validate actual = {'a': 'x', 'b': 'y'} expected = {'b': 'y'} validate(actual, expected) Now this fails with that ValidationError: does not satisfy mapping requirements (1 difference): { 'a': Extra('x'), } Here is what's going on: The type of difference used is determined by the type of object being checked. And with mappings, there can be situations (as in your example) where values don't have anything to be compared against—a sort of null condition. Datatest tries to generate sensable differences for these value-vs-no value situations. And when I designed the behavior, I was trying to make it interoperate with the acceptances as smoothly as possible. But because the acceptances weren't as flexible as they are now, I had to make some tough choices. I decided on the following:
I really hated that last one but I was the least awful thing to do at the time. This scheme helped the workflow when adding acceptances. But there's no reason to keep doing this and it's not the behavior people expect to see. The fix I have in mindI think I can unify the no value comparison behavior as follows:
And then replace the current This would make it so your original example would fail with: ValidationError: does not satisfy mapping requirements (1 difference): { 'a': Extra(1), } Which could then be accepted with... from datatest import validate, accepted actual = {'a': 1, 'b': 2} expected = {'b': 2} with accepted(Extra): # <- new format in upcoming API validate(actual, expected) ...or with... from datatest import validate, accepted actual = {'a': 1, 'b': 2} expected = {'b': 2} with accepted.tolerance(1): # Accepts +/- 1 validate(actual, expected) I'll follow up when I start to address this. |
This is a first step towards addressing issue #47.
That makes sense. Thanks! |
…gs(). These changes should allow for a broader range of quantative types beyond datetimes and strictly-numeric types. Related to issue #47.
After testing this in production, it will be added to the docs and the existing AllowedDeviation methods will be deprecated. Related to issue #47.
Now returns Missing and Extra differences when checking numbers against a NOVALUE sentinel. See issue #47 for details.
This has been resolved in the development version for a few weeks but I just published a new stable release (0.9.6) so the fix is available from PyPI now, too. Thanks @TheOtherDude for filing the issue. |
I expected
with AcceptedExtra():
to ignore missing keys in dicts, but instead it raises aDeviation
fromNone
.Here is an example:
The output is:
Thanks for the cool package, by the way!
The text was updated successfully, but these errors were encountered: