From b7aff6020c34ecae3bb0891b469193d8772b8197 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Sat, 30 May 2020 15:18:53 +0200 Subject: [PATCH] Make functions more general purpose and update docstrings and tests --- spacy/cli/validate.py | 4 ++-- spacy/tests/test_misc.py | 22 ++++++++++++---------- spacy/util.py | 27 +++++++++++++++++++++------ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/spacy/cli/validate.py b/spacy/cli/validate.py index 3c49abb3e..080cd77e2 100644 --- a/spacy/cli/validate.py +++ b/spacy/cli/validate.py @@ -5,7 +5,7 @@ from wasabi import msg from .. import about from ..util import get_package_version, get_installed_models, get_base_version -from ..util import get_package_path, get_model_meta, is_compatible_model +from ..util import get_package_path, get_model_meta, is_compatible_version def validate(): @@ -83,7 +83,7 @@ def get_model_pkgs(): model_path = get_package_path(package) model_meta = get_model_meta(model_path) spacy_version = model_meta.get("spacy_version", "n/a") - is_compat = is_compatible_model(spacy_version) + is_compat = is_compatible_version(about.__version__, spacy_version) pkgs[pkg_name] = { "name": package, "version": version, diff --git a/spacy/tests/test_misc.py b/spacy/tests/test_misc.py index 9aa95c431..e4b4e570c 100644 --- a/spacy/tests/test_misc.py +++ b/spacy/tests/test_misc.py @@ -94,16 +94,18 @@ def test_ascii_filenames(): @pytest.mark.parametrize( - "version,compatible", + "version,constraint,compatible", [ - (spacy_version, True), - (f">={spacy_version}", True), - ("2.0.0", False), - (">=2.0.0", True), - (">=1.0.0,<2.1.1", False), - (">=1.2.3,<4.5.6", True), - ("n/a", None), + (spacy_version, spacy_version, True), + (spacy_version, f">={spacy_version}", True), + ("3.0.0", "2.0.0", False), + ("3.2.1", ">=2.0.0", True), + ("2.2.10a1", ">=1.0.0,<2.1.1", False), + ("3.0.0.dev3", ">=1.2.3,<4.5.6", True), + ("n/a", ">=1.2.3,<4.5.6", None), + ("1.2.3", "n/a", None), + ("n/a", "n/a", None), ], ) -def test_is_compatible_model(version, compatible): - assert util.is_compatible_model(version) is compatible +def test_is_compatible_version(version, constraint, compatible): + assert util.is_compatible_version(version, constraint) is compatible diff --git a/spacy/util.py b/spacy/util.py index 835e46fc6..741b289c1 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -238,17 +238,27 @@ def get_package_version(name): return None -def is_compatible_model(constraint): - version = Version(about.__version__) +def is_compatible_version(version, constraint, prereleases=True): + """Check if a version (e.g. "2.0.0") is compatible given a version + constraint (e.g. ">=1.9.0,<2.2.1"). If the constraint is a specific version, + it's interpreted as =={version}. + + version (str): The version to check. + constraint (str): The constraint string. + prereleases (bool): Whether to allow prereleases. If set to False, + prerelease versions will be considered incompatible. + RETURNS (bool / None): Whether the version is compatible, or None if the + version or constraint are invalid. + """ + # Handle cases where exact version is provided as constraint if constraint[0].isdigit(): - # Handle cases where exact version is provided as constraint constraint = f"=={constraint}" try: spec = SpecifierSet(constraint) - except InvalidSpecifier: + version = Version(version) + except (InvalidSpecifier, InvalidVersion): return None - # Allow prereleases and dev versions - spec.prereleases = True + spec.prereleases = prereleases return version in spec @@ -262,6 +272,11 @@ def get_model_version_range(spacy_version): def get_base_version(version): + """Generate the base version without any prerelease identifiers. + + version (str): The version, e.g. "3.0.0.dev1". + RETURNS (str): The base version, e.g. "3.0.0". + """ return Version(version).base_version