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

"TypeError: argument of type 'bool' is not iterable" when unevaluatedProperties present but instance is boolean #867

Closed
shabbyrobe opened this issue Oct 26, 2021 · 5 comments
Labels
Bug Something doesn't work the way it should.

Comments

@shabbyrobe
Copy link

shabbyrobe commented Oct 26, 2021

Here's a minimum reproducer with jsonschema==4.1.2:

import jsonschema

validator = jsonschema.Draft202012Validator({
    "type": ["object", "boolean"],
    "unevaluatedProperties": False,
    "properties": {
        "foo": { "type": "string" },
    },
})

validator.validate(True)

I get the following exception:

Traceback (most recent call last):
  File "bug.py", line 12, in <module>
    validator.validate(instance)
  File "jsonschema-bug/venv/lib/python3.8/site-packages/jsonschema/validators.py", line 248, in validate
    for error in self.iter_errors(*args, **kwargs):
  File "jsonschema-bug/venv/lib/python3.8/site-packages/jsonschema/validators.py", line 224, in iter_errors
    for error in errors:
  File "jsonschema-bug/venv/lib/python3.8/site-packages/jsonschema/_validators.py", line 434, in unevaluatedProperties
    evaluated_property_keys = find_evaluated_property_keys_by_schema(
  File "sonschema-bug/venv/lib/python3.8/site-packages/jsonschema/_utils.py", line 295, in find_evaluated_property_keys_by_schema
    if property in instance and validator.evolve(
TypeError: argument of type 'bool' is not iterable

I wonder if the checks on line 286 and 293 should also consider the type of the instance before acting on it? I would expect the above schema to pass, but even if it shouldn't, this isn't a great error for it to be raising. I'm very happy to submit a PR if someone can nudge me in the right direction for how to approach a fix.

Just to preempt a little bit, I'm aware the example can be rewritten as a oneOf/anyOf, but that's neither here nor there; this is intended as a minimum reproducer of an issue I found rather than a demonstration of what I'm actually doing in practice.

@Julian Julian added the Bug Something doesn't work the way it should. label Oct 26, 2021
@robherring
Copy link
Contributor

Looks like the same type of issue as commit 12a92ea. 'additionalProperties' has the object type check, but 'unevaluatedProperties' does not. Comparing 'additionalItems' and 'unevaluatedItems', the same issue exists there too.

@Julian
Copy link
Member

Julian commented Oct 30, 2021

Thanks for the report!

I wonder if the checks on line 286 and 293 should also consider the type of the instance before acting on it? I would expect the above schema to pass, but even if it shouldn't, this isn't a great error for it to be raising. I'm very happy to submit a PR if someone can nudge me in the right direction for how to approach a fix.

A PR would be very welcome -- the first step would be adding tests to the official test suite here: https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/tests/draft2020-12/unevaluatedProperties.json which is what is run as part of this library's test suite to essentially "force" its implementation to be correct.

Could you perhaps add one there which I can merge, and then it should be reasonably easy to add some lines here that fix the problem? Let me know if you need further help.

@shabbyrobe
Copy link
Author

Thanks Julian, I'll definitely take a run at that and let you know once I have a PR there on the test repo.

I cloned this repo last night too after robherring's helpful hints but I had all kinds of trouble getting the tests to run using tox. I was getting some cryptic ungooglable error about wheels that nobody else in the world seems to be having 😆. Is there a quick-and-dirty way you can point me to for running the jsonschema repo's test suite quickly as part of a development workflow that doesn't involve the whole tox rigmarole?

@Julian
Copy link
Member

Julian commented Oct 31, 2021

Great!

Is there a quick-and-dirty way you can point me to for running the jsonschema repo's test suite quickly

pip install . && yourfavoritetestrunner should do it -- e.g.:

python3.9 -m venv venv && venv/bin/python -m pip install . pytest && venv/bin/python -m pytest jsonschema

@Julian
Copy link
Member

Julian commented Jul 25, 2022

This was fixed in #949.

@Julian Julian closed this as completed Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something doesn't work the way it should.
Projects
None yet
Development

No branches or pull requests

3 participants