Skip to content

Commit

Permalink
fixes logic that loads _module, due to API change in Python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Feb 9, 2021
1 parent 75db187 commit 3e64aa6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/ploomber/env/EnvDict.py
Expand Up @@ -276,22 +276,29 @@ def raw_preprocess(raw, path_to_raw):
raise ValueError('_module cannot be {{here}} if '
'not loaded from a file')
else:
try:
# check if it's a filesystem path
as_path = Path(module)

if as_path.exists():
if as_path.is_file():
raise ValueError(
'Could not resolve _module "{}", '
'expected a module or a directory but got a '
'file'.format(module))
else:
path_to_module = as_path

# must be a dotted path
else:
module_spec = importlib.util.find_spec(module)
except ValueError:
# raises ValueError if passed "."
module_spec = None

if not module_spec:
path_to_module = Path(module)

if not (path_to_module.exists() and path_to_module.is_dir()):
# package does not exist
if module_spec is None:
raise ValueError('Could not resolve _module "{}", '
'failed to import as a module '
'and is not a directory'.format(module))

else:
path_to_module = Path(module_spec.origin).parent
'it is not a valid module '
'nor a directory'.format(module))
else:
path_to_module = Path(module_spec.origin).parent

preprocessed['_module'] = path_to_module

Expand Down
13 changes: 12 additions & 1 deletion tests/env/test_env.py
Expand Up @@ -143,7 +143,18 @@ def test_init_with_nonexistent_package(cleanup_env):
Env({'_module': 'i_do_not_exist'})

expected = ('Could not resolve _module "i_do_not_exist", '
'failed to import as a module and is not a directory')
'it is not a valid module nor a directory')
assert exc_info.value.args[0] == expected


def test_init_with_file(tmp_directory, cleanup_env):
Path('not_a_package').touch()

with pytest.raises(ValueError) as exc_info:
Env({'_module': 'not_a_package'})

expected = ('Could not resolve _module "not_a_package", '
'expected a module or a directory but got a file')
assert exc_info.value.args[0] == expected


Expand Down

0 comments on commit 3e64aa6

Please sign in to comment.