Skip to content

Commit

Permalink
Merge pull request #281 from evalott100/remove_import_pydantic_directly
Browse files Browse the repository at this point in the history
Fixed imports to avoid pydantic if the version is >=2
  • Loading branch information
evalott100 authored Jul 5, 2023
2 parents 5abfa58 + 9b8ed05 commit 2cbbf1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
20 changes: 19 additions & 1 deletion event_model/documents/generate/type_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

from dataclasses import dataclass

pydantic_version = None
try:
import pydantic
try:
from pydantic import v1 as pydantic # type: ignore
except ImportError:
import pydantic

pydantic_version = pydantic.__version__

Field = pydantic.Field
FieldInfo = pydantic.fields.FieldInfo
BaseConfig = pydantic.BaseConfig
BaseModel = pydantic.BaseModel
create_model = pydantic.create_model
except ModuleNotFoundError:

def Field(*args, **kwargs): # type: ignore
Expand All @@ -18,6 +27,15 @@ def Field(*args, **kwargs): # type: ignore
class FieldInfo: # type: ignore
...

class BaseConfig: # type: ignore
...

class BaseModel: # type: ignore
...

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


extra_schema = {}

Expand Down
13 changes: 11 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,24 @@
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 not pydantic_version:
raise EnvironmentError(
"schema generation requires pydantic < 2.0 to run, pydantic isn't installed"
)


SCHEMA_OUT_DIR = Path("event_model") / SCHEMA_PATH

Expand Down

0 comments on commit 2cbbf1e

Please sign in to comment.