Skip to content

Update pre-commit hooks #407

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
39 changes: 12 additions & 27 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
default_stages: [commit, push]
default_stages: [pre-commit, pre-push]
default_language_version:
# force all unspecified python hooks to run python3
python: python3
Expand All @@ -10,37 +10,22 @@ repos:
- id: check-hooks-apply

- repo: https://github.com/asottile/pyupgrade
rev: v2.19.0
rev: v3.20.0
hooks:
- id: pyupgrade
args: ["--py36-plus"]
args: ["--py39-plus"]

- repo: local
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.1.0
hooks:
- id: flynt
name: Convert to f-strings with flynt
entry: flynt
language: python
additional_dependencies: ['flynt==0.76']

- id: black
name: black
entry: black
language: system
require_serial: true
types: [python]

- repo: https://github.com/pycqa/isort
rev: 6.0.1
hooks:
- id: isort
name: isort
entry: isort
args: ['--filter-files']
language: system
require_serial: true
types: [python]

- id: pyflakes
name: pyflakes
entry: pyflakes
language: system
require_serial: true
types: [python]
- repo: https://github.com/pycqa/flake8
rev: 7.3.0
hooks:
- id: flake8
1 change: 1 addition & 0 deletions openapi_spec_validator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""OpenAPI spec validator module."""

from openapi_spec_validator.shortcuts import validate
from openapi_spec_validator.shortcuts import validate_spec
from openapi_spec_validator.shortcuts import validate_spec_url
Expand Down
2 changes: 1 addition & 1 deletion openapi_spec_validator/__main__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging
import sys
from argparse import ArgumentParser
from collections.abc import Sequence
from typing import Optional
from typing import Sequence

from jsonschema.exceptions import ValidationError
from jsonschema.exceptions import best_match
Expand Down
5 changes: 2 additions & 3 deletions openapi_spec_validator/readers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import sys
from os import path
from pathlib import Path
from typing import Tuple

from jsonschema_path.handlers import all_urls_handler
from jsonschema_path.handlers import file_handler
from jsonschema_path.typing import Schema


def read_from_stdin(filename: str) -> Tuple[Schema, str]:
def read_from_stdin(filename: str) -> tuple[Schema, str]:
return file_handler(sys.stdin), "" # type: ignore


def read_from_filename(filename: str) -> Tuple[Schema, str]:
def read_from_filename(filename: str) -> tuple[Schema, str]:
if not path.isfile(filename):
raise OSError(f"No such file: {filename}")

Expand Down
1 change: 1 addition & 0 deletions openapi_spec_validator/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""OpenAIP spec validator schemas module."""

from functools import partial

from jsonschema.validators import Draft4Validator
Expand Down
4 changes: 2 additions & 2 deletions openapi_spec_validator/schemas/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""OpenAIP spec validator schemas utils module."""

from importlib.resources import as_file
from importlib.resources import files
from os import path
from typing import Tuple

from jsonschema_path.readers import FilePathReader
from jsonschema_path.typing import Schema


def get_schema(version: str) -> Tuple[Schema, str]:
def get_schema(version: str) -> tuple[Schema, str]:
schema_path = f"resources/schemas/v{version}/schema.json"
ref = files("openapi_spec_validator") / schema_path
with as_file(ref) as resource_path:
Expand Down
8 changes: 4 additions & 4 deletions openapi_spec_validator/shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""OpenAPI spec validator shortcuts module."""

import warnings
from typing import Mapping
from collections.abc import Mapping
from typing import Optional
from typing import Type

from jsonschema_path import SchemaPath
from jsonschema_path.handlers import all_urls_handler
Expand Down Expand Up @@ -50,7 +50,7 @@ def validate(

def validate_url(
spec_url: str,
cls: Optional[Type[SpecValidator]] = None,
cls: Optional[type[SpecValidator]] = None,
) -> None:
spec = all_urls_handler(spec_url)
return validate(spec, base_uri=spec_url, cls=cls)
Expand Down Expand Up @@ -82,7 +82,7 @@ def validate_spec(
def validate_spec_url(
spec_url: str,
validator: Optional[SupportsValidation] = None,
cls: Optional[Type[SpecValidator]] = None,
cls: Optional[type[SpecValidator]] = None,
) -> None:
warnings.warn(
"validate_spec_url shortcut is deprecated. Use validate_url instead.",
Expand Down
7 changes: 3 additions & 4 deletions openapi_spec_validator/validation/caches.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections.abc import Iterable
from collections.abc import Iterator
from typing import Generic
from typing import Iterable
from typing import Iterator
from typing import List
from typing import TypeVar

T = TypeVar("T")
Expand All @@ -14,7 +13,7 @@ class CachedIterable(Iterable[T], Generic[T]):
It should not be iterated by his own.
"""

cache: List[T]
cache: list[T]
iter: Iterator[T]
completed: bool

Expand Down
11 changes: 6 additions & 5 deletions openapi_spec_validator/validation/decorators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""OpenAPI spec validator validation decorators module."""

import logging
from collections.abc import Iterable
from collections.abc import Iterator
from functools import wraps
from typing import Any
from typing import Callable
from typing import Iterable
from typing import Iterator
from typing import TypeVar

from jsonschema.exceptions import ValidationError
Expand All @@ -19,7 +20,7 @@


def wraps_errors(
func: Callable[..., Any]
func: Callable[..., Any],
) -> Callable[..., Iterator[ValidationError]]:
@wraps(func)
def wrapper(*args: Any, **kwds: Any) -> Iterator[ValidationError]:
Expand All @@ -35,7 +36,7 @@ def wrapper(*args: Any, **kwds: Any) -> Iterator[ValidationError]:


def wraps_cached_iter(
func: Callable[[Args], Iterator[T]]
func: Callable[[Args], Iterator[T]],
) -> Callable[[Args], CachedIterable[T]]:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> CachedIterable[T]:
Expand All @@ -46,7 +47,7 @@ def wrapper(*args: Any, **kwargs: Any) -> CachedIterable[T]:


def unwraps_iter(
func: Callable[[Args], Iterable[T]]
func: Callable[[Args], Iterable[T]],
) -> Callable[[Args], Iterator[T]]:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Iterator[T]:
Expand Down
25 changes: 14 additions & 11 deletions openapi_spec_validator/validation/keywords.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import string
from collections.abc import Iterator
from collections.abc import Sequence
from typing import TYPE_CHECKING
from typing import Any
from typing import Sequence
from typing import Iterator
from typing import List
from typing import Optional
from typing import cast

Expand Down Expand Up @@ -68,7 +67,7 @@ class SchemaValidator(KeywordValidator):
def __init__(self, registry: "KeywordValidatorRegistry"):
super().__init__(registry)

self.schema_ids_registry: Optional[List[int]] = []
self.schema_ids_registry: Optional[list[int]] = []

@property
def default_validator(self) -> ValueValidator:
Expand Down Expand Up @@ -113,8 +112,9 @@ def __call__(
all_of = schema / "allOf"
for inner_schema in all_of:
yield from self(inner_schema, require_properties=False)
nested_properties += list(self._collect_properties(inner_schema))

nested_properties += list(
self._collect_properties(inner_schema)
)

if "anyOf" in schema:
any_of = schema / "anyOf"
Expand Down Expand Up @@ -154,8 +154,12 @@ def __call__(
require_properties=False,
)

required = "required" in schema and (schema / "required").read_value() or []
properties = "properties" in schema and (schema / "properties").keys() or []
required = (
"required" in schema and (schema / "required").read_value() or []
)
properties = (
"properties" in schema and (schema / "properties").keys() or []
)
if "allOf" in schema:
extra_properties = list(
set(required) - set(properties) - set(nested_properties)
Expand Down Expand Up @@ -305,7 +309,7 @@ class OperationValidator(KeywordValidator):
def __init__(self, registry: "KeywordValidatorRegistry"):
super().__init__(registry)

self.operation_ids_registry: Optional[List[str]] = []
self.operation_ids_registry: Optional[list[str]] = []

@property
def responses_validator(self) -> ResponsesValidator:
Expand Down Expand Up @@ -356,8 +360,7 @@ def __call__(
for path in self._get_path_params_from_url(url):
if path not in all_params:
yield UnresolvableParameterError(
"Path parameter '{}' for '{}' operation in '{}' "
"was not resolved".format(path, name, url)
f"Path parameter '{path}' for '{name}' operation in '{url}' was not resolved"
)
return

Expand Down
11 changes: 4 additions & 7 deletions openapi_spec_validator/validation/protocols.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterator
from collections.abc import Iterator
from typing import Optional
from typing import Protocol
from typing import runtime_checkable
Expand All @@ -10,21 +10,18 @@

@runtime_checkable
class SupportsValidation(Protocol):
def is_valid(self, instance: Schema) -> bool:
...
def is_valid(self, instance: Schema) -> bool: ...

def iter_errors(
self,
instance: Schema,
base_uri: str = "",
spec_url: Optional[str] = None,
) -> Iterator[OpenAPIValidationError]:
...
) -> Iterator[OpenAPIValidationError]: ...

def validate(
self,
instance: Schema,
base_uri: str = "",
spec_url: Optional[str] = None,
) -> None:
...
) -> None: ...
10 changes: 4 additions & 6 deletions openapi_spec_validator/validation/proxies.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""OpenAPI spec validator validation proxies module."""

import warnings
from collections.abc import Hashable
from typing import Any
from typing import Iterator
from typing import Mapping
from collections.abc import Iterator
from collections.abc import Mapping
from typing import Optional
from typing import Tuple

from jsonschema.exceptions import ValidationError
from jsonschema_path.typing import Schema
Expand Down Expand Up @@ -59,7 +57,7 @@ def iter_errors(


class DetectValidatorProxy:
def __init__(self, choices: Mapping[Tuple[str, str], SpecValidatorProxy]):
def __init__(self, choices: Mapping[tuple[str, str], SpecValidatorProxy]):
self.choices = choices

def detect(self, instance: Schema) -> SpecValidatorProxy:
Expand Down
5 changes: 2 additions & 3 deletions openapi_spec_validator/validation/registries.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import annotations

from collections.abc import Mapping
from typing import DefaultDict
from typing import Mapping
from typing import Type

from openapi_spec_validator.validation.keywords import KeywordValidator


class KeywordValidatorRegistry(DefaultDict[str, KeywordValidator]):
def __init__(
self, keyword_validators: Mapping[str, Type[KeywordValidator]]
self, keyword_validators: Mapping[str, type[KeywordValidator]]
):
super().__init__()
self.keyword_validators = keyword_validators
Expand Down
4 changes: 1 addition & 3 deletions openapi_spec_validator/validation/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Type

from openapi_spec_validator.validation.validators import SpecValidator

SpecValidatorType = Type[SpecValidator]
SpecValidatorType = type[SpecValidator]
11 changes: 5 additions & 6 deletions openapi_spec_validator/validation/validators.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""OpenAPI spec validator validation validators module."""

import logging
import warnings
from collections.abc import Iterator
from collections.abc import Mapping
from functools import lru_cache
from typing import Iterator
from typing import List
from typing import Mapping
from typing import Optional
from typing import Type
from typing import cast

from jsonschema.exceptions import ValidationError
Expand All @@ -31,10 +30,10 @@

class SpecValidator:
resolver_handlers = default_handlers
keyword_validators: Mapping[str, Type[keywords.KeywordValidator]] = {
keyword_validators: Mapping[str, type[keywords.KeywordValidator]] = {
"__root__": keywords.RootValidator,
}
root_keywords: List[str] = []
root_keywords: list[str] = []
schema_validator: Validator = NotImplemented

def __init__(
Expand Down
4 changes: 1 addition & 3 deletions openapi_spec_validator/versions/consts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List

from openapi_spec_validator.versions.datatypes import SpecVersion

OPENAPIV2 = SpecVersion(
Expand All @@ -20,4 +18,4 @@
minor="1",
)

VERSIONS: List[SpecVersion] = [OPENAPIV2, OPENAPIV30, OPENAPIV31]
VERSIONS: list[SpecVersion] = [OPENAPIV2, OPENAPIV30, OPENAPIV31]
Loading
Loading