Skip to content

Commit

Permalink
python-jsonschema#251 - correcting types
Browse files Browse the repository at this point in the history
  • Loading branch information
sloanlance committed Sep 30, 2023
1 parent 99742fb commit b6489a0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 41 deletions.
3 changes: 1 addition & 2 deletions src/check_jsonschema/checker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import pathlib
import typing as t

import click
Expand Down Expand Up @@ -47,7 +46,7 @@ def _fail(self, msg: str, err: Exception | None = None) -> t.NoReturn:
raise _Exit(1)

def get_validator(
self, path: pathlib.Path, doc: dict[str, t.Any]
self, path: str, doc: dict[str, t.Any]
) -> jsonschema.protocols.Validator:
try:
return self._schema_loader.get_validator(
Expand Down
10 changes: 3 additions & 7 deletions src/check_jsonschema/cli/main_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
import textwrap
from io import TextIOWrapper
import typing as t

import click
import jsonschema
Expand Down Expand Up @@ -59,13 +59,11 @@ def pretty_helptext_list(values: list[str] | tuple[str, ...]) -> str:
class FilesDefaultStdin(click.Argument):
def __init__(self, *args, **kwargs):
kwargs['nargs'] = -1
kwargs['type'] = click.File('r') # will work with '-' for stdin?
# kwargs['type'] = click.Path() # works with '/dev/stdin'
kwargs['type'] = click.File('rb')
super().__init__(*args, **kwargs)

def process_value(self, ctx, value):
return super().process_value(ctx, value or ('-',))
# return super().process_value(ctx, value or ('/dev/stdin',))


@click.command(
Expand Down Expand Up @@ -230,7 +228,6 @@ def process_value(self, ctx, value):
help="Reduce output verbosity",
count=True,
)
# @click.argument("instancefiles", required=True, nargs=-1)
@click.argument("instancefiles", cls=FilesDefaultStdin)
def main(
*,
Expand All @@ -250,8 +247,7 @@ def main(
output_format: str,
verbose: int,
quiet: int,
# instancefiles: tuple[str, ...],
instancefiles: tuple[TextIOWrapper, ...],
instancefiles: tuple[t.BinaryIO, ...],
) -> None:
args = ParseResult()

Expand Down
5 changes: 2 additions & 3 deletions src/check_jsonschema/cli/parse_result.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import enum
from io import TextIOWrapper
import typing as t

import click
import jsonschema
Expand All @@ -22,8 +22,7 @@ def __init__(self) -> None:
self.schema_mode: SchemaLoadingMode = SchemaLoadingMode.filepath
self.schema_path: str | None = None
self.base_uri: str | None = None
# self.instancefiles: tuple[str, ...] = ()
self.instancefiles: tuple[TextIOWrapper, ...] = ()
self.instancefiles: tuple[t.BinaryIO, ...] = ()
# cache controls
self.disable_cache: bool = False
self.cache_filename: str | None = None
Expand Down
20 changes: 8 additions & 12 deletions src/check_jsonschema/instance_loader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations

import pathlib
import typing as t
from io import TextIOWrapper

from .parsers import ParseError, ParserSet
from .transforms import Transform
Expand All @@ -11,12 +9,11 @@
class InstanceLoader:
def __init__(
self,
# filenames: t.Sequence[str],
filenames: t.Sequence[TextIOWrapper],
files: t.Sequence[t.BinaryIO],
default_filetype: str = "json",
data_transform: Transform | None = None,
) -> None:
self._filenames = filenames
self._files = files
self._default_filetype = default_filetype
self._data_transform = (
data_transform if data_transform is not None else Transform()
Expand All @@ -26,15 +23,14 @@ def __init__(
modify_yaml_implementation=self._data_transform.modify_yaml_implementation
)

def iter_files(self) -> t.Iterator[tuple[pathlib.Path, ParseError | t.Any]]:
for fn in self._filenames:
# path = pathlib.Path(fn)
def iter_files(self) -> t.Iterator[tuple[str, ParseError | t.Any]]:
for file in self._files:
try:
# data: t.Any = self._parsers.parse_file(path, self._default_filetype)
data: t.Any = self._parsers.parse_file(fn, self._default_filetype)
data: t.Any = self._parsers.parse_data_with_path(
file, file.name, self._default_filetype
)
except ParseError as err:
data = err
else:
data = self._data_transform(data)
# yield (path, data)
yield (fn.name, data)
yield (file.name, data)
13 changes: 5 additions & 8 deletions src/check_jsonschema/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import pathlib
import typing as t
from io import TextIOWrapper

import ruamel.yaml

Expand Down Expand Up @@ -85,8 +84,7 @@ def get(
)

def parse_data_with_path(
# self, data: t.BinaryIO | bytes, path: pathlib.Path | str, default_filetype: str
self, data: t.TextIO, path: pathlib.Path | str, default_filetype: str
self, data: t.BinaryIO | bytes, path: pathlib.Path | str, default_filetype: str
) -> t.Any:
loadfunc = self.get(path, default_filetype)
try:
Expand All @@ -96,8 +94,7 @@ def parse_data_with_path(
except LOADING_FAILURE_ERROR_TYPES as e:
raise FailedFileLoadError(f"Failed to parse {path}") from e

# def parse_file(self, path: pathlib.Path | str, default_filetype: str) -> t.Any:
def parse_file(self, path: TextIOWrapper, default_filetype: str) -> t.Any:
# with open(path, "rb") as fp:
# return self.parse_data_with_path(fp, path, default_filetype)
return self.parse_data_with_path(path, path.name, default_filetype)
# click gives file objects, why is this opening files from paths?
def parse_file(self, path: pathlib.Path | str, default_filetype: str) -> t.Any:
with open(path, "rb") as fp:
return self.parse_data_with_path(fp, path, default_filetype)
8 changes: 3 additions & 5 deletions src/check_jsonschema/result.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import pathlib

import jsonschema

from .parsers import ParseError
Expand All @@ -17,18 +15,18 @@ def __init__(self) -> None:
def success(self) -> bool:
return not (bool(self.parse_errors) or bool(self.validation_errors))

def record_validation_success(self, path: pathlib.Path) -> None:
def record_validation_success(self, path: str) -> None:
self.successes.append(str(path))

def record_validation_error(
self, path: pathlib.Path, err: jsonschema.ValidationError
self, path: str, err: jsonschema.ValidationError
) -> None:
filename = str(path)
if filename not in self.validation_errors:
self.validation_errors[filename] = []
self.validation_errors[filename].append(err)

def record_parse_error(self, path: pathlib.Path, err: ParseError) -> None:
def record_parse_error(self, path: str, err: ParseError) -> None:
filename = str(path)
if filename not in self.parse_errors:
self.parse_errors[filename] = []
Expand Down
7 changes: 3 additions & 4 deletions src/check_jsonschema/schema_loader/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import pathlib
import typing as t
import urllib.error
import urllib.parse
Expand Down Expand Up @@ -47,7 +46,7 @@ def set_defaults_then_validate(
class SchemaLoaderBase:
def get_validator(
self,
path: pathlib.Path,
path: str,
instance_doc: dict[str, t.Any],
format_opts: FormatOptions,
fill_defaults: bool,
Expand Down Expand Up @@ -117,7 +116,7 @@ def get_schema(self) -> dict[str, t.Any]:

def get_validator(
self,
path: pathlib.Path,
path: str,
instance_doc: dict[str, t.Any],
format_opts: FormatOptions,
fill_defaults: bool,
Expand Down Expand Up @@ -189,7 +188,7 @@ def __init__(self, base_uri: str | None = None) -> None:

def get_validator(
self,
path: pathlib.Path,
path: str,
instance_doc: dict[str, t.Any],
format_opts: FormatOptions,
fill_defaults: bool,
Expand Down

0 comments on commit b6489a0

Please sign in to comment.