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

refactor: use schemapi from altair submodule #101

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
pip install .[dev]

- name: Run pytest
run: pytest --ignore gosling/examples --doctest-modules gosling
run: pytest --ignore gosling/examples --ignore tools/altair --doctest-modules gosling

deploy:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "tools/altair"]
path = tools/altair
url = https://github.com/altair-viz/altair.git
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
pytest --ignore gosling/examples --doctest-modules gosling
pytest --ignore gosling/examples --ignore tools/altair --doctest-modules gosling

clean-generated:
rm -rf doc/_build
Expand Down
22 changes: 16 additions & 6 deletions gosling/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class SchemaBase(object):
_schema = None
_rootschema = None
_class_is_valid_at_instantiation = True
_validator = jsonschema.Draft7Validator

def __init__(self, *args, **kwds):
# Two valid options for initialization, which should be handled by
Expand Down Expand Up @@ -439,7 +440,9 @@ def validate(cls, instance, schema=None):
if schema is None:
schema = cls._schema
resolver = jsonschema.RefResolver.from_schema(cls._rootschema or cls._schema)
return jsonschema.validate(instance, schema, resolver=resolver)
return jsonschema.validate(
instance, schema, cls=cls._validator, resolver=resolver
)

@classmethod
def resolve_references(cls, schema=None):
Expand All @@ -464,6 +467,10 @@ def __dir__(self):
return list(self._kwds.keys())


def _passthrough(*args, **kwds):
return args[0] if args else kwds


class _FromDict(object):
"""Class used to construct SchemaBase class hierarchies from a dict

Expand Down Expand Up @@ -518,7 +525,9 @@ def _freeze(val):

return hash(_freeze(schema))

def from_dict(self, dct, cls=None, schema=None, rootschema=None):
def from_dict(
self, dct, cls=None, schema=None, rootschema=None, default_class=_passthrough
):
"""Construct an object from a dict representation"""
if (schema is None) == (cls is None):
raise ValueError("Must provide either cls or schema, but not both.")
Expand All @@ -527,9 +536,6 @@ def from_dict(self, dct, cls=None, schema=None, rootschema=None):
rootschema = rootschema or cls._rootschema
rootschema = rootschema or schema

def _passthrough(*args, **kwds):
return args[0] if args else kwds

if isinstance(dct, SchemaBase):
return dct

Expand All @@ -538,7 +544,10 @@ def _passthrough(*args, **kwds):
# Our class dict is constructed breadth-first from top to bottom,
# so the first class that matches is the most general match.
matches = self.class_dict[self.hash_schema(schema)]
cls = matches[0] if matches else _passthrough
if matches:
cls = matches[0]
else:
cls = default_class
schema = _resolve_references(schema, rootschema)

if "anyOf" in schema or "oneOf" in schema:
Expand All @@ -554,6 +563,7 @@ def _passthrough(*args, **kwds):
dct,
schema=possible_schema,
rootschema=rootschema,
default_class=cls,
)

if isinstance(dct, dict):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ python_requires = >=3.7
packages = find:
include_package_data = True
install_requires =
jsonschema>=3.0,<4.0
jsonschema>=3.0
jinja2
pandas

Expand Down
1 change: 1 addition & 0 deletions tools/altair
Submodule altair added at 2bb4ac
10 changes: 5 additions & 5 deletions tools/generate_schema_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import textwrap
from typing import Optional, TypeVar

import generate_api_docs # noqa: E402

# import schemapi from here
here = pathlib.Path(__file__)
sys.path.insert(0, str(here.parent))
sys.path.insert(0, str(here.parent / 'altair' / 'tools'))

from schemapi import codegen
from schemapi.codegen import CodeSnippet
Expand All @@ -19,7 +21,6 @@
indent_arglist,
resolve_references,
)
import generate_api_docs # noqa: E402

T = TypeVar("T")

Expand Down Expand Up @@ -202,10 +203,9 @@ def copy_schemapi_util():
"""
Copy the schemapi utility and its test file into gosling/utils/
"""
current_dir = here.parent
# copy the schemapi utility file
source_path = current_dir / "schemapi" / "schemapi.py"
destination_path = current_dir / ".." / "gosling" / "schemapi.py"
source_path = here.parent / 'altair' / 'tools' / "schemapi" / "schemapi.py"
destination_path = here.parent / ".." / "gosling" / "schemapi.py"

if not destination_path.parent.exists():
os.makedirs(destination_path.parent)
Expand Down
9 changes: 0 additions & 9 deletions tools/schemapi/__init__.py

This file was deleted.

219 changes: 0 additions & 219 deletions tools/schemapi/codegen.py

This file was deleted.

Loading