mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-02 14:45:17 +02:00
fixbug: Optional not working
This commit is contained in:
parent
e199c6b476
commit
284699224e
1 changed files with 10 additions and 8 deletions
|
|
@ -236,19 +236,12 @@ class ActionNode:
|
|||
def create_model_class(cls, class_name: str, mapping: Dict[str, Tuple[Type, Any]]):
|
||||
"""基于pydantic v2的模型动态生成,用来检验结果类型正确性"""
|
||||
|
||||
def is_optional_type(tp):
|
||||
if typing.get_origin(tp) is Union:
|
||||
args = typing.get_args(tp)
|
||||
non_none_types = [arg for arg in args if arg is not type(None)]
|
||||
return len(non_none_types) == 1 and len(args) == 2
|
||||
return False
|
||||
|
||||
def check_fields(cls, values):
|
||||
all_fields = set(mapping.keys())
|
||||
required_fields = set()
|
||||
for k, v in mapping.items():
|
||||
type_v, field_info = v
|
||||
if is_optional_type(type_v):
|
||||
if ActionNode.is_optional_type(type_v):
|
||||
continue
|
||||
required_fields.add(k)
|
||||
|
||||
|
|
@ -732,3 +725,12 @@ class ActionNode:
|
|||
root_node.add_child(child_node)
|
||||
|
||||
return root_node
|
||||
|
||||
@staticmethod
|
||||
def is_optional_type(tp) -> bool:
|
||||
"""Return True if `tp` is `typing.Optional[...]`"""
|
||||
if typing.get_origin(tp) is Union:
|
||||
args = typing.get_args(tp)
|
||||
non_none_types = [arg for arg in args if arg is not type(None)]
|
||||
return len(non_none_types) == 1 and len(args) == 2
|
||||
return False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue