Skip to content

Commit 48bf5e3

Browse files
committed
Mypy static type check
1 parent 6d78075 commit 48bf5e3

File tree

19 files changed

+668
-450
lines changed

19 files changed

+668
-450
lines changed

.github/workflows/python-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ jobs:
5757
PYTEST_ADDOPTS: "--color=yes"
5858
run: poetry run pytest
5959

60+
- name: Static type check
61+
run: poetry run mypy
62+
6063
- name: Upload coverage
6164
uses: codecov/codecov-action@v1

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
include README.md
22
include requirements.txt
33
include requirements_dev.txt
4+
include openapi_spec_validator/py.typed
45
include openapi_spec_validator/resources/schemas/*/*
56
include LICENSE

openapi_spec_validator/__init__.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
1-
# -*- coding: utf-8 -*-
21
from jsonschema_spec.handlers import default_handlers
32

43
from openapi_spec_validator.shortcuts import validate_spec_detect_factory
5-
from openapi_spec_validator.shortcuts import validate_spec_url_detect_factory
64
from openapi_spec_validator.shortcuts import validate_spec_factory
5+
from openapi_spec_validator.shortcuts import validate_spec_url_detect_factory
76
from openapi_spec_validator.shortcuts import validate_spec_url_factory
87
from openapi_spec_validator.validation import openapi_v2_spec_validator
98
from openapi_spec_validator.validation import openapi_v3_spec_validator
109
from openapi_spec_validator.validation import openapi_v30_spec_validator
1110
from openapi_spec_validator.validation import openapi_v31_spec_validator
1211

13-
__author__ = 'Artur Maciag'
14-
__email__ = '[email protected]'
15-
__version__ = '0.5.0a3'
16-
__url__ = 'https://github.com/p1c2u/openapi-spec-validator'
17-
__license__ = 'Apache License, Version 2.0'
12+
__author__ = "Artur Maciag"
13+
__email__ = "[email protected]"
14+
__version__ = "0.5.0a3"
15+
__url__ = "https://github.com/p1c2u/openapi-spec-validator"
16+
__license__ = "Apache License, Version 2.0"
1817

1918
__all__ = [
20-
'openapi_v2_spec_validator',
21-
'openapi_v3_spec_validator',
22-
'openapi_v30_spec_validator',
23-
'openapi_v31_spec_validator',
24-
'validate_v2_spec',
25-
'validate_v3_spec',
26-
'validate_v30_spec',
27-
'validate_v31_spec',
28-
'validate_spec',
29-
'validate_v2_spec_url',
30-
'validate_v3_spec_url',
31-
'validate_v30_spec_url',
32-
'validate_v31_spec_url',
33-
'validate_spec_url',
19+
"openapi_v2_spec_validator",
20+
"openapi_v3_spec_validator",
21+
"openapi_v30_spec_validator",
22+
"openapi_v31_spec_validator",
23+
"validate_v2_spec",
24+
"validate_v3_spec",
25+
"validate_v30_spec",
26+
"validate_v31_spec",
27+
"validate_spec",
28+
"validate_v2_spec_url",
29+
"validate_v3_spec_url",
30+
"validate_v30_spec_url",
31+
"validate_v31_spec_url",
32+
"validate_spec_url",
3433
]
3534

3635
# shortcuts
37-
validate_spec = validate_spec_detect_factory({
36+
validate_spec = validate_spec_detect_factory(
37+
{
3838
("swagger", "2.0"): openapi_v2_spec_validator,
3939
("openapi", "3.0"): openapi_v30_spec_validator,
4040
("openapi", "3.1"): openapi_v31_spec_validator,
4141
},
4242
)
43-
validate_spec_url = validate_spec_url_detect_factory({
43+
validate_spec_url = validate_spec_url_detect_factory(
44+
{
4445
("swagger", "2.0"): openapi_v2_spec_validator,
4546
("openapi", "3.0"): openapi_v30_spec_validator,
4647
("openapi", "3.1"): openapi_v31_spec_validator,
4748
},
4849
)
4950
validate_v2_spec = validate_spec_factory(openapi_v2_spec_validator)
50-
validate_v2_spec_url = validate_spec_url_factory(
51-
openapi_v2_spec_validator)
51+
validate_v2_spec_url = validate_spec_url_factory(openapi_v2_spec_validator)
5252

5353
validate_v30_spec = validate_spec_factory(openapi_v30_spec_validator)
54-
validate_v30_spec_url = validate_spec_url_factory(
55-
openapi_v30_spec_validator)
54+
validate_v30_spec_url = validate_spec_url_factory(openapi_v30_spec_validator)
5655

5756
validate_v31_spec = validate_spec_factory(openapi_v31_spec_validator)
58-
validate_v31_spec_url = validate_spec_url_factory(
59-
openapi_v31_spec_validator)
57+
validate_v31_spec_url = validate_spec_url_factory(openapi_v31_spec_validator)
6058

6159
# aliases to the latest v3 version
6260
validate_v3_spec = validate_v31_spec

openapi_spec_validator/__main__.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import logging
21
import argparse
2+
import logging
33
import sys
44

55
from jsonschema.exceptions import best_match
66

7-
from openapi_spec_validator import (
8-
openapi_v2_spec_validator,
9-
openapi_v30_spec_validator,
10-
openapi_v31_spec_validator,
11-
)
7+
from openapi_spec_validator import openapi_v2_spec_validator
8+
from openapi_spec_validator import openapi_v30_spec_validator
9+
from openapi_spec_validator import openapi_v31_spec_validator
10+
from openapi_spec_validator.readers import read_from_filename
11+
from openapi_spec_validator.readers import read_from_stdin
1212
from openapi_spec_validator.validation.exceptions import ValidationError
13-
from openapi_spec_validator.readers import read_from_stdin, read_from_filename
1413

1514
logger = logging.getLogger(__name__)
1615
logging.basicConfig(
17-
format='%(asctime)s %(levelname)s %(name)s %(message)s',
18-
level=logging.WARNING
16+
format="%(asctime)s %(levelname)s %(name)s %(message)s",
17+
level=logging.WARNING,
1918
)
2019

2120

@@ -35,14 +34,14 @@ def print_validationerror(exc, errors="best-match"):
3534
print("## " + str(best_match(exc.context)))
3635
if len(exc.context) > 1:
3736
print(
38-
"\n({} more subschemas errors,".format(len(exc.context) - 1),
37+
f"\n({len(exc.context) - 1} more subschemas errors,",
3938
"use --errors=all to see them.)",
4039
)
4140

4241

4342
def main(args=None):
4443
parser = argparse.ArgumentParser()
45-
parser.add_argument('filename', help="Absolute or relative path to file")
44+
parser.add_argument("filename", help="Absolute or relative path to file")
4645
parser.add_argument(
4746
"--errors",
4847
choices=("best-match", "all"),
@@ -51,17 +50,17 @@ def main(args=None):
5150
"""use "all" to get all subschema errors.""",
5251
)
5352
parser.add_argument(
54-
'--schema',
53+
"--schema",
5554
help="OpenAPI schema (default: 3.1.0)",
5655
type=str,
57-
choices=['2.0', '3.0.0', '3.1.0'],
58-
default='3.1.0'
56+
choices=["2.0", "3.0.0", "3.1.0"],
57+
default="3.1.0",
5958
)
6059
args = parser.parse_args(args)
6160

6261
# choose source
6362
reader = read_from_filename
64-
if args.filename in ['-', '/-']:
63+
if args.filename in ["-", "/-"]:
6564
reader = read_from_stdin
6665

6766
# read source
@@ -73,9 +72,9 @@ def main(args=None):
7372

7473
# choose the validator
7574
validators = {
76-
'2.0': openapi_v2_spec_validator,
77-
'3.0.0': openapi_v30_spec_validator,
78-
'3.1.0': openapi_v31_spec_validator,
75+
"2.0": openapi_v2_spec_validator,
76+
"3.0.0": openapi_v30_spec_validator,
77+
"3.1.0": openapi_v31_spec_validator,
7978
}
8079
validator = validators[args.schema]
8180

@@ -89,8 +88,8 @@ def main(args=None):
8988
print(exc)
9089
sys.exit(2)
9190
else:
92-
print('OK')
91+
print("OK")
9392

9493

95-
if __name__ == '__main__':
94+
if __name__ == "__main__":
9695
main()

openapi_spec_validator/py.typed

Whitespace-only changes.

openapi_spec_validator/readers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
import pathlib
33
import sys
44

5-
from jsonschema_spec.handlers import file_handler, all_urls_handler
5+
from jsonschema_spec.handlers import all_urls_handler
6+
from jsonschema_spec.handlers import file_handler
67

78

89
def read_from_stdin(filename):
9-
return file_handler(sys.stdin), ''
10+
return file_handler(sys.stdin), ""
1011

1112

1213
def read_from_filename(filename):
1314
if not os.path.isfile(filename):
14-
raise IOError("No such file: {0}".format(filename))
15+
raise OSError(f"No such file: {filename}")
1516

1617
filename = os.path.abspath(filename)
1718
uri = pathlib.Path(filename).as_uri()

openapi_spec_validator/schemas/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
__all__ = ["schema_v2", "schema_v3", "schema_v30", "schema_v31"]
55

6-
schema_v2, _ = get_schema('2.0')
7-
schema_v30, _ = get_schema('3.0')
8-
schema_v31, _ = get_schema('3.1')
6+
schema_v2, _ = get_schema("2.0")
7+
schema_v30, _ = get_schema("3.0")
8+
schema_v31, _ = get_schema("3.1")
99

1010
# alias to the latest v3 version
1111
schema_v3 = schema_v31

openapi_spec_validator/schemas/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
from os import path
33

44
import importlib_resources
5-
65
from jsonschema_spec.readers import FilePathReader
76

87

98
def get_schema(version):
10-
schema_path = 'resources/schemas/v{0}/schema.json'.format(version)
11-
ref = importlib_resources.files('openapi_spec_validator') / schema_path
9+
schema_path = f"resources/schemas/v{version}/schema.json"
10+
ref = importlib_resources.files("openapi_spec_validator") / schema_path
1211
with importlib_resources.as_file(ref) as resource_path:
1312
schema_path_full = path.join(path.dirname(__file__), resource_path)
1413
return FilePathReader(schema_path_full).read()

openapi_spec_validator/shortcuts.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ def detect_validator(choices, spec):
1414

1515

1616
def validate_spec_detect_factory(choices):
17-
def validate(spec, spec_url=''):
17+
def validate(spec, spec_url=""):
1818
validator_class = detect_validator(choices, spec)
1919
return validator_class.validate(spec, spec_url=spec_url)
20+
2021
return validate
2122

2223

2324
def validate_spec_factory(validator_class):
24-
def validate(spec, spec_url=''):
25+
def validate(spec, spec_url=""):
2526
return validator_class.validate(spec, spec_url=spec_url)
27+
2628
return validate
2729

2830

@@ -31,11 +33,13 @@ def validate(spec_url):
3133
spec = all_urls_handler(spec_url)
3234
validator_class = detect_validator(choices, spec)
3335
return validator_class.validate(spec, spec_url=spec_url)
36+
3437
return validate
3538

3639

3740
def validate_spec_url_factory(validator_class):
3841
def validate(spec_url):
3942
spec = all_urls_handler(spec_url)
4043
return validator_class.validate(spec, spec_url=spec_url)
44+
4145
return validate

openapi_spec_validator/validation/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# -*- coding: utf-8 -*-
2-
from jsonschema.validators import Draft202012Validator
31
from jsonschema.validators import Draft4Validator
2+
from jsonschema.validators import Draft202012Validator
43
from jsonschema_spec.handlers import default_handlers
54
from openapi_schema_validator import oas30_format_checker
65
from openapi_schema_validator import oas31_format_checker
@@ -13,30 +12,36 @@
1312
from openapi_spec_validator.validation.validators import SpecValidator
1413

1514
__all__ = [
16-
'openapi_v2_spec_validator',
17-
'openapi_v3_spec_validator',
18-
'openapi_v30_spec_validator',
19-
'openapi_v31_spec_validator',
15+
"openapi_v2_spec_validator",
16+
"openapi_v3_spec_validator",
17+
"openapi_v30_spec_validator",
18+
"openapi_v31_spec_validator",
2019
]
2120

2221
# v2.0 spec
2322
openapi_v2_schema_validator = Draft4Validator(schema_v2)
2423
openapi_v2_spec_validator = SpecValidator(
25-
openapi_v2_schema_validator, OAS30Validator, oas30_format_checker,
24+
openapi_v2_schema_validator,
25+
OAS30Validator,
26+
oas30_format_checker,
2627
resolver_handlers=default_handlers,
2728
)
2829

2930
# v3.0 spec
3031
openapi_v30_schema_validator = Draft4Validator(schema_v30)
3132
openapi_v30_spec_validator = SpecValidator(
32-
openapi_v30_schema_validator, OAS30Validator, oas30_format_checker,
33+
openapi_v30_schema_validator,
34+
OAS30Validator,
35+
oas30_format_checker,
3336
resolver_handlers=default_handlers,
3437
)
3538

3639
# v3.1 spec
3740
openapi_v31_schema_validator = Draft202012Validator(schema_v31)
3841
openapi_v31_spec_validator = SpecValidator(
39-
openapi_v31_schema_validator, OAS31Validator, oas31_format_checker,
42+
openapi_v31_schema_validator,
43+
OAS31Validator,
44+
oas31_format_checker,
4045
resolver_handlers=default_handlers,
4146
)
4247

openapi_spec_validator/validation/decorators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""OpenAPI spec validator validation decorators module."""
2-
from functools import wraps
32
import logging
3+
from functools import wraps
44

55
log = logging.getLogger(__name__)
66

77

8-
class ValidationErrorWrapper(object):
9-
8+
class ValidationErrorWrapper:
109
def __init__(self, error_class):
1110
self.error_class = error_class
1211

@@ -20,4 +19,5 @@ def wrapper(*args, **kwds):
2019
yield self.error_class.create_from(err)
2120
else:
2221
yield err
22+
2323
return wrapper

0 commit comments

Comments
 (0)