Skip to content

Commit

Permalink
Fixed imports to avoid pydantic if the version is >=2
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Jul 5, 2023
1 parent 34a4eb6 commit 2ba5eab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
32 changes: 28 additions & 4 deletions event_model/documents/generate/type_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,43 @@
"""


import pkgutil
from dataclasses import dataclass

try:
Field = None
FieldInfo = None
BaseConfig = None
BaseModel = None
create_model = None

pydantic_path = pkgutil.find_loader("pydantic")
pydantic_version = None
if pydantic_path:
import pydantic

pydantic_version = pydantic.__version__

if pydantic_version and pydantic_version < "2.0":
Field = pydantic.Field
FieldInfo = pydantic.fields.FieldInfo
except ModuleNotFoundError:
BaseConfig = pydantic.BaseConfig
BaseModel = pydantic.BaseModel
create_model = pydantic.create_model
else:

def Field(*args, **kwargs):
...

class FieldInfo:
...

class BaseConfig:
...

def Field(*args, **kwargs): # type: ignore
class BaseModel:
...

class FieldInfo: # type: ignore
def create_model(*args, **kwargs):
...


Expand Down
10 changes: 8 additions & 2 deletions event_model/documents/generate/typeddict_to_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from pathlib import Path
from typing import Dict, Optional, Tuple, Type, Union

from pydantic import BaseConfig, BaseModel, Field, create_model
from pydantic.fields import FieldInfo
from typing_extensions import (
Annotated,
NotRequired,
Expand All @@ -33,13 +31,21 @@
from event_model.documents.generate.type_wrapper import (
ALLOWED_ANNOTATION_ELEMENTS,
AsRef,
BaseConfig,
BaseModel,
Field,
FieldInfo,
create_model,
extra_schema,
pydantic_version,
)

# The hacky indexing on types isn't possible with python < 3.9
if sys.version_info[:2] < (3, 9):
raise EnvironmentError("schema generation requires python 3.8 or higher")

if pydantic_version and pydantic_version >= "2.0":
raise EnvironmentError(f"schema generation requires pydantic < 2.0 to run, current version {pydantic_version}")

SCHEMA_OUT_DIR = Path("event_model") / SCHEMA_PATH

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ dev = [
"numpydoc",

# For schema generation.
"pydantic",
"pydantic<2.0",
]

[project.scripts]
Expand Down

0 comments on commit 2ba5eab

Please sign in to comment.