Skip to content

Fix formatting error for unique items if instance is a tuple #224

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

Merged
merged 2 commits into from
Sep 17, 2015
Merged

Fix formatting error for unique items if instance is a tuple #224

merged 2 commits into from
Sep 17, 2015

Conversation

playpauseandstop
Copy link
Contributor

I have found very uncommon, but still a bug, with formatting error messages for unique items, when instance is a tuple.

Attached a test case to reproduce:

import unittest

from jsonschema.exceptions import ValidationError
from jsonschema.validators import Draft4Validator, validate


TEST_SCHEMA = {
    'type': 'array',
    'items': {
        'type': 'number',
    },
    'minItems': 2,
    'maxItems': 3,
    'uniqueItems': True,
}


TYPES = Draft4Validator.DEFAULT_TYPES.copy()
TYPES['array'] = (list, tuple)


class Validator(Draft4Validator):

    DEFAULT_TYPES = TYPES


class TestItemsErrorMessages(unittest.TestCase):

    array_type = list
    validator_class = Draft4Validator

    def check_error_message(self, instance):
        self.assertRaises(ValidationError,
                          validate,
                          instance,
                          TEST_SCHEMA,
                          self.validator_class)

    def test_min_items(self):
        self.check_error_message(self.array_type([1]))

    def test_max_items(self):
        self.check_error_message(self.array_type([1, 2, 3, 4]))

    def test_unique_items(self):
        self.check_error_message(self.array_type([1, 1, 2]))


class TestItemsErrorMessagesCustomValidator(TestItemsErrorMessages):

    array_type = tuple
    validator_class = Validator


if __name__ == '__main__':
    unittest.main()

Its results:

playpauseandstop@shep:~/Projects/rororo$ env/bin/python ../jsonschema_unique_items.py 
.....E
======================================================================
ERROR: test_unique_items (__main__.TestItemsErrorMessagesCustomValidator)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "../jsonschema_unique_items.py", line 46, in test_unique_items
    self.check_error_message(self.array_type([1, 1, 2]))
  File "../jsonschema_unique_items.py", line 37, in check_error_message
    self.validator_class)
  File "/home/playpauseandstop/.pyenv/versions/3.4.3/lib/python3.4/unittest/case.py", line 704, in assertRaises
    return context.handle('assertRaises', callableObj, args, kwargs)
  File "/home/playpauseandstop/.pyenv/versions/3.4.3/lib/python3.4/unittest/case.py", line 162, in handle
    callable_obj(*args, **kwargs)
  File "/home/playpauseandstop/Projects/rororo/env/lib/python3.4/site-packages/jsonschema/validators.py", line 428, in validate
    cls(schema, *args, **kwargs).validate(instance)
  File "/home/playpauseandstop/Projects/rororo/env/lib/python3.4/site-packages/jsonschema/validators.py", line 116, in validate
    for error in self.iter_errors(*args, **kwargs):
  File "/home/playpauseandstop/Projects/rororo/env/lib/python3.4/site-packages/jsonschema/validators.py", line 95, in iter_errors
    for error in errors:
  File "/home/playpauseandstop/Projects/rororo/env/lib/python3.4/site-packages/jsonschema/_validators.py", line 139, in uniqueItems
    yield ValidationError("%r has non-unique elements" % instance)
TypeError: not all arguments converted during string formatting

----------------------------------------------------------------------
Ran 6 tests in 0.012s

FAILED (errors=1)

And the reason is easy, when formatting min/max items error messages you use '%r' % (instance,) statements, but for unique items don't, so when tuple is an instance, this broken validation message.

@playpauseandstop playpauseandstop changed the title Fix formatting error for unique items if instance is tuple Fix formatting error for unique items if instance is a tuple May 8, 2015
@Julian
Copy link
Member

Julian commented May 8, 2015

Sigh, yup definitely a bug, one-arg str % basically always ends up being one :(.

Could you possibly add your test to the suite as well rather than just here in the ticket? There's already a test case that makes assertions about error messages which should make it I hope not that difficult to add? Otherwise lgtm, thanks so much for filing.

@playpauseandstop
Copy link
Contributor Author

@Julian

I'm not sure where exactly to put my test case and if it good idea to test against Validator with custom default types. But if it ok, please point me in which exact test case should I move my test case, maybe test_validators:TestValidationErrorDetails?

@playpauseandstop
Copy link
Contributor Author

FYI, added tests for items errors to jsonschema.tests.test_validators module

@Julian
Copy link
Member

Julian commented Sep 17, 2015

Hey, so sorry this took so long for me to merge :( but did so now. Really appreciate the PR.

@Julian Julian merged commit 7c4eefb into python-jsonschema:master Sep 17, 2015
Julian added a commit that referenced this pull request Jun 2, 2018
e64ebf9 Merge pull request #226 from hrzndhrn/add/date-stirng-tests
f25195c Remove test unrelated to this PR
113497d Put tests in the right file
03480d4 Fix test with negative offset
eadbf69 Add tests for date-strings
4336571 Merge pull request #224 from santhosh-tekuri/content_nonstrings
1507e57 content: ignore non-strings

git-subtree-dir: json
git-subtree-split: e64ebf90a001f4e0e18984d2086ea15765cfead2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants