Skip to content

Commit

Permalink
move pydantic conditional import to compat module (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabir Khan authored Aug 31, 2023
1 parent d95fe5a commit 4602c99
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 22 deletions.
10 changes: 10 additions & 0 deletions spacy_llm/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@
except ImportError:
accelerate = None
has_accelerate = False


from pydantic import VERSION

PYDANTIC_V2 = VERSION.startswith("2.")

if PYDANTIC_V2:
from pydantic.v1 import BaseModel, ValidationError, validator # noqa: F401
else:
from pydantic import BaseModel, ValidationError, validator # noqa: F401
6 changes: 1 addition & 5 deletions spacy_llm/tasks/rel/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
from spacy.tokens import Doc
from wasabi import msg

try:
from pydantic.v1 import ValidationError
except ImportError:
from pydantic import ValidationError

from ...compat import ValidationError
from .task import RELTask
from .util import RelationItem

Expand Down
10 changes: 2 additions & 8 deletions spacy_llm/tasks/rel/util.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from typing import List

try:
from pydantic.v1 import BaseModel, validator
except ImportError:
from pydantic import BaseModel, validator

from spacy.training import Example

from ...compat import Self
from ...compat import BaseModel, Self, validator
from ...ty import FewshotExample


Expand Down Expand Up @@ -47,8 +42,7 @@ def generate(cls, example: Example, **kwargs) -> Self:
)
for ent in example.reference.ents
]

return cls.construct(
return cls(
text=example.reference.text,
ents=entities,
relations=example.reference._.rel,
Expand Down
2 changes: 1 addition & 1 deletion spacy_llm/tasks/span/examples.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import abc
from typing import Dict, Iterable, List

from pydantic import BaseModel
from spacy.tokens import Span

from ...compat import BaseModel
from ...ty import FewshotExample, Self


Expand Down
3 changes: 1 addition & 2 deletions spacy_llm/tasks/summarization/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pydantic import BaseModel
from spacy.training import Example

from ...compat import Self
from ...compat import BaseModel, Self


class SummarizationExample(BaseModel):
Expand Down
3 changes: 1 addition & 2 deletions spacy_llm/tasks/textcat/util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import Any, Dict, Iterable

from pydantic import BaseModel
from spacy.scorer import Scorer
from spacy.training import Example

from ...compat import Self
from ...compat import BaseModel, Self


class TextCatExample(BaseModel):
Expand Down
3 changes: 1 addition & 2 deletions spacy_llm/tests/tasks/test_ner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import spacy
import srsly
from confection import Config
from pydantic import ValidationError
from spacy.language import Language
from spacy.tokens import Span
from spacy.training import Example
from spacy.util import make_tempdir

from spacy_llm.compat import Literal
from spacy_llm.compat import Literal, ValidationError
from spacy_llm.pipeline import LLMWrapper
from spacy_llm.registry import fewshot_reader, file_reader, lowercase_normalizer
from spacy_llm.registry import strip_normalizer
Expand Down
3 changes: 1 addition & 2 deletions spacy_llm/ty.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, TypeVar
from typing import Union, cast

from pydantic import BaseModel
from spacy.tokens import Doc
from spacy.training.example import Example
from spacy.vocab import Vocab

from .compat import Protocol, Self, runtime_checkable
from .compat import BaseModel, Protocol, Self, runtime_checkable
from .models import langchain

_PromptType = Any
Expand Down

0 comments on commit 4602c99

Please sign in to comment.