Skip to content

Commit

Permalink
Fix setting SETTINGS.DISCOVER_SOURCES to "none" (IDSIA#835)
Browse files Browse the repository at this point in the history
* Fix setting SETTINGS.DISCOVER_SOURCES to "none"

* Update flake config
  • Loading branch information
thequilo committed Jul 20, 2021
1 parent 914ebc2 commit bb507a3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ repos:
rev: 3.9.2
hooks:
- id: flake8
exclude: ^(tests|examples|docs)/*
exclude: ^(tests|examples|docs)/.*
additional_dependencies: [pep8-naming, flake8-docstrings]
4 changes: 2 additions & 2 deletions sacred/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,14 +709,14 @@ def get_dependencies_from_pkg(globs, base_path):


source_discovery_strategies = {
"none": lambda globs, path: set(),
"none": lambda *_, **__: set(),
"imported": get_sources_from_imported_modules,
"sys": get_sources_from_sys_modules,
"dir": get_sources_from_local_dir,
}

dependency_discovery_strategies = {
"none": lambda globs, path: set(),
"none": lambda *_, **__: set(),
"imported": get_dependencies_from_imported_modules,
"sys": get_dependencies_from_sys_modules,
"pkg": get_dependencies_from_pkg,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pep8ignore =
dist/* ALL
sacred.egg-info/* ALL
[flake8]
ignore = D100,D101,D102,D103,D104,D105,D203,D401,F821,E722,E203,E501 \
ignore = D100,D101,D102,D103,D104,D105,D203,D401,F821,E722,E203,E501,N818 \
# flake8 default ignores:
E121,E123,E126,E226,E24,E704,W503,W504
max-complexity = 10
Expand Down
45 changes: 38 additions & 7 deletions tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os.path
import os
from pathlib import Path

import mock
import pytest
Expand Down Expand Up @@ -138,8 +139,41 @@ def test_package_dependency_repr():
assert repr(pd) == "<PackageDependency: pytest=12.4>"


def test_gather_sources_and_dependencies():
@pytest.mark.parametrize(
"discover_sources, expected_sources",
[
(
"imported",
{
Source.create(os.path.join(TEST_DIRECTORY, "__init__.py")),
Source.create(os.path.join(TEST_DIRECTORY, "dependency_example.py")),
Source.create(os.path.join(TEST_DIRECTORY, "foo", "__init__.py")),
Source.create(os.path.join(TEST_DIRECTORY, "foo", "bar.py")),
},
),
(
"dir",
{
# This list would be too long to explicitly insert here
Source.create(str(path.resolve()))
for path in Path(TEST_DIRECTORY).rglob("*.py")
},
),
(
"none",
{
Source.create(os.path.join(TEST_DIRECTORY, "dependency_example.py")),
},
),
# "sys" is not tested here because it depends on the environment. Can't
# make it work consistently on my local machine and azure
],
)
def test_gather_sources_and_dependencies(discover_sources, expected_sources):
from tests.dependency_example import some_func
from sacred import SETTINGS

SETTINGS.DISCOVER_SOURCES = discover_sources

main, sources, deps = gather_sources_and_dependencies(
some_func.__globals__, save_git_info=False
Expand All @@ -148,12 +182,6 @@ def test_gather_sources_and_dependencies():
assert isinstance(sources, set)
assert isinstance(deps, set)
assert main == Source.create(os.path.join(TEST_DIRECTORY, "dependency_example.py"))
expected_sources = {
Source.create(os.path.join(TEST_DIRECTORY, "__init__.py")),
Source.create(os.path.join(TEST_DIRECTORY, "dependency_example.py")),
Source.create(os.path.join(TEST_DIRECTORY, "foo", "__init__.py")),
Source.create(os.path.join(TEST_DIRECTORY, "foo", "bar.py")),
}
assert sources == expected_sources

assert PackageDependency.create(pytest) in deps
Expand All @@ -166,6 +194,9 @@ def test_gather_sources_and_dependencies():
else:
assert len(deps) == 2

# Reset to default to prevent side-effects
SETTINGS.DISCOVER_SOURCES = "imported"


def test_custom_base_dir():
from tests.basedir.my_experiment import some_func
Expand Down

0 comments on commit bb507a3

Please sign in to comment.