2023-05-11 08:27:58 +00:00
|
|
|
"""Test the BaseOutputParser class and its sub-classes."""
|
|
|
|
from abc import ABC
|
2023-06-26 21:16:14 +00:00
|
|
|
from collections import defaultdict
|
2023-05-11 08:27:58 +00:00
|
|
|
from typing import List, Optional, Set, Type
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from langchain.schema import BaseOutputParser
|
|
|
|
|
|
|
|
|
|
|
|
def non_abstract_subclasses(
|
|
|
|
cls: Type[ABC], to_skip: Optional[Set] = None
|
|
|
|
) -> List[Type]:
|
|
|
|
"""Recursively find all non-abstract subclasses of a class."""
|
|
|
|
_to_skip = to_skip or set()
|
|
|
|
subclasses = []
|
|
|
|
for subclass in cls.__subclasses__():
|
|
|
|
if not getattr(subclass, "__abstractmethods__", None):
|
|
|
|
if subclass.__name__ not in _to_skip:
|
|
|
|
subclasses.append(subclass)
|
|
|
|
subclasses.extend(non_abstract_subclasses(subclass, to_skip=_to_skip))
|
|
|
|
return subclasses
|
|
|
|
|
|
|
|
|
2023-06-18 23:55:18 +00:00
|
|
|
# parsers defined not in the output_parsers module:
|
|
|
|
_PARSERS_TO_SKIP = {
|
|
|
|
"FakeOutputParser",
|
|
|
|
"BaseOutputParser",
|
|
|
|
"FinishedOutputParser",
|
|
|
|
"RouterOutputParser",
|
2023-07-06 14:00:24 +00:00
|
|
|
"TrajectoryRunEvalOutputParser",
|
2023-06-18 23:55:18 +00:00
|
|
|
}
|
2023-05-11 08:27:58 +00:00
|
|
|
_NON_ABSTRACT_PARSERS = non_abstract_subclasses(
|
|
|
|
BaseOutputParser, to_skip=_PARSERS_TO_SKIP
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cls", _NON_ABSTRACT_PARSERS)
|
|
|
|
def test_subclass_implements_type(cls: Type[BaseOutputParser]) -> None:
|
|
|
|
try:
|
|
|
|
cls._type
|
|
|
|
except NotImplementedError:
|
|
|
|
pytest.fail(f"_type property is not implemented in class {cls.__name__}")
|
|
|
|
|
|
|
|
|
|
|
|
def test_all_subclasses_implement_unique_type() -> None:
|
2023-06-26 21:16:14 +00:00
|
|
|
types = defaultdict(list)
|
2023-05-11 08:27:58 +00:00
|
|
|
for cls in _NON_ABSTRACT_PARSERS:
|
|
|
|
try:
|
2023-06-26 21:16:14 +00:00
|
|
|
types[cls._type].append(cls.__name__)
|
2023-05-11 08:27:58 +00:00
|
|
|
except NotImplementedError:
|
|
|
|
# This is handled in the previous test
|
|
|
|
pass
|
2023-06-26 21:16:14 +00:00
|
|
|
dups = {t: names for t, names in types.items() if len(names) > 1}
|
2023-05-11 08:27:58 +00:00
|
|
|
assert not dups, f"Duplicate types: {dups}"
|