Skip to content

Commit a7f6bc5

Browse files
rmitschsvlandeg
andauthored
Workaround for Typer optional default values with Python calls (explosion#10788)
* Workaround for Typer optional default values with Python calls: added test and workaround. * @rmitsch Workaround for Typer optional default values with Python calls: reverting some black formatting changes. Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> * @rmitsch Workaround for Typer optional default values with Python calls: removing return type hint. Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> * Workaround for Typer optional default values with Python calls: fixed imports, added GitHub issue marker. * Workaround for Typer optional default values with Python calls: removed forcing of default values for optional arguments in init_config_cli(). Added default values for init_config(). Synchronized default values for init_config_cli() and init_config(). * Workaround for Typer optional default values with Python calls: removed unused import. * Workaround for Typer optional default values with Python calls: fixed usage of optimize in init_config_cli(). * Workaround for Typer optional default values with Pythhon calls: remove output_file from InitDefaultValues. * Workaround for Typer optional default values with Python calls: rename class for default init values. * Workaround for Typer optional default values with Python calls: remove newline. * remove introduced newlines * Remove test_init_config_from_python_without_optional_args(). * remove leftover import * reformat import * remove duplicate Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
1 parent 3d3fbed commit a7f6bc5

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

spacy/cli/init_config.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .. import util
1111
from ..language import DEFAULT_CONFIG_PRETRAIN_PATH
1212
from ..schemas import RecommendationSchema
13+
from ..util import SimpleFrozenList
1314
from ._util import init_cli, Arg, Opt, show_validation_error, COMMAND
1415
from ._util import string_to_list, import_code
1516

@@ -24,16 +25,30 @@ class Optimizations(str, Enum):
2425
accuracy = "accuracy"
2526

2627

28+
class InitValues:
29+
"""
30+
Default values for initialization. Dedicated class to allow synchronized default values for init_config_cli() and
31+
init_config(), i.e. initialization calls via CLI respectively Python.
32+
"""
33+
34+
lang = "en"
35+
pipeline = SimpleFrozenList(["tagger", "parser", "ner"])
36+
optimize = Optimizations.efficiency
37+
gpu = False
38+
pretraining = False
39+
force_overwrite = False
40+
41+
2742
@init_cli.command("config")
2843
def init_config_cli(
2944
# fmt: off
3045
output_file: Path = Arg(..., help="File to save the config to or - for stdout (will only output config and no additional logging info)", allow_dash=True),
31-
lang: str = Opt("en", "--lang", "-l", help="Two-letter code of the language to use"),
32-
pipeline: str = Opt("tagger,parser,ner", "--pipeline", "-p", help="Comma-separated names of trainable pipeline components to include (without 'tok2vec' or 'transformer')"),
33-
optimize: Optimizations = Opt(Optimizations.efficiency.value, "--optimize", "-o", help="Whether to optimize for efficiency (faster inference, smaller model, lower memory consumption) or higher accuracy (potentially larger and slower model). This will impact the choice of architecture, pretrained weights and related hyperparameters."),
34-
gpu: bool = Opt(False, "--gpu", "-G", help="Whether the model can run on GPU. This will impact the choice of architecture, pretrained weights and related hyperparameters."),
35-
pretraining: bool = Opt(False, "--pretraining", "-pt", help="Include config for pretraining (with 'spacy pretrain')"),
36-
force_overwrite: bool = Opt(False, "--force", "-F", help="Force overwriting the output file"),
46+
lang: str = Opt(InitValues.lang, "--lang", "-l", help="Two-letter code of the language to use"),
47+
pipeline: str = Opt(",".join(InitValues.pipeline), "--pipeline", "-p", help="Comma-separated names of trainable pipeline components to include (without 'tok2vec' or 'transformer')"),
48+
optimize: Optimizations = Opt(InitValues.optimize, "--optimize", "-o", help="Whether to optimize for efficiency (faster inference, smaller model, lower memory consumption) or higher accuracy (potentially larger and slower model). This will impact the choice of architecture, pretrained weights and related hyperparameters."),
49+
gpu: bool = Opt(InitValues.gpu, "--gpu", "-G", help="Whether the model can run on GPU. This will impact the choice of architecture, pretrained weights and related hyperparameters."),
50+
pretraining: bool = Opt(InitValues.pretraining, "--pretraining", "-pt", help="Include config for pretraining (with 'spacy pretrain')"),
51+
force_overwrite: bool = Opt(InitValues.force_overwrite, "--force", "-F", help="Force overwriting the output file"),
3752
# fmt: on
3853
):
3954
"""
@@ -133,11 +148,11 @@ def fill_config(
133148

134149
def init_config(
135150
*,
136-
lang: str,
137-
pipeline: List[str],
138-
optimize: str,
139-
gpu: bool,
140-
pretraining: bool = False,
151+
lang: str = InitValues.lang,
152+
pipeline: List[str] = InitValues.pipeline,
153+
optimize: str = InitValues.optimize,
154+
gpu: bool = InitValues.gpu,
155+
pretraining: bool = InitValues.pretraining,
141156
silent: bool = True,
142157
) -> Config:
143158
msg = Printer(no_print=silent)

0 commit comments

Comments
 (0)