From d3b3663942ebe862a83cba4ac5a3e2b0e3a6a2cc Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Sun, 4 Oct 2020 10:11:27 +0200 Subject: [PATCH] Adjust error message and add test --- spacy/errors.py | 7 +++++-- spacy/pipeline/pipe.pyx | 2 +- spacy/tests/pipeline/test_pipe_methods.py | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/spacy/errors.py b/spacy/errors.py index 878eed114..5343e7ce8 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -85,8 +85,11 @@ class Warnings: "attribute or operator.") # TODO: fix numbering after merging develop into master - W088 = ("This component implements a 'begin_training' method, " - "which should probably be renamed to 'initialize'.") + W088 = ("The pipeline component {name} implements a 'begin_training' " + "method, which won't be called by spaCy. As of v3.0, 'begin_training' " + "has been renamed to 'initialize' so you likely want to rename the " + "component method. See the documentation for details: " + "https://nightly.spacy.io/api/language#initialize") W089 = ("The nlp.begin_training method has been renamed to nlp.initialize.") W090 = ("Could not locate any {format} files in path '{path}'.") W091 = ("Could not clean/remove the temp directory at {dir}: {msg}.") diff --git a/spacy/pipeline/pipe.pyx b/spacy/pipeline/pipe.pyx index a18f04ee3..41ca23ace 100644 --- a/spacy/pipeline/pipe.pyx +++ b/spacy/pipeline/pipe.pyx @@ -39,7 +39,7 @@ cdef class Pipe: """Raise a warning if an inheriting class implements 'begin_training' (from v2) instead of the new 'initialize' method (from v3)""" if hasattr(cls, "begin_training"): - warnings.warn(Warnings.W088) + warnings.warn(Warnings.W088.format(name=cls.__name__)) @property def labels(self) -> Optional[Tuple[str]]: diff --git a/spacy/tests/pipeline/test_pipe_methods.py b/spacy/tests/pipeline/test_pipe_methods.py index c0b9762ed..e647ba440 100644 --- a/spacy/tests/pipeline/test_pipe_methods.py +++ b/spacy/tests/pipeline/test_pipe_methods.py @@ -1,5 +1,6 @@ import pytest from spacy.language import Language +from spacy.pipeline import Pipe from spacy.util import SimpleFrozenList, get_arg_names @@ -370,3 +371,14 @@ def test_pipe_label_data_no_labels(pipe): initialize = getattr(pipe, "initialize", None) if initialize is not None: assert "labels" not in get_arg_names(initialize) + + +def test_warning_pipe_begin_training(): + with pytest.warns(UserWarning, match="begin_training"): + + class IncompatPipe(Pipe): + def __init__(self): + ... + + def begin_training(*args, **kwargs): + ...