Merge pull request #5535 from adrianeboyd/feature/model-spacy-version-check

This commit is contained in:
Ines Montani 2020-06-03 15:35:20 +02:00 committed by GitHub
commit d79964bcb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 0 deletions

View File

@ -115,6 +115,18 @@ class Warnings(object):
"`spacy.gold.biluo_tags_from_offsets(nlp.make_doc(text), entities)`"
" to check the alignment. Misaligned entities ('-') will be "
"ignored during training.")
W031 = ("Model '{model}' ({model_version}) requires spaCy {version} and "
"is incompatible with the current spaCy version ({current}). This "
"may lead to unexpected results or runtime errors. To resolve "
"this, download a newer compatible model or retrain your custom "
"model with the current spaCy version. For more details and "
"available updates, run: python -m spacy validate")
W032 = ("Unable to determine model compatibility for model '{model}' "
"({model_version}) with the current spaCy version ({current}). "
"This may lead to unexpected results or runtime errors. To resolve "
"this, download a newer compatible model or retrain your custom "
"model with the current spaCy version. For more details and "
"available updates, run: python -m spacy validate")
@add_codes

View File

@ -4,12 +4,14 @@ from __future__ import unicode_literals
import pytest
import os
import ctypes
import srsly
from pathlib import Path
from spacy import util
from spacy import prefer_gpu, require_gpu
from spacy.compat import symlink_to, symlink_remove, path2str, is_windows
from spacy._ml import PrecomputableAffine
from subprocess import CalledProcessError
from .util import make_tempdir
@pytest.fixture
@ -146,3 +148,33 @@ def test_load_model_blank_shortcut():
assert nlp.pipeline == []
with pytest.raises(ImportError):
util.load_model("blank:fjsfijsdof")
def test_load_model_version_compat():
"""Test warnings for various spacy_version specifications in meta. Since
this is more of a hack for v2, manually specify the current major.minor
version to simplify test creation."""
nlp = util.load_model("blank:en")
assert nlp.meta["spacy_version"].startswith(">=2.3")
with make_tempdir() as d:
# no change: compatible
nlp.to_disk(d)
meta_path = Path(d / "meta.json")
util.get_model_meta(d)
# additional compatible upper pin
nlp.meta["spacy_version"] = ">=2.3.0,<2.4.0"
srsly.write_json(meta_path, nlp.meta)
util.get_model_meta(d)
# incompatible older version
nlp.meta["spacy_version"] = ">=2.2.5"
srsly.write_json(meta_path, nlp.meta)
with pytest.warns(UserWarning):
util.get_model_meta(d)
# invalid version specification
nlp.meta["spacy_version"] = ">@#$%_invalid_version"
srsly.write_json(meta_path, nlp.meta)
with pytest.warns(UserWarning):
util.get_model_meta(d)

View File

@ -17,6 +17,7 @@ import srsly
import catalogue
import sys
import warnings
from . import about
try:
import jsonschema
@ -250,6 +251,31 @@ def get_model_meta(path):
for setting in ["lang", "name", "version"]:
if setting not in meta or not meta[setting]:
raise ValueError(Errors.E054.format(setting=setting))
if "spacy_version" in meta:
about_major_minor = ".".join(about.__version__.split(".")[:2])
if not meta["spacy_version"].startswith(">=" + about_major_minor):
# try to simplify version requirements from model meta to vx.x
# for warning message
meta_spacy_version = "v" + ".".join(
meta["spacy_version"].replace(">=", "").split(".")[:2]
)
# if the format is unexpected, supply the full version
if not re.match(r"v\d+\.\d+", meta_spacy_version):
meta_spacy_version = meta["spacy_version"]
warn_msg = Warnings.W031.format(
model=meta["lang"] + "_" + meta["name"],
model_version=meta["version"],
version=meta_spacy_version,
current=about.__version__,
)
warnings.warn(warn_msg)
else:
warn_msg = Warnings.W032.format(
model=meta["lang"] + "_" + meta["name"],
model_version=meta["version"],
current=about.__version__,
)
warnings.warn(warn_msg)
return meta