From c3c064ace40f4f310d2d220b54b4dc0325a1e3ba Mon Sep 17 00:00:00 2001 From: Basile Dura Date: Fri, 2 Jun 2023 14:29:52 +0200 Subject: [PATCH] fix: `InitializableComponent` type hints (#12692) * fix: InitializableComponent type hints * fix: avoid circular dependency * style: clean imports in language.py * style: use relative imports Co-authored-by: Adriane Boyd * fix: apply black --------- Co-authored-by: Adriane Boyd --- spacy/language.py | 7 +++++-- spacy/ty.py | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spacy/language.py b/spacy/language.py index 289e6dd2c..0e9ff6893 100644 --- a/spacy/language.py +++ b/spacy/language.py @@ -1,6 +1,6 @@ from typing import Iterator, Optional, Any, Dict, Callable, Iterable from typing import Union, Tuple, List, Set, Pattern, Sequence -from typing import NoReturn, TYPE_CHECKING, TypeVar, cast, overload +from typing import NoReturn, TypeVar, cast, overload from dataclasses import dataclass import random @@ -1269,7 +1269,10 @@ class Language: "No 'get_examples' callback provided to 'Language.initialize', creating dummy examples" ) doc = Doc(self.vocab, words=["x", "y", "z"]) - get_examples = lambda: [Example.from_dict(doc, {})] + + def get_examples(): + return [Example.from_dict(doc, {})] + if not hasattr(get_examples, "__call__"): err = Errors.E930.format( method="Language.initialize", obj=type(get_examples) diff --git a/spacy/ty.py b/spacy/ty.py index 8f2903d78..7e79a3d4d 100644 --- a/spacy/ty.py +++ b/spacy/ty.py @@ -1,11 +1,13 @@ from typing import TYPE_CHECKING from typing import Optional, Any, Iterable, Dict, Callable, Sequence, List + from .compat import Protocol, runtime_checkable from thinc.api import Optimizer, Model if TYPE_CHECKING: from .training import Example + from .language import Language @runtime_checkable @@ -32,7 +34,7 @@ class InitializableComponent(Protocol): def initialize( self, get_examples: Callable[[], Iterable["Example"]], - nlp: Iterable["Example"], + nlp: "Language", **kwargs: Any ): ...