Skip to content

Commit

Permalink
Support both pydantic 1 and 2
Browse files Browse the repository at this point in the history
Signed-off-by: junjie.jiang <junjie.jiang@zilliz.com>
  • Loading branch information
junjiejiangjjj committed Dec 1, 2023
1 parent e7ba2e6 commit 9366b50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ numpy
twine
tenacity
contextvars; python_version <= '3.6'
pydantic<2
pydantic
21 changes: 19 additions & 2 deletions towhee/runtime/check_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.

from typing import Dict, Any, Set, Tuple, Optional
from pydantic import BaseModel, constr, validator
import re
from pydantic import BaseModel, validator

from towhee.runtime.constants import (
WindowConst,
Expand All @@ -34,15 +35,31 @@ def must_larger_than_zero(cls, v):


class TupleForm(BaseModel):
"""Check schema name
"""
data: Optional[Tuple[str, ...]]
schema_data: Optional[Tuple[constr(regex='^[a-zA-Z_][a-zA-Z_0-9]*$'), ...]]
schema_data: Optional[Tuple[str, ...]]

@validator('*', pre=True)
def must_be_tuple(cls, v):
if isinstance(v, str):
return (v,)
return v

@validator('schema_data', pre=True)
def check_schema(cls, v):
def _check_pattern(v: Tuple[str]):
pattern = '^[a-zA-Z_][a-zA-Z_0-9]*$'
for item in v:
if re.match(pattern, item) is None:
raise ValueError(f'Schema can only consist of letters, numbers, and underscores, the error schema: [{item}]')

if isinstance(v, str):
_check_pattern((v, ))
elif isinstance(v, tuple):
_check_pattern(v)
return v


class SetForm(BaseModel):
data: Set[str]
Expand Down

0 comments on commit 9366b50

Please # to comment.