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

0 should not be considered equal to False #575

Closed
Zac-HD opened this issue Jul 1, 2019 · 8 comments
Closed

0 should not be considered equal to False #575

Zac-HD opened this issue Jul 1, 2019 · 8 comments
Labels
Bug Something doesn't work the way it should.

Comments

@Zac-HD
Copy link
Member

Zac-HD commented Jul 1, 2019

I'm working on some schema canonicalisation and intersection logic for hypothesis-jsonschema, and encountered the surprising-to-me property that jsonschema sometimes considers 0 == False (and similarly 1 == True):

# All of these pass:
validate(0, {"just": False})            # 0 == False
validate(False, {"enum": [0.0]})        # 0.0 == False
# Fails, "On instance: [0, 0]", despite `unbool` logic
validate([0, False], {"uniqueItems": True})

According to the core spec, instances are only equal if they are of the same type.

(and there is only one number type in JSON, so 1 == 1.0)

The validation spec doesn't actually say anything about how value equality (ctrl-f "equal") should be defined, but I certainly found this surprising and the uniqueItems message convinced me to report it.

@Julian
Copy link
Member

Julian commented Jul 1, 2019

The latter likely related to your still-open #529.

I actually... don't have an opinion yet :D, have to think about what I expect.

Obviously the inconsistency is wrong, but not sure which to expect. I guess slightly leaning towards them always being equal, but have to think.

Thoughts welcome of course :), and yeah thanks for filing obviously!

@Zac-HD
Copy link
Member Author

Zac-HD commented Jul 1, 2019

It's not obvious, thats for sure! My preference would be to treat all numeric zeros (integer or float) as equal, but booleans as unequal to numbers. Either way, it's a question that should be addressed by the specification since it does change the semantics.

Treating booleans as unequal to numbers unlocks several nice optimisations in the logic I usr for canonicalising schemas and calculating intersections - with I can treat them as enums; without I have to keep and track the type information too. Not sure if that will convince anyone but me though 😅

Final fun fact: IEEE754 allows you to represent negative zero, but it can't be serialised as JSON. So hypothesis-jsonschema already has special handling for zeros, even before this issue!

@gregsdennis
Copy link

gregsdennis commented Jul 2, 2019

Having 0 synonymous to false is certainly a language-specific thing. It is in many C-based languages, but notably not in C# (or any .Net language). I imagine there are other such cases in other strongly-typed languages where integers and booleans are distinct concepts.

JSON Schema as a spec has historically (and will remain) language-agnostic, so I disagree with this being in the spec. It would require that languages that distinguish between integers and booleans (e.g. C# and other .Net languages) would have to support something that is contrary to the language/framework in which their written, which would be a poor developer experience.

If an implementation that is written in a language that supports equivalency between integers and booleans wishes to support this, then I see it as an augmentation over JSON Schema and should be documented as such.

@Zac-HD
Copy link
Member Author

Zac-HD commented Jul 2, 2019

JSON Schema as a spec has historically (and will remain) language-agnostic, so I disagree with this being in the spec. It would require that languages that distinguish between integers and booleans (e.g. C# and other .Net languages) would have to support something that is contrary to the language/framework in which their written, which would be a poor developer experience.

The options are that either:

  • Jsonschema has different equality semantics to (some) host languages, or
  • There are instances which may or may not validate against a given schema depending on which language you're using.

The former is certainly unintuitive, but the latter is a disaster for interoperability: imagine the developer experience of an instance passing validation on the (JS) frontend, but failing at the (C#) backend with the same schema!

@Julian
Copy link
Member

Julian commented Jul 2, 2019 via email

@Zac-HD
Copy link
Member Author

Zac-HD commented Jul 8, 2019

My problem here is that "different languages, even among those that distinguish booleans from integers, have different validation semantics" is the opposite of a language-agnostic specification.

To put that another way, a language-agnostic spec would have the same semantics regardless of language, so that whether an instance validates against some schema is always independent of which language is being used to validate it. I think this is a very important property for a validation spec to have.

@Julian
Copy link
Member

Julian commented Jul 19, 2019

Uh another funny example of this by the way, or at least a slightly related one -- {"oneOf": [{"format": "date"}, {}]} which does various things not only across languages but potentially across implementations or here the same implementation :) (see e.g. #585, #482, and countless other users finding this out).

@Zac-HD Zac-HD changed the title Should 0 == False ? 0 should not be considered equal to False Jul 22, 2019
@Zac-HD
Copy link
Member Author

Zac-HD commented Jul 22, 2019

A-ha! Looking in the core specificiation (instead of the validation spec):

Two JSON instances are said to be equal if and only if they are of the same type and have the same value according to the data model. Specifically, this means: ... both are numbers, and have the same mathematical value; or ...

So 1 == 1.0, but 1 != True.

@Julian Julian added the Bug Something doesn't work the way it should. label Jul 26, 2019
Julian added a commit that referenced this issue Aug 1, 2019
@Julian Julian closed this as completed in 27eb5d2 Aug 2, 2019
ankostis added a commit to ankostis/jsonschema that referenced this issue Apr 3, 2020
 to evade failures due to overridden equals e.g. pandas.
 Patches changes of python-jsonschema#575.
ankostis added a commit to ankostis/jsonschema that referenced this issue Apr 3, 2020
…)...

 to evade failures due to overridden equals e.g. pandas.
 Patches changes of python-jsonschema#575.
Julian added a commit that referenced this issue Jul 11, 2022
69acf529 Merge pull request #572 from json-schema-org/default-reviewers
26a51ca3 Merge pull request #575 from jdesrosiers/add-package-json
d0e80a96 Re-add package.json
88d69482 Merge pull request #573 from json-schema-org/handling-nulls-in-instances
bd653f34 fixed findings by julian
eab34ba6 fix `prefixItems` tests to actually use it (instead of `items`, copy-paste error)
6680a003 fixed typo in `type` declaration: surround with quotes
257f5220 added null-handling tests for unevaluatedItems
e3c007dd added null-handling tests for unevaluatedProperties
3d3bdf4e added null-handling tests for properties
54cc5703 added null-handling tests for patternProperties
28419842 added null-handling tests for contains
4d1a916a added null-handling tests for additionalItems
6e47a99a added null-handling tests for prefixItems; fixed indentation on previous commit
761d4b2b added null-handling tests for array-form items
6315a51d added null-handling tests for single-form items
278e5b3b added null-handling tests for additionalProperties
bce6b82a Set up default reviewers (the whole test suite team).
9cb7da92 Merge pull request #564 from json-schema-org/sanity-check-terminology
2a316683 Merge pull request #569 from spacether/feat_adds_new_python_consumer
fae0017d Removes idea files
8af12a3f Adds python-experimental to the users list
c7a0f3e9 Reconcile terminology in the sanity checker with the README

git-subtree-dir: json
git-subtree-split: 69acf52990b004240839ae19b4bec8fb01d50876
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