import pytest from pytojsonschema.common import init_schema_map, InvalidTypeAnnotation from pytojsonschema.functions import filter_by_patterns, process_function_def, process_file, process_package from .conftest import assert_expected, TEST_TYPING_NAMESPACE @pytest.mark.parametrize( "ast_function_def, type_namespace, schema_map, expected", [ [ ast.parse("def foo(a, /): pass").body[0], TEST_TYPING_NAMESPACE, init_schema_map(), InvalidTypeAnnotation( "Function 'foo' contains positional only arguments"), ], [ ast.parse("def foo(a, *args): pass").body[0], TEST_TYPING_NAMESPACE, init_schema_map(), InvalidTypeAnnotation( "Function 'foo' contains a variable number positional arguments i.e. *args" ), ], [ ast.parse("def foo(**bar): pass").body[0], TEST_TYPING_NAMESPACE, init_schema_map(),
def test_init_schema_map(): assert init_schema_map() == init_schema_map()
import pytest from pytojsonschema.common import init_schema_map, InvalidTypeAnnotation from pytojsonschema.jsonschema import get_json_schema_from_ast_element from pytojsonschema.types import ANY_SCHEMA from .conftest import assert_expected, TEST_TYPING_NAMESPACE @pytest.mark.parametrize( "ast_element, type_namespace, schema_map, expected", [ [ ast.parse("None").body[0].value, TEST_TYPING_NAMESPACE, init_schema_map(), { "type": "null" } ], [ ast.parse("bool").body[0].value, TEST_TYPING_NAMESPACE, init_schema_map(), { "type": "boolean" } ], [ ast.parse("complex").body[0].value, TEST_TYPING_NAMESPACE, init_schema_map(), InvalidTypeAnnotation( "Type 'complex' is invalid. Base types and the ones you have imported are bool, int, float, str. "
def test_process_import_from_local(): with tempfile.TemporaryDirectory() as package: # Package creation subpackage = os.path.join(package, "subpackage") os.mkdir(subpackage) package_init = os.path.join(package, "__init__.py") package_foo = os.path.join(package, "foo.py") subpackage_init = os.path.join(subpackage, "__init__.py") subpackage_bar = os.path.join(subpackage, "bar.py") subpackage_baz = os.path.join(subpackage, "baz.py") with open(package_init, "w") as f: f.write( "import typing\n\nfrom .foo import B\n\n\nclass A(typing.TypedDict):\n param: B\n" ) with open(package_foo, "w") as f: f.write( "import typing\n\n\nclass B(typing.TypedDict):\n param: int\n" ) with open(subpackage_init, "w") as f: f.write( "import typing\n\n\nC = typing.Union[bool, float]\n\n\neval(3)" ) with open(subpackage_bar, "w") as f: f.write( "from .. import A\nfrom ..foo import B\nfrom . import C\nfrom .baz import D\nfrom .baz import bad\n" ) with open(subpackage_baz, "w") as f: f.write("import typing\n\n\nD = typing.Dict[str, int]\n") # Tests type_namespace = init_typing_namespace() b_schema = { "type": "object", "properties": { "param": { "type": "integer" } }, "required": ["param"], "additionalProperties": False, } a_schema = { "type": "object", "properties": { "param": b_schema }, "required": ["param"], "additionalProperties": False, } with open(package_init) as f: schema_map = init_schema_map() process_import_from( ast.parse(f.read()).body[1], package, type_namespace, schema_map) assert schema_map == { "bool": { "type": "boolean" }, "int": { "type": "integer" }, "float": { "type": "number" }, "str": { "type": "string" }, "B": b_schema, } with open(subpackage_bar) as f: file_content = f.read() schema_map = init_schema_map() process_import_from( ast.parse(file_content).body[0], subpackage, type_namespace, schema_map) assert schema_map == { "bool": { "type": "boolean" }, "int": { "type": "integer" }, "float": { "type": "number" }, "str": { "type": "string" }, "A": a_schema, } schema_map = init_schema_map() process_import_from( ast.parse(file_content).body[1], subpackage, type_namespace, schema_map) assert schema_map == { "bool": { "type": "boolean" }, "int": { "type": "integer" }, "float": { "type": "number" }, "str": { "type": "string" }, "B": b_schema, } schema_map = init_schema_map() process_import_from( ast.parse(file_content).body[2], subpackage, type_namespace, schema_map) assert schema_map == { "bool": { "type": "boolean" }, "int": { "type": "integer" }, "float": { "type": "number" }, "str": { "type": "string" }, "C": { "anyOf": [{ "type": "boolean" }, { "type": "number" }] }, } schema_map = init_schema_map() process_import_from( ast.parse(file_content).body[3], subpackage, type_namespace, schema_map) assert schema_map == { "bool": { "type": "boolean" }, "int": { "type": "integer" }, "float": { "type": "number" }, "str": { "type": "string" }, "D": { "additionalProperties": { "type": "integer" }, "type": "object" }, } schema_map = init_schema_map() process_import_from( ast.parse(file_content).body[4], subpackage, type_namespace, schema_map) assert schema_map == { "bool": { "type": "boolean" }, "int": { "type": "integer" }, "float": { "type": "number" }, "str": { "type": "string" }, }
def test_process_import_from_external(ast_import_from, base_path, expected): type_namespace, schema_map = init_typing_namespace(), init_schema_map() process_import_from(ast_import_from, base_path, type_namespace, schema_map) assert expected[0] == type_namespace assert expected[1] == schema_map
def test_process_import(ast_import, expected): type_namespace, schema_map = init_typing_namespace(), init_schema_map() process_import(ast_import, type_namespace, schema_map) assert expected[0] == type_namespace assert expected[1] == schema_map
"ast_import, expected", [ [ ast.parse("import typing as foo").body[0], ( { "Union": {"foo.Union"}, "List": {"foo.List"}, "Dict": {"foo.Dict"}, "Optional": {"foo.Optional"}, "Any": {"foo.Any"}, "TypedDict": {"foo.TypedDict"}, "Enum": set(), }, dict( init_schema_map(), **{ "foo.Any": { "anyOf": [ { "type": "object" }, { "type": "array" }, { "type": "null" }, { "type": "string" },