Skip to content

Commit

Permalink
fix mypy and test py39
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Wigley committed Dec 1, 2020
1 parent ae61097 commit 30f3b48
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 31 deletions.
84 changes: 64 additions & 20 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,60 +1,104 @@
version: 2

jobs:
py36:
lint:
docker:
- image: circleci/python:3.6

steps: &test-steps
- image: circleci/python:3.9
steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "setup.py" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- v1-lint-{{ checksum "setup.py" }}
- v1-lint-

- run:
name: "Setup"
command: |
python3 -m venv venv
. venv/bin/activate
python setup.py develop
pip install black pytest mypy
pip install . black mypy
- save_cache:
paths:
- ./venv
key: v1-lint-{{ checksum "setup.py" }}

- run:
name: "Check code formatting"
command: |
. venv/bin/activate
black --check hologram/ tests/
- run:
name: "Run mypy"
command: |
. venv/bin/activate
mypy hologram
py36:
docker:
- image: circleci/python:3.6

steps: &test-steps
- checkout

- restore_cache:
keys:
- v1-unit-{{ checksum "setup.py" }}
# fallback to using the latest cache if no exact match is found
- v1-unit-

- run:
name: "Setup"
command: |
python3 -m venv venv
. venv/bin/activate
pip install . pytest
- save_cache:
paths:
- ./venv
key: v1-dependencies-{{ checksum "setup.py" }}
key: v1-unit-{{ checksum "setup.py" }}

- run:
name: "Run tests"
command: |
. venv/bin/activate
pytest
- run:
name: "Run mypy"
command: |
. venv/bin/activate
mypy hologram
py37:
docker:
- image: circleci/python:3.7

steps: *test-steps

py38:
docker:
- image: circleci/python:3.8

steps: *test-steps

py39:
docker:
- image: circleci/python:3.9

steps: *test-steps

workflows:
version: 2
build-all:
jobs:
- py36
- py37
- lint
- py36:
requires:
- lint
- py37:
requires:
- lint
- py38:
requires:
- lint
- py39:
requires:
- lint
7 changes: 5 additions & 2 deletions hologram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get_type_hints,
Callable,
Generic,
Hashable,
)
import re
from datetime import datetime
Expand Down Expand Up @@ -183,7 +184,8 @@ def as_dict(self) -> Dict:


@functools.lru_cache()
def _validate_schema(schema_cls: Type[T]) -> JsonDict:
def _validate_schema(h_schema_cls: Hashable) -> JsonDict:
schema_cls = cast(Type[JsonSchemaMixin], h_schema_cls) # making mypy happy
schema = schema_cls.json_schema()
jsonschema.Draft7Validator.check_schema(schema)
return schema
Expand Down Expand Up @@ -958,7 +960,8 @@ def _get_field_type_name(field_type: Any) -> str:

@classmethod
def validate(cls, data: Any):
schema = _validate_schema(cls)
h_cls = cast(Hashable, cls)
schema = _validate_schema(h_cls)
validator = jsonschema.Draft7Validator(schema)
error = next(iter(validator.iter_errors(data)), None)
if error is not None:
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ def read(f):
name="hologram",
description="JSON schema generation from dataclasses",
long_description=read("README.md"),
long_description_content_type='text/markdown',
long_description_content_type="text/markdown",
packages=["hologram"],
package_data={"hologram": ["py.typed"]},
version=package_version,
author="Connor McArthur, Jacob Beck, Simon Knibbs",
author_email="info@fishtowanalytics.com, simon.knibbs@gmail.com",
url="https://github.com/fishtown-analytics/hologram",
install_requires=requires,
setup_requires=["pytest-runner", "setuptools_scm", "wheel"],
tests_require=["pytest", "flake8", "mypy"],
license="MIT",
classifiers=[
"Development Status :: 4 - Beta",
Expand All @@ -35,6 +33,8 @@ def read(f):
"Programming Language :: Python",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Topic :: Software Development :: Libraries",
],
)
5 changes: 3 additions & 2 deletions third-party-stubs/jsonschema/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ from typing import Any, Dict, Iterable
# we need the 'as' to tell mypy we want this visible with both names
from jsonschema.exceptions import ValidationError as ValidationError


class Draft7Validator:
def __init__(self, schema: Dict[str, Any]) -> None: ...
@classmethod
def check_schema(cls, schema: Dict[str, Any]) -> None: ...
def iter_errors(self, instance, _schema=None) -> Iterable[ValidationError]: ...
def iter_errors(
self, instance, _schema=None
) -> Iterable[ValidationError]: ...
5 changes: 1 addition & 4 deletions third-party-stubs/jsonschema/exceptions.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Any, Optional, Tuple, Iterable, TypeVar, Type

# we need this to denote that create_from applies to subclasses
T = TypeVar('T', bound=ValidationError)

T = TypeVar("T", bound=ValidationError)

class ValidationError(Exception):
def __init__(
Expand All @@ -18,9 +17,7 @@ class ValidationError(Exception):
schema_path: Tuple[Any] = ...,
parent: Any = ...,
) -> None: ...

@classmethod
def create_from(cls: Type[T], ValidationError) -> T: ...


def best_match(errors: Iterable[ValidationError]) -> ValidationError: ...

0 comments on commit 30f3b48

Please sign in to comment.