mirror of
https://github.com/explosion/spaCy.git
synced 2025-07-07 13:26:02 +03:00
Make functions more general purpose and update docstrings and tests
This commit is contained in:
parent
a7e370bcbf
commit
b7aff6020c
|
@ -5,7 +5,7 @@ from wasabi import msg
|
||||||
|
|
||||||
from .. import about
|
from .. import about
|
||||||
from ..util import get_package_version, get_installed_models, get_base_version
|
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():
|
def validate():
|
||||||
|
@ -83,7 +83,7 @@ def get_model_pkgs():
|
||||||
model_path = get_package_path(package)
|
model_path = get_package_path(package)
|
||||||
model_meta = get_model_meta(model_path)
|
model_meta = get_model_meta(model_path)
|
||||||
spacy_version = model_meta.get("spacy_version", "n/a")
|
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] = {
|
pkgs[pkg_name] = {
|
||||||
"name": package,
|
"name": package,
|
||||||
"version": version,
|
"version": version,
|
||||||
|
|
|
@ -94,16 +94,18 @@ def test_ascii_filenames():
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"version,compatible",
|
"version,constraint,compatible",
|
||||||
[
|
[
|
||||||
(spacy_version, True),
|
(spacy_version, spacy_version, True),
|
||||||
(f">={spacy_version}", True),
|
(spacy_version, f">={spacy_version}", True),
|
||||||
("2.0.0", False),
|
("3.0.0", "2.0.0", False),
|
||||||
(">=2.0.0", True),
|
("3.2.1", ">=2.0.0", True),
|
||||||
(">=1.0.0,<2.1.1", False),
|
("2.2.10a1", ">=1.0.0,<2.1.1", False),
|
||||||
(">=1.2.3,<4.5.6", True),
|
("3.0.0.dev3", ">=1.2.3,<4.5.6", True),
|
||||||
("n/a", None),
|
("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):
|
def test_is_compatible_version(version, constraint, compatible):
|
||||||
assert util.is_compatible_model(version) is compatible
|
assert util.is_compatible_version(version, constraint) is compatible
|
||||||
|
|
|
@ -238,17 +238,27 @@ def get_package_version(name):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def is_compatible_model(constraint):
|
def is_compatible_version(version, constraint, prereleases=True):
|
||||||
version = Version(about.__version__)
|
"""Check if a version (e.g. "2.0.0") is compatible given a version
|
||||||
if constraint[0].isdigit():
|
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
|
# Handle cases where exact version is provided as constraint
|
||||||
|
if constraint[0].isdigit():
|
||||||
constraint = f"=={constraint}"
|
constraint = f"=={constraint}"
|
||||||
try:
|
try:
|
||||||
spec = SpecifierSet(constraint)
|
spec = SpecifierSet(constraint)
|
||||||
except InvalidSpecifier:
|
version = Version(version)
|
||||||
|
except (InvalidSpecifier, InvalidVersion):
|
||||||
return None
|
return None
|
||||||
# Allow prereleases and dev versions
|
spec.prereleases = prereleases
|
||||||
spec.prereleases = True
|
|
||||||
return version in spec
|
return version in spec
|
||||||
|
|
||||||
|
|
||||||
|
@ -262,6 +272,11 @@ def get_model_version_range(spacy_version):
|
||||||
|
|
||||||
|
|
||||||
def get_base_version(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
|
return Version(version).base_version
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user