From 23c63eefafa3e91eb802e1dba6b1ef3145b71b58 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Wed, 30 Sep 2020 15:15:11 +0200 Subject: [PATCH] Tidy up env vars [ci skip] --- spacy/cli/_util.py | 4 ++-- spacy/tests/test_cli.py | 12 +++++++----- spacy/util.py | 4 ++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/spacy/cli/_util.py b/spacy/cli/_util.py index 2c944bf3a..69c32bbad 100644 --- a/spacy/cli/_util.py +++ b/spacy/cli/_util.py @@ -16,6 +16,7 @@ import os from ..schemas import ProjectConfigSchema, validate from ..util import import_file, run_command, make_tempdir, registry, logger +from ..util import ENV_VARS if TYPE_CHECKING: from pathy import Pathy # noqa: F401 @@ -39,7 +40,6 @@ commands to check and validate your config files, training and evaluation data, and custom model implementations. """ INIT_HELP = """Commands for initializing configs and pipeline packages.""" -OVERRIDES_ENV_VAR = "SPACY_CONFIG_OVERRIDES" # Wrappers for Typer's annotations. Initially created to set defaults and to # keep the names short, but not needed at the moment. @@ -65,7 +65,7 @@ def setup_cli() -> None: def parse_config_overrides( - args: List[str], env_var: Optional[str] = OVERRIDES_ENV_VAR + args: List[str], env_var: Optional[str] = ENV_VARS.CONFIG_OVERRIDES ) -> Dict[str, Any]: """Generate a dictionary of config overrides based on the extra arguments provided on the CLI, e.g. --training.batch_size to override diff --git a/spacy/tests/test_cli.py b/spacy/tests/test_cli.py index bba71d6da..62584d0ce 100644 --- a/spacy/tests/test_cli.py +++ b/spacy/tests/test_cli.py @@ -3,10 +3,11 @@ from click import NoSuchOption from spacy.training import docs_to_json, offsets_to_biluo_tags from spacy.training.converters import iob_to_docs, conll_ner_to_docs, conllu_to_docs from spacy.schemas import ProjectConfigSchema, RecommendationSchema, validate +from spacy.util import ENV_VARS from spacy.cli.init_config import init_config, RECOMMENDATIONS from spacy.cli._util import validate_project_commands, parse_config_overrides from spacy.cli._util import load_project_config, substitute_project_variables -from spacy.cli._util import string_to_list, OVERRIDES_ENV_VAR +from spacy.cli._util import string_to_list from thinc.api import ConfigValidationError import srsly import os @@ -342,21 +343,22 @@ def test_parse_config_overrides_invalid_2(args): def test_parse_cli_overrides(): - os.environ[OVERRIDES_ENV_VAR] = "--x.foo bar --x.bar=12 --x.baz false --y.foo=hello" + overrides = "--x.foo bar --x.bar=12 --x.baz false --y.foo=hello" + os.environ[ENV_VARS.CONFIG_OVERRIDES] = overrides result = parse_config_overrides([]) assert len(result) == 4 assert result["x.foo"] == "bar" assert result["x.bar"] == 12 assert result["x.baz"] is False assert result["y.foo"] == "hello" - os.environ[OVERRIDES_ENV_VAR] = "--x" + os.environ[ENV_VARS.CONFIG_OVERRIDES] = "--x" assert parse_config_overrides([], env_var=None) == {} with pytest.raises(SystemExit): parse_config_overrides([]) - os.environ[OVERRIDES_ENV_VAR] = "hello world" + os.environ[ENV_VARS.CONFIG_OVERRIDES] = "hello world" with pytest.raises(SystemExit): parse_config_overrides([]) - del os.environ[OVERRIDES_ENV_VAR] + del os.environ[ENV_VARS.CONFIG_OVERRIDES] @pytest.mark.parametrize("lang", ["en", "nl"]) diff --git a/spacy/util.py b/spacy/util.py index 761eb9f0a..8a96ba4fe 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -71,6 +71,10 @@ logging.basicConfig(format="%(message)s") logger = logging.getLogger("spacy") +class ENV_VARS: + CONFIG_OVERRIDES = "SPACY_CONFIG_OVERRIDES" + + class registry(thinc.registry): languages = catalogue.create("spacy", "languages", entry_points=True) architectures = catalogue.create("spacy", "architectures", entry_points=True)