Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz1273327 committed Jun 18, 2024
1 parent 7f17c93 commit 0bd6463
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package
name: Python Test

on:
push:
Expand All @@ -25,7 +25,7 @@ jobs:
- name: Install devDependence
run: |
python -m pip install --upgrade pip
pip install mypy pycodestyle coverage lxml
pip install mypy pycodestyle coverage lxml types-PyYAML pydantic
- name: Install dependencies
run: |
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
Expand Down
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

1. 提供一个默认的返回config的main而不是提示没有注册main
2. 允许main返回任意值,这个值会在调用__call__时返回
3. 增加方法`with_schema`用于在实例化后注册一个schema,支持json字符串,dict和pydantic的类三种形式,注意,pydantic的类不支持嵌套,不支持Union,不支持Optional,但可以将`with_schema`作为装饰器使用,且节点将会使用被装饰的类名作为节点名,类docstring作为description
3. 增加方法`with_schema`用于在实例化后注册一个schema,支持json字符串,dict和pydantic的类三种形式,注意,pydantic的类不支持嵌套,不支持Union,不支持Optional,但可以将`with_schema`作为装饰器使用,且节点将会使用被装饰的类名小写作为节点名,类docstring作为description

## 移除特性

Expand Down
9 changes: 5 additions & 4 deletions schema_entry/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import functools
from copy import deepcopy
from pathlib import Path
from typing import Callable, Sequence, Dict, List, Any, Tuple, Optional, Union
from typing import Callable, Sequence, Dict, List, Any, Tuple, Optional, Union, cast
from jsonschema import validate
from yaml import load as yaml_load

Expand Down Expand Up @@ -176,11 +176,12 @@ def with_schema(self, schemaObj: Union[str, dict, PydanticModelLike]) -> Union[s
if isinstance(schemaObj, str):
self.schema = json.loads(schemaObj)
elif isinstance(schemaObj, dict):
self.schema = schemaObj
schemaObjSchemaType = cast(SchemaType, schemaObj)
self.schema = schemaObjSchemaType
else:
schema = schemaObj.model_json_schema()
schema = cast(SchemaType, schemaObj.model_json_schema())
self.schema = pydantic_schema_to_protocol(schema)
self._name = schemaObj.__name__
self._name = schemaObj.__name__.lower()
self.__doc__ = schemaObj.__doc__
return schemaObj

Expand Down
2 changes: 1 addition & 1 deletion schema_entry/entrypoint_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class PydanticModelLike(Protocol):
def model_json_schema(
def model_json_schema(self,
by_alias: bool,
ref_template: str,
schema_generator: Any,
Expand Down
10 changes: 5 additions & 5 deletions schema_entry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def parse_schema_as_cmd(key: str, schema: PropertyType, parser: argparse.Argumen
return parser


def _remove_sigal_allOf(x):
def _remove_sigal_allOf(x: Dict[str, Any]) -> Dict[str, Any]:
info = {}
if x.get("allOf"):
if len(x["allOf"]) == 1:
Expand All @@ -232,14 +232,14 @@ def _remove_sigal_allOf(x):
return x


def remove_sigal_allOf(d):
def remove_sigal_allOf(d: Dict[str, Any]) -> Dict[str, Any]:
for key, value in d.get('properties').items():
info = _remove_sigal_allOf(value)
d['properties'][key] = info
return d


def remove_defs_interference(d):
def remove_defs_interference(d: Dict[str, Any]) -> Dict[str, Any]:
if d.get("$defs"):
for key, value in d["$defs"].items():
if value.get("title"):
Expand All @@ -249,7 +249,7 @@ def remove_defs_interference(d):
return d


def replace_refs(d):
def replace_refs(d: Dict[str, Any]) -> Dict[str, Any]:
info = jsonref.replace_refs(d)
print(info)
if info.get("$defs"):
Expand All @@ -258,7 +258,7 @@ def replace_refs(d):
return info


def pydantic_schema_to_protocol(schema: dict) -> dict:
def pydantic_schema_to_protocol(schema: Dict[str, Any]) -> Dict[str, Any]:
schema = remove_sigal_allOf(schema)
schema = remove_defs_interference(schema)
schema = replace_refs(schema)
Expand Down

0 comments on commit 0bd6463

Please sign in to comment.