1+ from argparse import ArgumentParser
12import logging
2- import argparse
33import sys
4+ from typing import Optional
5+ from typing import Sequence
46
57from jsonschema .exceptions import best_match
8+ from jsonschema .exceptions import ValidationError
69
7- from openapi_spec_validator import (
8- openapi_v2_spec_validator ,
9- openapi_v30_spec_validator ,
10- openapi_v31_spec_validator ,
11- )
12- from openapi_spec_validator .validation .exceptions import ValidationError
13- from openapi_spec_validator .readers import read_from_stdin , read_from_filename
10+ from openapi_spec_validator import openapi_v2_spec_validator
11+ from openapi_spec_validator import openapi_v30_spec_validator
12+ from openapi_spec_validator import openapi_v31_spec_validator
13+ from openapi_spec_validator .readers import read_from_filename
14+ from openapi_spec_validator .readers import read_from_stdin
1415
1516logger = logging .getLogger (__name__ )
1617logging .basicConfig (
17- format = ' %(asctime)s %(levelname)s %(name)s %(message)s' ,
18- level = logging .WARNING
18+ format = " %(asctime)s %(levelname)s %(name)s %(message)s" ,
19+ level = logging .WARNING ,
1920)
2021
2122
22- def print_validationerror (exc , errors = "best-match" ):
23+ def print_validationerror (exc : ValidationError , errors : str = "best-match" ) -> None :
2324 print ("# Validation Error\n " )
2425 print (exc )
2526 if exc .cause :
@@ -35,14 +36,14 @@ def print_validationerror(exc, errors="best-match"):
3536 print ("## " + str (best_match (exc .context )))
3637 if len (exc .context ) > 1 :
3738 print (
38- "\n ({} more subschemas errors," . format ( len (exc .context ) - 1 ) ,
39+ f "\n ({ len (exc .context ) - 1 } more subschemas errors," ,
3940 "use --errors=all to see them.)" ,
4041 )
4142
4243
43- def main (args = None ):
44- parser = argparse . ArgumentParser ()
45- parser .add_argument (' filename' , help = "Absolute or relative path to file" )
44+ def main (args : Optional [ Sequence [ str ]] = None ) -> None :
45+ parser = ArgumentParser ()
46+ parser .add_argument (" filename" , help = "Absolute or relative path to file" )
4647 parser .add_argument (
4748 "--errors" ,
4849 choices = ("best-match" , "all" ),
@@ -51,46 +52,46 @@ def main(args=None):
5152 """use "all" to get all subschema errors.""" ,
5253 )
5354 parser .add_argument (
54- ' --schema' ,
55+ " --schema" ,
5556 help = "OpenAPI schema (default: 3.1.0)" ,
5657 type = str ,
57- choices = [' 2.0' , ' 3.0.0' , ' 3.1.0' ],
58- default = ' 3.1.0'
58+ choices = [" 2.0" , " 3.0.0" , " 3.1.0" ],
59+ default = " 3.1.0" ,
5960 )
60- args = parser .parse_args (args )
61+ args_parsed = parser .parse_args (args )
6162
6263 # choose source
6364 reader = read_from_filename
64- if args .filename in ['-' , '/-' ]:
65+ if args_parsed .filename in ["-" , "/-" ]:
6566 reader = read_from_stdin
6667
6768 # read source
6869 try :
69- spec , spec_url = reader (args .filename )
70+ spec , spec_url = reader (args_parsed .filename )
7071 except Exception as exc :
7172 print (exc )
7273 sys .exit (1 )
7374
7475 # choose the validator
7576 validators = {
76- ' 2.0' : openapi_v2_spec_validator ,
77- ' 3.0.0' : openapi_v30_spec_validator ,
78- ' 3.1.0' : openapi_v31_spec_validator ,
77+ " 2.0" : openapi_v2_spec_validator ,
78+ " 3.0.0" : openapi_v30_spec_validator ,
79+ " 3.1.0" : openapi_v31_spec_validator ,
7980 }
80- validator = validators [args .schema ]
81+ validator = validators [args_parsed .schema ]
8182
8283 # validate
8384 try :
8485 validator .validate (spec , spec_url = spec_url )
8586 except ValidationError as exc :
86- print_validationerror (exc , args .errors )
87+ print_validationerror (exc , args_parsed .errors )
8788 sys .exit (1 )
8889 except Exception as exc :
8990 print (exc )
9091 sys .exit (2 )
9192 else :
92- print ('OK' )
93+ print ("OK" )
9394
9495
95- if __name__ == ' __main__' :
96+ if __name__ == " __main__" :
9697 main ()
0 commit comments