Skip to content

Commit

Permalink
fixbug: Optional not working
Browse files Browse the repository at this point in the history
  • Loading branch information
莘权 马 committed Aug 6, 2024
1 parent e199c6b commit 2846992
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions metagpt/actions/action_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,12 @@ def get_mapping(self, mode="children", exclude=None) -> Dict[str, Tuple[Type, An
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)

Expand Down Expand Up @@ -732,3 +725,12 @@ def from_pydantic(cls, model: Type[BaseModel], key: str = None):
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

0 comments on commit 2846992

Please sign in to comment.