diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d38a5315e..718191e71 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -27,14 +27,11 @@ repos: rev: 25.1.0 hooks: - id: black -- repo: https://github.com/PyCQA/flake8 - rev: 7.2.0 +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.5.5 hooks: - - id: flake8 - additional_dependencies: - - 'flake8-bugbear==24.4.26' - - 'flake8-typing-as-t==0.0.3' - - 'flake8-comprehensions==3.15.0' + - id: ruff + args: ["--fix"] - repo: https://github.com/sirosen/slyp rev: 0.8.2 hooks: diff --git a/pyproject.toml b/pyproject.toml index deb45caa5..1f9925db0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,13 @@ namespaces = false [tool.isort] profile = "black" +[tool.ruff] +select = [ + "B", # flake8-bugbear + "C4", # flake8-comprehensions + "TYP", # flake8-typing-as-t +] + [tool.mypy] # strict = true # TODO: enable disallow_untyped_defs = true diff --git a/src/check_jsonschema/builtin_schemas/__init__.py b/src/check_jsonschema/builtin_schemas/__init__.py index e9a89e02b..780268951 100644 --- a/src/check_jsonschema/builtin_schemas/__init__.py +++ b/src/check_jsonschema/builtin_schemas/__init__.py @@ -17,8 +17,8 @@ def _get(package: str, resource: str, name: str) -> dict[str, t.Any]: importlib.resources.files(package).joinpath(resource).read_bytes() ), ) - except (FileNotFoundError, ModuleNotFoundError): - raise NoSuchSchemaError(f"no builtin schema named {name} was found") + except (FileNotFoundError, ModuleNotFoundError) as err: + raise NoSuchSchemaError(f"no builtin schema named {name} was found") from err def _get_vendored_schema(name: str) -> dict[str, t.Any]: diff --git a/src/check_jsonschema/checker.py b/src/check_jsonschema/checker.py index c6cd852eb..974fe69e3 100644 --- a/src/check_jsonschema/checker.py +++ b/src/check_jsonschema/checker.py @@ -1,6 +1,5 @@ from __future__ import annotations -import pathlib import typing as t import click @@ -8,11 +7,7 @@ import referencing.exceptions from . import utils -from .formats import FormatOptions -from .instance_loader import InstanceLoader from .parsers import ParseError -from .regex_variants import RegexImplementation -from .reporter import Reporter from .result import CheckResult from .schema_loader import SchemaLoaderBase, SchemaParseError, UnsupportedUrlScheme @@ -22,6 +17,15 @@ def __init__(self, code: int) -> None: self.code = code +if t.TYPE_CHECKING: + import pathlib + + from .formats import FormatOptions + from .instance_loader import InstanceLoader + from .regex_variants import RegexImplementation + from .reporter import Reporter + + class SchemaChecker: def __init__( self, diff --git a/src/check_jsonschema/cli/main_command.py b/src/check_jsonschema/cli/main_command.py index 9e93ff1ff..8ba530e8f 100644 --- a/src/check_jsonschema/cli/main_command.py +++ b/src/check_jsonschema/cli/main_command.py @@ -5,7 +5,7 @@ import typing as t import click -import jsonschema +import jsonschema # noqa: TCH002 from ..catalog import CUSTOM_SCHEMA_NAMES, SCHEMA_CATALOG from ..checker import SchemaChecker diff --git a/src/check_jsonschema/cli/parse_result.py b/src/check_jsonschema/cli/parse_result.py index bfd9065b1..4d12eec1c 100644 --- a/src/check_jsonschema/cli/parse_result.py +++ b/src/check_jsonschema/cli/parse_result.py @@ -4,11 +4,14 @@ import typing as t import click -import jsonschema from ..formats import FormatOptions from ..regex_variants import RegexImplementation, RegexVariantName -from ..transforms import Transform + +if t.TYPE_CHECKING: + import jsonschema + + from ..transforms import Transform class SchemaLoadingMode(enum.Enum): diff --git a/src/check_jsonschema/cli/warnings.py b/src/check_jsonschema/cli/warnings.py index 8ad61526f..57e9ca28a 100644 --- a/src/check_jsonschema/cli/warnings.py +++ b/src/check_jsonschema/cli/warnings.py @@ -3,7 +3,8 @@ import typing as t import warnings -import click +if t.TYPE_CHECKING: + import click def deprecation_warning_callback( diff --git a/src/check_jsonschema/formats/__init__.py b/src/check_jsonschema/formats/__init__.py index 2308c4313..8556def62 100644 --- a/src/check_jsonschema/formats/__init__.py +++ b/src/check_jsonschema/formats/__init__.py @@ -1,13 +1,16 @@ from __future__ import annotations import copy +import typing import jsonschema import jsonschema.validators -from ..regex_variants import RegexImplementation from .implementations import validate_rfc3339, validate_time +if typing.TYPE_CHECKING: + from ..regex_variants import RegexImplementation + # all known format strings except for a selection from draft3 which have either # been renamed or removed: # - color diff --git a/src/check_jsonschema/identify_filetype.py b/src/check_jsonschema/identify_filetype.py index b2b9dc390..99d574934 100644 --- a/src/check_jsonschema/identify_filetype.py +++ b/src/check_jsonschema/identify_filetype.py @@ -4,7 +4,10 @@ from __future__ import annotations -import pathlib +import typing + +if typing.TYPE_CHECKING: + import pathlib _EXTENSION_MAP = { "json": "json", diff --git a/src/check_jsonschema/parsers/__init__.py b/src/check_jsonschema/parsers/__init__.py index 6db5e95bf..e570a9074 100644 --- a/src/check_jsonschema/parsers/__init__.py +++ b/src/check_jsonschema/parsers/__init__.py @@ -1,14 +1,16 @@ from __future__ import annotations import io -import pathlib import typing as t -import ruamel.yaml - from ..identify_filetype import path_to_type from . import json5, json_, toml, yaml +if t.TYPE_CHECKING: + import pathlib + + import ruamel.yaml + _PARSER_ERRORS: set[type[Exception]] = { json_.JSONDecodeError, yaml.ParseError, diff --git a/src/check_jsonschema/reporter.py b/src/check_jsonschema/reporter.py index 48663002c..27d12dcd6 100644 --- a/src/check_jsonschema/reporter.py +++ b/src/check_jsonschema/reporter.py @@ -13,10 +13,12 @@ import click import jsonschema -from .parsers import ParseError -from .result import CheckResult from .utils import format_error, iter_validation_error +if t.TYPE_CHECKING: + from .parsers import ParseError + from .result import CheckResult + class Reporter(abc.ABC): def __init__(self, *, verbosity: int, **kwargs: t.Any) -> None: diff --git a/src/check_jsonschema/result.py b/src/check_jsonschema/result.py index 00e4301e3..1ad6e129c 100644 --- a/src/check_jsonschema/result.py +++ b/src/check_jsonschema/result.py @@ -1,10 +1,13 @@ from __future__ import annotations -import pathlib +import typing -import jsonschema +if typing.TYPE_CHECKING: + import pathlib -from .parsers import ParseError + import jsonschema + + from .parsers import ParseError class CheckResult: diff --git a/src/check_jsonschema/schema_loader/main.py b/src/check_jsonschema/schema_loader/main.py index e056389a9..b817b8eb0 100644 --- a/src/check_jsonschema/schema_loader/main.py +++ b/src/check_jsonschema/schema_loader/main.py @@ -1,7 +1,6 @@ from __future__ import annotations import functools -import pathlib import typing as t import urllib.error import urllib.parse @@ -11,12 +10,16 @@ from ..builtin_schemas import get_builtin_schema from ..formats import FormatOptions, format_checker_for_regex_impl, make_format_checker from ..parsers import ParserSet -from ..regex_variants import RegexImplementation from ..utils import is_url_ish from .errors import UnsupportedUrlScheme from .readers import HttpSchemaReader, LocalSchemaReader, StdinSchemaReader from .resolver import make_reference_registry +if t.TYPE_CHECKING: + import pathlib + + from ..regex_variants import RegexImplementation + def _extend_with_default( validator_class: type[jsonschema.protocols.Validator], @@ -144,7 +147,7 @@ def get_validator( ) -> jsonschema.protocols.Validator: return self._get_validator(format_opts, regex_impl, fill_defaults) - @functools.lru_cache + @functools.lru_cache # noqa: B019 def _get_validator( self, format_opts: FormatOptions, diff --git a/src/check_jsonschema/schema_loader/resolver.py b/src/check_jsonschema/schema_loader/resolver.py index 15344d6bd..91011fc80 100644 --- a/src/check_jsonschema/schema_loader/resolver.py +++ b/src/check_jsonschema/schema_loader/resolver.py @@ -7,9 +7,11 @@ from referencing.jsonschema import DRAFT202012, Schema from ..cachedownloader import CacheDownloader -from ..parsers import ParserSet from ..utils import filename2path +if t.TYPE_CHECKING: + from ..parsers import ParserSet + def make_reference_registry( parsers: ParserSet, retrieval_uri: str | None, schema: dict, disable_cache: bool diff --git a/src/check_jsonschema/transforms/__init__.py b/src/check_jsonschema/transforms/__init__.py index b294e6005..8ab6fd5d8 100644 --- a/src/check_jsonschema/transforms/__init__.py +++ b/src/check_jsonschema/transforms/__init__.py @@ -1,7 +1,9 @@ from __future__ import annotations +import typing + from .azure_pipelines import AZURE_TRANSFORM -from .base import Transform +from .base import Transform # noqa: TCH001 from .gitlab import GITLAB_TRANSFORM TRANSFORM_LIBRARY: dict[str, Transform] = { diff --git a/src/check_jsonschema/transforms/base.py b/src/check_jsonschema/transforms/base.py index 0ed9a8647..a05c3134a 100644 --- a/src/check_jsonschema/transforms/base.py +++ b/src/check_jsonschema/transforms/base.py @@ -2,7 +2,8 @@ import typing as t -import ruamel.yaml +if t.TYPE_CHECKING: + import ruamel.yaml class Transform: diff --git a/src/check_jsonschema/transforms/gitlab.py b/src/check_jsonschema/transforms/gitlab.py index bb75ee92e..7fa3731f0 100644 --- a/src/check_jsonschema/transforms/gitlab.py +++ b/src/check_jsonschema/transforms/gitlab.py @@ -2,10 +2,11 @@ import typing as t -import ruamel.yaml - from .base import Transform +if t.TYPE_CHECKING: + import ruamel.yaml + class GitLabReferenceExpectationViolation(ValueError): def __init__(self, msg: str, data: t.Any) -> None: diff --git a/src/check_jsonschema/utils.py b/src/check_jsonschema/utils.py index e6e36f282..566559517 100644 --- a/src/check_jsonschema/utils.py +++ b/src/check_jsonschema/utils.py @@ -11,7 +11,9 @@ import urllib.request import click -import jsonschema + +if t.TYPE_CHECKING: + import jsonschema WINDOWS = os.name == "nt"