Make functions more general purpose and update docstrings and tests

This commit is contained in:
Ines Montani 2020-05-30 15:18:53 +02:00
parent a7e370bcbf
commit b7aff6020c
3 changed files with 35 additions and 18 deletions

View File

@ -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,

View File

@ -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

View File

@ -238,17 +238,27 @@ def get_package_version(name):
return None
def is_compatible_model(constraint):
version = Version(about.__version__)
if constraint[0].isdigit():
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():
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