mirror of
https://github.com/explosion/spaCy.git
synced 2025-06-26 07:53:18 +03:00
In order to support Python 3.13, we had to migrate to Cython 3.0. This caused some tricky interaction with our Pydantic usage, because Cython 3 uses the from __future__ import annotations semantics, which causes type annotations to be saved as strings. The end result is that we can't have Language.factory decorated functions in Cython modules anymore, as the Language.factory decorator expects to inspect the signature of the functions and build a Pydantic model. If the function is implemented in Cython, an error is raised because the type is not resolved. To address this I've moved the factory functions into a new module, spacy.pipeline.factories. I've added __getattr__ importlib hooks to the previous locations, in case anyone was importing these functions directly. The change should have no backwards compatibility implications. Along the way I've also refactored the registration of functions for the config. Previously these ran as import-time side-effects, using the registry decorator. I've created instead a new module spacy.registrations. When the registry is accessed it calls a function ensure_populated(), which cases the registrations to occur. I've made a similar change to the Language.factory registrations in the new spacy.pipeline.factories module. I want to remove these import-time side-effects so that we can speed up the loading time of the library, which can be especially painful on the CLI. I also find that I'm often working to track down the implementations of functions referenced by strings in the config. Having the registrations all happen in one place will make this easier. With these changes I've fortunately avoided the need to migrate to Pydantic v2 properly --- we're still using the v1 compatibility shim. We might not be able to hold out forever though: Pydantic (reasonably) aren't actively supporting the v1 shims. I put a lot of work into v2 migration when investigating the 3.13 support, and it's definitely challenging. In any case, it's a relief that we don't have to do the v2 migration at the same time as the Cython 3.0/Python 3.13 support.
86 lines
3.8 KiB
Python
86 lines
3.8 KiB
Python
# coding: utf-8
|
|
"""Test factory import compatibility from original and new locations."""
|
|
|
|
import importlib
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"factory_name,original_module,compat_module",
|
|
[
|
|
("make_tagger", "spacy.pipeline.factories", "spacy.pipeline.tagger"),
|
|
("make_sentencizer", "spacy.pipeline.factories", "spacy.pipeline.sentencizer"),
|
|
("make_ner", "spacy.pipeline.factories", "spacy.pipeline.ner"),
|
|
("make_parser", "spacy.pipeline.factories", "spacy.pipeline.dep_parser"),
|
|
("make_tok2vec", "spacy.pipeline.factories", "spacy.pipeline.tok2vec"),
|
|
("make_spancat", "spacy.pipeline.factories", "spacy.pipeline.spancat"),
|
|
(
|
|
"make_spancat_singlelabel",
|
|
"spacy.pipeline.factories",
|
|
"spacy.pipeline.spancat",
|
|
),
|
|
("make_lemmatizer", "spacy.pipeline.factories", "spacy.pipeline.lemmatizer"),
|
|
("make_entity_ruler", "spacy.pipeline.factories", "spacy.pipeline.entityruler"),
|
|
("make_span_ruler", "spacy.pipeline.factories", "spacy.pipeline.span_ruler"),
|
|
(
|
|
"make_edit_tree_lemmatizer",
|
|
"spacy.pipeline.factories",
|
|
"spacy.pipeline.edit_tree_lemmatizer",
|
|
),
|
|
(
|
|
"make_attribute_ruler",
|
|
"spacy.pipeline.factories",
|
|
"spacy.pipeline.attributeruler",
|
|
),
|
|
(
|
|
"make_entity_linker",
|
|
"spacy.pipeline.factories",
|
|
"spacy.pipeline.entity_linker",
|
|
),
|
|
("make_textcat", "spacy.pipeline.factories", "spacy.pipeline.textcat"),
|
|
("make_token_splitter", "spacy.pipeline.factories", "spacy.pipeline.functions"),
|
|
("make_doc_cleaner", "spacy.pipeline.factories", "spacy.pipeline.functions"),
|
|
(
|
|
"make_morphologizer",
|
|
"spacy.pipeline.factories",
|
|
"spacy.pipeline.morphologizer",
|
|
),
|
|
("make_senter", "spacy.pipeline.factories", "spacy.pipeline.senter"),
|
|
("make_span_finder", "spacy.pipeline.factories", "spacy.pipeline.span_finder"),
|
|
(
|
|
"make_multilabel_textcat",
|
|
"spacy.pipeline.factories",
|
|
"spacy.pipeline.textcat_multilabel",
|
|
),
|
|
("make_beam_ner", "spacy.pipeline.factories", "spacy.pipeline.ner"),
|
|
("make_beam_parser", "spacy.pipeline.factories", "spacy.pipeline.dep_parser"),
|
|
("make_nn_labeller", "spacy.pipeline.factories", "spacy.pipeline.multitask"),
|
|
# This one's special because the function was named make_span_ruler, so
|
|
# the name in the registrations.py doesn't match the name we make the import hook
|
|
# point to. We could make a test just for this but shrug
|
|
# ("make_future_entity_ruler", "spacy.pipeline.factories", "spacy.pipeline.span_ruler"),
|
|
],
|
|
)
|
|
def test_factory_import_compatibility(factory_name, original_module, compat_module):
|
|
"""Test that factory functions can be imported from both original and compatibility locations."""
|
|
# Import from the original module (registrations.py)
|
|
original_module_obj = importlib.import_module(original_module)
|
|
original_factory = getattr(original_module_obj, factory_name)
|
|
assert (
|
|
original_factory is not None
|
|
), f"Could not import {factory_name} from {original_module}"
|
|
|
|
# Import from the compatibility module (component file)
|
|
compat_module_obj = importlib.import_module(compat_module)
|
|
compat_factory = getattr(compat_module_obj, factory_name)
|
|
assert (
|
|
compat_factory is not None
|
|
), f"Could not import {factory_name} from {compat_module}"
|
|
|
|
# Test that they're the same function (identity)
|
|
assert original_factory is compat_factory, (
|
|
f"Factory {factory_name} imported from {original_module} is not the same object "
|
|
f"as the one imported from {compat_module}"
|
|
)
|