|
9 | 9 | from io import BytesIO
|
10 | 10 | from os import PathLike
|
11 | 11 | from pathlib import Path
|
12 |
| -from typing import Any, Callable, Dict, List, Optional, Tuple, Type, TypeAlias |
| 12 | +from typing import ( |
| 13 | + Any, |
| 14 | + Callable, |
| 15 | + Dict, |
| 16 | + List, |
| 17 | + Optional, |
| 18 | + Tuple, |
| 19 | + Type, |
| 20 | + TypeAlias, |
| 21 | + Union, |
| 22 | + get_args, |
| 23 | + get_origin, |
| 24 | +) |
13 | 25 |
|
14 | 26 | import numpy as np
|
15 | 27 | import yaml
|
@@ -166,6 +178,12 @@ class FrontMatterParser(PipelineStep):
|
166 | 178 |
|
167 | 179 | front_matter_class: Optional[Type] = None
|
168 | 180 |
|
| 181 | + def _is_optional_str(self, field_type: Type) -> bool: |
| 182 | + """Check if a type is Optional[str].""" |
| 183 | + origin = get_origin(field_type) |
| 184 | + args = get_args(field_type) |
| 185 | + return origin is Union and type(None) in args and str in args |
| 186 | + |
169 | 187 | def _extract_front_matter_and_text(self, markdown_content: str) -> tuple[Dict[str, Any], str]:
|
170 | 188 | """Extract YAML front matter and text from markdown content."""
|
171 | 189 | if markdown_content.startswith("---\n"):
|
@@ -210,8 +228,14 @@ def _parse_front_matter(self, front_matter_dict: Dict[str, Any], text: str) -> A
|
210 | 228 | kwargs[field_name] = int(value)
|
211 | 229 | elif field_type is bool and isinstance(value, str):
|
212 | 230 | kwargs[field_name] = value.lower() == "true"
|
213 |
| - elif field_type is Optional[str]: |
214 |
| - kwargs[field_name] = value if value else None |
| 231 | + elif self._is_optional_str(field_type): |
| 232 | + # Handle boolean values that YAML might produce (e.g., 'no' -> False) |
| 233 | + if isinstance(value, bool): |
| 234 | + kwargs[field_name] = None |
| 235 | + elif isinstance(value, str): |
| 236 | + kwargs[field_name] = value if value else None |
| 237 | + else: |
| 238 | + kwargs[field_name] = None if not value else value |
215 | 239 | else:
|
216 | 240 | kwargs[field_name] = value
|
217 | 241 |
|
|
0 commit comments