Skip to content

Commit

Permalink
Overlapping function names prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
vvancak committed Sep 17, 2024
1 parent a13553c commit ea30fe1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
19 changes: 18 additions & 1 deletion rialto/jobs/module_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
from rialto.common.utils import get_caller_module


class ModuleRegisterException(Exception):
"""Module Register Exception - Usually, means a clash in the dependencies"""

pass


class ModuleRegister:
"""
Module register. Class which is used by @datasource and @config_parser decorators to register callables / getters.
Expand Down Expand Up @@ -72,13 +78,24 @@ def find_callable(cls, callable_name, module_name):
:return: The found callable or None if not found.
"""

found_functions = []

# Loop through this module, and its dependencies
searched_modules = [module_name] + cls._dependency_tree.get(module_name, [])
for module in searched_modules:
# Loop through all functions registered in the module
for func in cls._storage.get(module, []):
if func.__name__ == callable_name:
return func
found_functions.append(func)

if len(found_functions) == 0:
return None

if len(found_functions) > 1:
raise ModuleRegisterException(f"Multiple functions with the same name {callable_name} found !")

else:
return found_functions[0]


def register_dependency_module(module):
Expand Down
15 changes: 15 additions & 0 deletions tests/jobs/dependency_checks_job/duplicate_dependency_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import tests.jobs.dependency_checks_job.datasources_a as a
import tests.jobs.dependency_checks_job.datasources_b as b
from rialto.jobs import job, register_dependency_module

# module "A" has i(), j(), k()
# module "B" has i(j), and dependency on module C

register_dependency_module(b)
register_dependency_module(a)


@job
def duplicate_dependency_job(i):
# i is in both A and B
pass
17 changes: 17 additions & 0 deletions tests/jobs/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import rialto.jobs.decorators as decorators
import tests.jobs.dependency_checks_job.complex_dependency_job as complex_dependency_job
import tests.jobs.dependency_checks_job.dependency_checks_job as dependency_checks_job
import tests.jobs.dependency_checks_job.duplicate_dependency_job as duplicate_dependency_job
import tests.jobs.test_job.test_job as test_job
from rialto.jobs.test_utils import disable_job_decorators, resolver_resolves

Expand Down Expand Up @@ -89,3 +90,19 @@ def test_complex_dependencies_fails_on_unimported(spark):

assert exc_info is not None
assert str(exc_info.value) == "k declaration not found!"


def test_complex_dependencies_fails_on_unimported(spark):
with pytest.raises(Exception) as exc_info:
assert resolver_resolves(spark, complex_dependency_job.unimported_dependency_job)

assert exc_info is not None
assert str(exc_info.value) == "k declaration not found!"


def test_duplicate_dependency_fails_on_duplicate(spark):
with pytest.raises(Exception) as exc_info:
assert resolver_resolves(spark, duplicate_dependency_job.duplicate_dependency_job)

assert exc_info is not None
assert str(exc_info.value) == f"Multiple functions with the same name i found !"

0 comments on commit ea30fe1

Please sign in to comment.