MCPcopy
hub / github.com/explosion/spaCy / test_language_pipe_error_handler_custom

Function test_language_pipe_error_handler_custom

spacy/tests/test_language.py:340–364  ·  view source on GitHub ↗

Test the error handling of a custom component that has no pipe method

(en_vocab, n_process)

Source from the content-addressed store, hash-verified

338
339@pytest.mark.parametrize("n_process", [1, 2])
340def test_language_pipe_error_handler_custom(en_vocab, n_process):
341 """Test the error handling of a custom component that has no pipe method"""
342 Language.component("my_evil_component", func=evil_component)
343 ops = get_current_ops()
344 if isinstance(ops, NumpyOps) or n_process < 2:
345 nlp = English()
346 nlp.add_pipe("my_evil_component")
347 texts = ["TEXT 111", "TEXT 222", "TEXT 333", "TEXT 342", "TEXT 666"]
348 with pytest.raises(ValueError):
349 # the evil custom component throws an error
350 list(nlp.pipe(texts))
351
352 nlp.set_error_handler(warn_error)
353 logger = logging.getLogger("spacy")
354 with mock.patch.object(logger, "warning") as mock_warning:
355 # the errors by the evil custom component raise a warning for each
356 # bad doc
357 docs = list(nlp.pipe(texts, n_process=n_process))
358 # HACK/TODO? the warnings in child processes don't seem to be
359 # detected by the mock logger
360 if n_process == 1:
361 mock_warning.assert_called()
362 assert mock_warning.call_count == 2
363 assert len(docs) + mock_warning.call_count == len(texts)
364 assert [doc.text for doc in docs] == ["TEXT 111", "TEXT 333", "TEXT 666"]
365
366
367@pytest.mark.parametrize("n_process", [1, 2])

Callers

nothing calls this directly

Calls 5

EnglishClass · 0.90
componentMethod · 0.80
add_pipeMethod · 0.80
set_error_handlerMethod · 0.80
pipeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…