Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for Typer optional default values with Python calls #10788

Merged
merged 17 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
dd8c03c
Workaround for Typer optional default values with Python calls: added…
rmitsch May 12, 2022
fe0730f
@rmitsch Workaround for Typer optional default values with Python cal…
rmitsch May 18, 2022
457a5c9
@rmitsch Workaround for Typer optional default values with Python cal…
rmitsch May 18, 2022
ef66c3d
Workaround for Typer optional default values with Python calls: fixed…
rmitsch May 18, 2022
9021176
Workaround for Typer optional default values with Python calls: remov…
rmitsch May 23, 2022
e19a14b
Workaround for Typer optional default values with Python calls: remov…
rmitsch May 23, 2022
e2555e5
Workaround for Typer optional default values with Python calls: fixed…
rmitsch May 23, 2022
5c7eed2
Workaround for Typer optional default values with Pythhon calls: remo…
rmitsch May 24, 2022
a569674
Merge branch 'master' into fix/typer-option-default-values
rmitsch May 24, 2022
2abe11e
Workaround for Typer optional default values with Python calls: renam…
rmitsch May 25, 2022
596fcf0
Merge branch 'fix/typer-option-default-values' of github.com:rmitsch/…
rmitsch May 25, 2022
527bee1
Workaround for Typer optional default values with Python calls: remov…
rmitsch May 25, 2022
da5ae2e
remove introduced newlines
svlandeg Jun 9, 2022
19a4d7a
Remove test_init_config_from_python_without_optional_args().
rmitsch Jun 9, 2022
16f8944
remove leftover import
svlandeg Jun 10, 2022
58047f8
reformat import
svlandeg Jun 10, 2022
64db12e
remove duplicate
svlandeg Jun 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions spacy/cli/init_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Optional, List, Tuple
from enum import Enum
from pathlib import Path

svlandeg marked this conversation as resolved.
Show resolved Hide resolved
import typer.models
from wasabi import Printer, diff_strings
from thinc.api import Config
import srsly
Expand Down Expand Up @@ -44,20 +46,37 @@ def init_config_cli(

DOCS: https://spacy.io/api/cli#init-config
"""
pipeline = string_to_list(pipeline)

rmitsch marked this conversation as resolved.
Show resolved Hide resolved
# Force default values for arguments, if Typer hasn't resolved them yet (might occur on calls from Python).
# Mandatory arguments are assigned ... as default value in Typer, hence we ignore those.
optional_args = {
arg_name: arg_value.default
if any(
[
isinstance(arg_value, typer_type)
for typer_type in (typer.models.OptionInfo, typer.models.ArgumentInfo)
]
)
and arg_value.default != Ellipsis
else arg_value
for arg_name, arg_value in locals().items()
}

optional_args["pipeline"] = string_to_list(optional_args["pipeline"])
is_stdout = str(output_file) == "-"
if not is_stdout and output_file.exists() and not force_overwrite:
if not is_stdout and output_file.exists() and not optional_args["force_overwrite"]:
msg = Printer()
msg.fail(
"The provided output file already exists. To force overwriting the config file, set the --force or -F flag.",
exits=1,
)

svlandeg marked this conversation as resolved.
Show resolved Hide resolved
config = init_config(
lang=lang,
pipeline=pipeline,
optimize=optimize.value,
gpu=gpu,
pretraining=pretraining,
lang=optional_args["lang"],
pipeline=optional_args["pipeline"],
optimize=optional_args["optimize"],
gpu=optional_args["gpu"],
pretraining=optional_args["pretraining"],
silent=is_stdout,
)
save_config(config, output_file, is_stdout=is_stdout)
Expand Down
23 changes: 23 additions & 0 deletions spacy/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from spacy.cli.debug_data import _get_labels_from_spancat
from spacy.cli.download import get_compatibility, get_version
from spacy.cli.init_config import RECOMMENDATIONS, init_config, fill_config
from spacy.cli.init_config import init_config_cli, Optimizations
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
from spacy.cli.package import get_third_party_dependencies
from spacy.cli.package import _is_permitted_package_name
from spacy.cli.validate import get_model_pkgs
Expand Down Expand Up @@ -740,3 +741,25 @@ def test_debug_data_compile_gold():
eg = Example(pred, ref)
data = _compile_gold([eg], ["ner"], nlp, True)
assert data["boundary_cross_ents"] == 1


@pytest.mark.issue(10727)
def test_init_config_from_python_without_optional_args():
rmitsch marked this conversation as resolved.
Show resolved Hide resolved
"""
Tests calling init_config_cli() from Python with optional arguments not set. This should detect bugs related to
Typer not automatically setting default values for arguments when decorated functions are called from Python instead
from the CLI.
"""

with make_tempdir() as temp_dir:
init_config_cli(output_file=temp_dir / "config.cfg")

with make_tempdir() as temp_dir:
init_config_cli(
output_file=temp_dir / "config.cfg",
lang="en",
pipeline="ner",
optimize=Optimizations.efficiency,
gpu=False,
pretraining=False,
)