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

_WrappedReferencingError is unhashable #1126

Closed
johnnymetz opened this issue Jul 12, 2023 · 1 comment
Closed

_WrappedReferencingError is unhashable #1126

johnnymetz opened this issue Jul 12, 2023 · 1 comment
Labels
Bug Something doesn't work the way it should.

Comments

@johnnymetz
Copy link

Background

jsonschema.exceptions._WrappedReferencingError is unhashable. This is causing issues with error tracking services (e.g. Rollbar) when trying to report these errors to the service.

Steps to Reproduce

import jsonschema
from typing import Hashable

try:
    jsonschema.validate(
        instance={
            "definitions": {"apiVersion": "v1"},
        },
        schema={
            "properties": {
                "definitions": {
                    "$ref": "https://app.pixiebrix.com/schemas/innerDefinition#",
                },
            },
        },
    )
except jsonschema.exceptions._WrappedReferencingError as e:
    print(isinstance(e, Hashable))  # prints False
    print(hash(e))  # raisesTypeError: unhashable type: '_WrappedReferencingError'

Expected Behavior

I believe exceptions should be hashable. See the following simple example:

from typing import Hashable

try:
    assert False
except Exception as e:
    print(isinstance(e, Hashable))  # prints True
    print(hash(e))  # prints 270994222
@Julian
Copy link
Member

Julian commented Jul 13, 2023

(_WrappedReferencingError is a private implementation detail, so you should of course never be catching it as-is, just to be sure).

Not all exceptions are going to be hashable. In particular in the example Exception doesn't really pass around any data, so it's trivially hashable, whereas some exception that does have some unhashable data attached to it might not be.

I can leave this open to see if this specific example is possible to make hashable, but in general there's no such promise for exceptions in general.

Julian added a commit to python-jsonschema/referencing that referenced this issue Jul 18, 2023
@Julian Julian closed this as completed in 801a4ce Jul 18, 2023
@Julian Julian added the Bug Something doesn't work the way it should. label Mar 12, 2024
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

2 participants