mirror of
https://github.com/explosion/spaCy.git
synced 2025-06-26 16:03:05 +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.
98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
import inspect
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from spacy.language import Language
|
|
from spacy.util import registry
|
|
|
|
# Path to the reference factory registrations, relative to this file
|
|
REFERENCE_FILE = Path(__file__).parent / "factory_registrations.json"
|
|
|
|
# Monkey patch the util.is_same_func to handle Cython functions
|
|
import inspect
|
|
|
|
from spacy import util
|
|
|
|
original_is_same_func = util.is_same_func
|
|
|
|
|
|
def patched_is_same_func(func1, func2):
|
|
# Handle Cython functions
|
|
try:
|
|
return original_is_same_func(func1, func2)
|
|
except TypeError:
|
|
# For Cython functions, just compare the string representation
|
|
return str(func1) == str(func2)
|
|
|
|
|
|
util.is_same_func = patched_is_same_func
|
|
|
|
|
|
@pytest.fixture
|
|
def reference_factory_registrations():
|
|
"""Load reference factory registrations from JSON file"""
|
|
if not REFERENCE_FILE.exists():
|
|
pytest.fail(
|
|
f"Reference file {REFERENCE_FILE} not found. Run export_factory_registrations.py first."
|
|
)
|
|
|
|
with REFERENCE_FILE.open("r") as f:
|
|
return json.load(f)
|
|
|
|
|
|
def test_factory_registrations_preserved(reference_factory_registrations):
|
|
"""Test that all factory registrations from the reference file are still present."""
|
|
# Ensure the registry is populated
|
|
registry.ensure_populated()
|
|
|
|
# Get all factory registrations
|
|
all_factories = registry.factories.get_all()
|
|
|
|
# Initialize our data structure to store current factory registrations
|
|
current_registrations = {}
|
|
|
|
# Process factory registrations
|
|
for name, func in all_factories.items():
|
|
# Store information about each factory
|
|
try:
|
|
module_name = func.__module__
|
|
except (AttributeError, TypeError):
|
|
# For Cython functions, just use a placeholder
|
|
module_name = str(func).split()[1].split(".")[0]
|
|
|
|
try:
|
|
func_name = func.__qualname__
|
|
except (AttributeError, TypeError):
|
|
# For Cython functions, use the function's name
|
|
func_name = (
|
|
func.__name__
|
|
if hasattr(func, "__name__")
|
|
else str(func).split()[1].split(".")[-1]
|
|
)
|
|
|
|
current_registrations[name] = {
|
|
"name": name,
|
|
"module": module_name,
|
|
"function": func_name,
|
|
}
|
|
|
|
# Check for missing registrations
|
|
missing_registrations = set(reference_factory_registrations.keys()) - set(
|
|
current_registrations.keys()
|
|
)
|
|
assert (
|
|
not missing_registrations
|
|
), f"Missing factory registrations: {', '.join(sorted(missing_registrations))}"
|
|
|
|
# Check for new registrations (not an error, but informative)
|
|
new_registrations = set(current_registrations.keys()) - set(
|
|
reference_factory_registrations.keys()
|
|
)
|
|
if new_registrations:
|
|
# This is not an error, just informative
|
|
print(
|
|
f"New factory registrations found: {', '.join(sorted(new_registrations))}"
|
|
)
|