Also raise error in Span.__reduce__

This commit is contained in:
Ines Montani 2019-02-13 13:22:05 +01:00
parent 2d0c3c73f4
commit fbf9f1edf1
3 changed files with 12 additions and 1 deletions

View File

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

View File

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

View File

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