diff --git a/spacy/errors.py b/spacy/errors.py index 567e29cd0..e97fc49e9 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -308,6 +308,12 @@ class Errors(object): "would always have to include its Doc and Vocab, which has " "practically no advantage over pickling the parent Doc directly. " "So instead of pickling the token, pickle the Doc it belongs to.") + E112 = ("Pickling a span is not supported, because spans are only views " + "of the parent Doc and can't exist on their own. A pickled span " + "would always have to include its Doc and Vocab, which has " + "practically no advantage over pickling the parent Doc directly. " + "So instead of pickling the span, pickle the Doc it belongs to or " + "use Span.as_doc to convert the span to a standalone Doc object.") @add_codes class TempErrors(object): diff --git a/spacy/tests/regression/test_issue2833.py b/spacy/tests/regression/test_issue2833.py index 81aa40eb3..de71a6524 100644 --- a/spacy/tests/regression/test_issue2833.py +++ b/spacy/tests/regression/test_issue2833.py @@ -7,7 +7,9 @@ from spacy.compat import pickle def test_issue2833(en_vocab): - """Test that a custom error is raised if a token is pickled.""" + """Test that a custom error is raised if a token or span is pickled.""" doc = Doc(en_vocab, words=["Hello", "world"]) with pytest.raises(NotImplementedError): pickle.dumps(doc[0]) + with pytest.raises(NotImplementedError): + pickle.dumps(doc[0:2]) diff --git a/spacy/tokens/span.pyx b/spacy/tokens/span.pyx index 9c7d8d153..593e6ddec 100644 --- a/spacy/tokens/span.pyx +++ b/spacy/tokens/span.pyx @@ -141,6 +141,9 @@ cdef class Span: for i in range(self.start, self.end): yield self.doc[i] + def __reduce__(self): + raise NotImplementedError(Errors.E112) + @property def _(self): """User space for adding custom attribute extensions."""