Skip to content

Can't use validators as mock spec in Python 3.8 #695

@EpicWink

Description

@EpicWink

In Python 3.8, the object you pass as the spec argument to unittest.mock.Mock has its attributes iterated over. One of those attributes is DEFAULT_TYPES, which when accessed will raise jsonschema.validators._DontDoThat. This is not an issue in previous version of Python.

This will be solved by #681.

  • jsonschema: 3.2
  • Python: 3.8.3
from unittest import mock
import jsonschema
mock.Mock(spec=jsonschema.Draft7Validator)

Current solution:

import sys, jsonschema
Draft7Validator = jsonschema.Draft7Validator
if sys.version_info >= (3, 8):
    from importlib import metadata
    if int(metadata.version("jsonschema").split(".")[0]) < 4:
        class _Override(jsonschema.validators._DefaultTypesDeprecatingMetaClass):
            DEFAULT_TYPES = None
        class Draft7Validator(jsonschema.Draft7Validator, metaclass=_Override):
            pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions