def test_defined_name_twice(tmp_directory, add_current_to_sys_path, no_sys_modules_cache): Path('a.py').write_text(""" def b(): pass def b(): pass """) loc = PythonCallableSource(dotted_path.load_dotted_path('a.b')).loc out = PythonCallableSource('a.b').loc assert str(Path(out).resolve()) == str(Path(loc).resolve())
def test_error_if_python_callable_does_not_need_product_but_has_it(): def fn(product): pass with pytest.raises(TypeError) as excinfo: PythonCallableSource(fn, needs_product=False).render({}) assert ("Function 'fn' should not have a 'product' " "parameter, but return its result instead") == str(excinfo.value)
def test_python_callable_with_dotted_path(tmp_directory, add_current_to_sys_path): Path('some_other_dotted_path.py').write_text(""" def some_fn(): pass """) # doing it this way should not import anything source = PythonCallableSource('some_other_dotted_path.some_fn') assert callable(source.primitive)
def test_detects_aliasing(tmp_directory, add_current_to_sys_path, no_sys_modules_cache): Path('a.py').write_text(""" from alias import b """) with pytest.raises(NotImplementedError): PythonCallableSource('a.b').loc
def test_hot_reload(backup_test_pkg): path_to_functions = Path(backup_test_pkg, 'functions.py') source = PythonCallableSource(functions.some_function, hot_reload=True) source_old = path_to_functions.read_text() source_new = 'def some_function():\n 1 + 1\n' path_to_functions.write_text(source_new) assert str(source) == source_new path_to_functions.write_text(source_old)
def test_python_callable_with_dotted_path_does_not_import( tmp_directory, add_current_to_sys_path): Path('some_dotted_path.py').write_text(""" import some_unknown_package def some_fn(): pass """) # doing it this way should not import anything source = PythonCallableSource('some_dotted_path.some_fn') assert str(source) == 'def some_fn():\n pass\n' assert source.name == 'some_fn'
def test_python_callable_properties(path_to_test_pkg): source = PythonCallableSource(functions.simple_w_docstring) tokens = source.loc.split(':') file_, line = ':'.join(tokens[:-1]), tokens[-1] assert source.doc == functions.simple_w_docstring.__doc__ assert source.name == 'simple_w_docstring' assert file_ == functions.__file__ assert line == '23' assert PythonCallableSource.__name__ in repr(source) assert functions.simple_w_docstring.__name__ in repr(source) assert source.loc in repr(source)
def test_error_if_last_def_not_a_function(tmp_directory, add_current_to_sys_path, no_sys_modules_cache, source): Path('a.py').write_text(f""" def b(): pass {source} """) with pytest.raises(TypeError) as excinfo: PythonCallableSource('a.b').loc assert ("Failed to load dotted path 'a.b'. Expected last " "defined 'b' to be a function." in str(excinfo.value))
def test_loc_is_consisent_when_initialized_from_str(tmp_directory, add_current_to_sys_path, no_sys_modules_cache, target_file, dotted_path_str): target_parent = Path(target_file).parent target_parent.mkdir(parents=True, exist_ok=True) for parent in Path(target_file).parents: (parent / '__init__.py').touch() Path(target_file).write_text(""" def symbol(): pass """) out = PythonCallableSource(dotted_path_str).loc # check that a.py hasn't been imported assert 'a' not in sys.modules loc = PythonCallableSource( dotted_path.load_dotted_path(dotted_path_str)).loc assert str(Path(out).resolve()) == str(Path(loc).resolve())
def test_python_callable_with_dotted_path_does_not_import( tmp_directory, add_current_to_sys_path, no_sys_modules_cache): loc = Path('some_dotted_path.py') loc.write_text(""" import some_unknown_package def some_fn(): pass """) loc = loc.resolve() source = PythonCallableSource('some_dotted_path.some_fn') assert str(source) == 'def some_fn():\n pass\n' assert source.name == 'some_fn' assert str(Path(source.loc).resolve()) == f'{loc}:4'
def _init_source(self, source): return PythonCallableSource(source)
def _init_source(source, kwargs): return PythonCallableSource(source, **kwargs)
def test_dotted_path_and_callable_give_same_source(): a = PythonCallableSource(functions.simple_w_docstring) b = PythonCallableSource('test_pkg.functions.simple_w_docstring') assert str(a) == str(b)