mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
fix: Add missing @classmethod decorators to field validators
- Add @classmethod decorator to validate_file_path in base_file_knowledge_source.py - Add @classmethod decorator to validate_file_path in excel_knowledge_source.py - Follows existing codebase pattern for Pydantic field validators (N805) Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Optional, Union
|
|
||||||
|
|
||||||
from pydantic import Field, field_validator
|
from pydantic import Field, field_validator
|
||||||
|
|
||||||
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
|
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
|
||||||
from crewai.knowledge.storage.knowledge_storage import KnowledgeStorage
|
from crewai.knowledge.storage.knowledge_storage import KnowledgeStorage
|
||||||
from crewai.utilities.constants import KNOWLEDGE_DIRECTORY
|
|
||||||
from crewai.utilities.logger import Logger
|
from crewai.utilities.logger import Logger
|
||||||
from crewai.utilities.paths import get_knowledge_directory
|
from crewai.utilities.paths import get_knowledge_directory
|
||||||
|
|
||||||
@@ -15,18 +13,19 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
|
|||||||
"""Base class for knowledge sources that load content from files."""
|
"""Base class for knowledge sources that load content from files."""
|
||||||
|
|
||||||
_logger: Logger = Logger(verbose=True)
|
_logger: Logger = Logger(verbose=True)
|
||||||
file_path: Optional[Union[Path, List[Path], str, List[str]]] = Field(
|
file_path: Path | list[Path] | str | list[str] | None = Field(
|
||||||
default=None,
|
default=None,
|
||||||
description="[Deprecated] The path to the file. Use file_paths instead.",
|
description="[Deprecated] The path to the file. Use file_paths instead.",
|
||||||
)
|
)
|
||||||
file_paths: Optional[Union[Path, List[Path], str, List[str]]] = Field(
|
file_paths: Path | list[Path] | str | list[str] | None = Field(
|
||||||
default_factory=list, description="The path to the file"
|
default_factory=list, description="The path to the file"
|
||||||
)
|
)
|
||||||
content: Dict[Path, str] = Field(init=False, default_factory=dict)
|
content: dict[Path, str] = Field(init=False, default_factory=dict)
|
||||||
storage: Optional[KnowledgeStorage] = Field(default=None)
|
storage: KnowledgeStorage | None = Field(default=None)
|
||||||
safe_file_paths: List[Path] = Field(default_factory=list)
|
safe_file_paths: list[Path] = Field(default_factory=list)
|
||||||
|
|
||||||
@field_validator("file_path", "file_paths", mode="before")
|
@field_validator("file_path", "file_paths", mode="before")
|
||||||
|
@classmethod
|
||||||
def validate_file_path(cls, v, info):
|
def validate_file_path(cls, v, info):
|
||||||
"""Validate that at least one of file_path or file_paths is provided."""
|
"""Validate that at least one of file_path or file_paths is provided."""
|
||||||
# Single check if both are None, O(1) instead of nested conditions
|
# Single check if both are None, O(1) instead of nested conditions
|
||||||
@@ -47,9 +46,8 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
|
|||||||
self.content = self.load_content()
|
self.content = self.load_content()
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def load_content(self) -> Dict[Path, str]:
|
def load_content(self) -> dict[Path, str]:
|
||||||
"""Load and preprocess file content. Should be overridden by subclasses. Assume that the file path is relative to the project root in the knowledge directory."""
|
"""Load and preprocess file content. Should be overridden by subclasses. Assume that the file path is relative to the project root in the knowledge directory."""
|
||||||
pass
|
|
||||||
|
|
||||||
def validate_content(self):
|
def validate_content(self):
|
||||||
"""Validate the paths."""
|
"""Validate the paths."""
|
||||||
@@ -75,11 +73,11 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
|
|||||||
else:
|
else:
|
||||||
raise ValueError("No storage found to save documents.")
|
raise ValueError("No storage found to save documents.")
|
||||||
|
|
||||||
def convert_to_path(self, path: Union[Path, str]) -> Path:
|
def convert_to_path(self, path: Path | str) -> Path:
|
||||||
"""Convert a path to a Path object."""
|
"""Convert a path to a Path object."""
|
||||||
return Path(get_knowledge_directory() + "/" + path) if isinstance(path, str) else path
|
return Path(get_knowledge_directory() + "/" + path) if isinstance(path, str) else path
|
||||||
|
|
||||||
def _process_file_paths(self) -> List[Path]:
|
def _process_file_paths(self) -> list[Path]:
|
||||||
"""Convert file_path to a list of Path objects."""
|
"""Convert file_path to a list of Path objects."""
|
||||||
|
|
||||||
if hasattr(self, "file_path") and self.file_path is not None:
|
if hasattr(self, "file_path") and self.file_path is not None:
|
||||||
@@ -94,7 +92,7 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
|
|||||||
raise ValueError("Your source must be provided with a file_paths: []")
|
raise ValueError("Your source must be provided with a file_paths: []")
|
||||||
|
|
||||||
# Convert single path to list
|
# Convert single path to list
|
||||||
path_list: List[Union[Path, str]] = (
|
path_list: list[Path | str] = (
|
||||||
[self.file_paths]
|
[self.file_paths]
|
||||||
if isinstance(self.file_paths, (str, Path))
|
if isinstance(self.file_paths, (str, Path))
|
||||||
else list(self.file_paths)
|
else list(self.file_paths)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class ExcelKnowledgeSource(BaseKnowledgeSource):
|
|||||||
safe_file_paths: list[Path] = Field(default_factory=list)
|
safe_file_paths: list[Path] = Field(default_factory=list)
|
||||||
|
|
||||||
@field_validator("file_path", "file_paths", mode="before")
|
@field_validator("file_path", "file_paths", mode="before")
|
||||||
|
@classmethod
|
||||||
def validate_file_path(cls, v, info):
|
def validate_file_path(cls, v, info):
|
||||||
"""Validate that at least one of file_path or file_paths is provided."""
|
"""Validate that at least one of file_path or file_paths is provided."""
|
||||||
# Single check if both are None, O(1) instead of nested conditions
|
# Single check if both are None, O(1) instead of nested conditions
|
||||||
|
|||||||
Reference in New Issue
Block a user