Skip to content

Commit

Permalink
Add type hints to the new files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarvaze committed Oct 17, 2019
1 parent dc87877 commit 0cce0a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
30 changes: 16 additions & 14 deletions pyramid_openapi3/check_openapi_responses.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Verify that endpoints have defined required responses."""

from .exceptions import ResponseValidationError
from openapi_core.schema.specs.models import Spec
from pathlib import Path
from pyramid.config import Configurator

import openapi_core
import typing as t
import yaml


def get_config(config_path: str) -> t.Dict:
def get_config(config_path: str) -> t.Dict[str, str]:
"""Read config from file."""
config = Path(config_path)

Expand All @@ -19,7 +21,7 @@ def get_config(config_path: str) -> t.Dict:
return yaml.safe_load(f)


def get_spec(schema_path: str) -> t.Any:
def get_spec(schema_path: str) -> Spec:
"""Create openapi spec from schema."""
schema = Path(schema_path)

Expand Down Expand Up @@ -47,30 +49,30 @@ def required_responses(
return required_resp


def validate_required_responses(spec_file, config: t.Dict) -> None:
def validate_required_responses(spec_file: str, config: Configurator) -> None:
"""Verify that all endpoints have defined required responses."""
check_failed = False
missing_responses_count = 0
errors = []
check_failed: bool = False
missing_responses_count: int = 0
errors: t.List = []

filepath = config.registry.settings.get("pyramid_openapi3_responses_config")
filepath: str = config.registry.settings.get("pyramid_openapi3_responses_config")
if not filepath:
return

responses_config = get_config(filepath)
responses_config: t.Dict[str, str] = get_config(filepath)

spec = get_spec(spec_file)
spec: Spec = get_spec(spec_file)
for path in spec.paths.values():
for operation in path.operations.values():
operation_responses = operation.responses.keys()
method = operation.http_method
endpoint = operation.path_name
has_params = len(operation.parameters) > 0
required = required_responses(
method: str = operation.http_method
endpoint: str = operation.path_name
has_params: bool = len(operation.parameters) > 0
required: t.Set = required_responses(
responses_config, endpoint, method, has_params
)

missing_responses = required - operation_responses
missing_responses: t.Set = required - operation_responses
for missing_response in missing_responses:
check_failed = True
missing_responses_count += 1
Expand Down
10 changes: 5 additions & 5 deletions pyramid_openapi3/tests/test_openapi_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@
"""


def test_get_config_no_file():
def test_get_config_no_file() -> None:
"""Test get_config when config is not a file object."""
from pyramid_openapi3.check_openapi_responses import get_config

with pytest.raises(Exception):
get_config("file_does_not_exist")


def test_get_spec_no_file():
def test_get_spec_no_file() -> None:
"""Test get_spec when config is not a file object."""
from pyramid_openapi3.check_openapi_responses import get_spec

with pytest.raises(Exception):
get_spec("file_does_not_exist")


def test_response_validation_error():
def test_response_validation_error() -> None:
"""Test that ResponseValidationError is raised when 404 is missing."""

SPEC_DOCUMENT = b"""
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_response_validation_error():
validate_required_responses(spec.name, config)


def test_happy_path():
def test_happy_path() -> None:
"""Test validation of openapi responses."""

SPEC_DOCUMENT = b"""
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_happy_path():
validate_required_responses(spec.name, config)


def test_no_params():
def test_no_params() -> None:
"""Test spec without parameters."""

SPEC_DOCUMENT = b"""
Expand Down

0 comments on commit 0cce0a6

Please sign in to comment.