From fdbe0205f19dac00ebac04ec95958446ce107f4d Mon Sep 17 00:00:00 2001 From: boukeversteegh Date: Sun, 14 Jun 2020 22:54:03 +0200 Subject: [PATCH] find_module docstring and search for init files instead of directories --- betterproto/tests/test_inputs.py | 2 +- betterproto/tests/util.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/betterproto/tests/test_inputs.py b/betterproto/tests/test_inputs.py index ede31d6..13f20d1 100644 --- a/betterproto/tests/test_inputs.py +++ b/betterproto/tests/test_inputs.py @@ -91,7 +91,7 @@ def test_data(request): if not plugin_module_entry_point: raise Exception( f"Test case {repr(test_case_name)} has no entry point. " - + "Please add a proto message or service called Test and recompile." + "Please add a proto message or service called Test and recompile." ) yield ( diff --git a/betterproto/tests/util.py b/betterproto/tests/util.py index 40b10c6..4c5889d 100644 --- a/betterproto/tests/util.py +++ b/betterproto/tests/util.py @@ -68,13 +68,23 @@ def get_test_case_json_data(test_case_name, json_file_name=None): def find_module( module: ModuleType, predicate: Callable[[ModuleType], bool] ) -> Optional[ModuleType]: + """ + Recursively search module tree for a module that matches the search predicate. + Assumes that the submodules are directories containing __init__.py. + + Example: + + # find module inside foo that contains Test + import foo + test_module = find_module(foo, lambda m: hasattr(m, 'Test')) + """ if predicate(module): return module module_path = pathlib.Path(*module.__path__) - for sub in list(module_path.glob("**/")): - if not sub.is_dir() or sub == module_path: + for sub in list(sub.parent for sub in module_path.glob("**/__init__.py")): + if sub == module_path: continue sub_module_path = sub.relative_to(module_path) sub_module_name = ".".join(sub_module_path.parts)