test_pydantic_compat.py
1 import ast 2 import glob 3 import os.path 4 from _ast import Import 5 from _ast import ImportFrom 6 from typing import Any 7 8 import pytest 9 10 import evidently 11 12 IMPORT_EXCEPTIONS = ["evidently.ui.config"] 13 IMPORT_EXCEPTIONS = [os.path.join(*i.split(".")) + ".py" for i in IMPORT_EXCEPTIONS] 14 15 16 @pytest.mark.parametrize( 17 "sourcefile", 18 glob.glob( 19 os.path.join(os.path.dirname(os.path.dirname(evidently.__file__)), "**", "*.py"), 20 recursive=True, 21 ), 22 ) 23 def test_all_imports_from_compat(sourcefile): 24 if sourcefile.endswith("_pydantic_compat.py"): 25 return 26 with open(sourcefile, encoding="utf8") as f: 27 tree = ast.parse(f.read(), filename=sourcefile) 28 29 class Visitor(ast.NodeVisitor): 30 def visit_ImportFrom(self, node: ImportFrom) -> Any: 31 if node.module is not None and node.module.startswith("pydantic"): 32 raise Exception(f"{sourcefile}:{node.lineno}") 33 34 def visit_Import(self, node: Import) -> Any: 35 if any(sourcefile.endswith(p) for p in IMPORT_EXCEPTIONS): 36 return 37 if any("pydantic" == name.name for name in node.names): 38 raise Exception(f"{sourcefile}:{node.lineno}") 39 40 Visitor().visit(tree)