diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-test.yml similarity index 93% rename from .github/workflows/python-package.yml rename to .github/workflows/python-test.yml index de17238..aef64c9 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-test.yml @@ -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: @@ -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 diff --git a/Changelog.md b/Changelog.md index 5d676c0..9f802c1 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 ## 移除特性 diff --git a/schema_entry/entrypoint.py b/schema_entry/entrypoint.py index 356c5dc..15ac51c 100644 --- a/schema_entry/entrypoint.py +++ b/schema_entry/entrypoint.py @@ -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 @@ -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 diff --git a/schema_entry/entrypoint_base.py b/schema_entry/entrypoint_base.py index 63c4487..25c80c1 100644 --- a/schema_entry/entrypoint_base.py +++ b/schema_entry/entrypoint_base.py @@ -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, diff --git a/schema_entry/utils.py b/schema_entry/utils.py index 1b88d46..3277482 100644 --- a/schema_entry/utils.py +++ b/schema_entry/utils.py @@ -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: @@ -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"): @@ -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"): @@ -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)