Spaces:
Paused
Paused
Update Modules/_docstrings.py
Browse files1234569 unmodified lines161718192021222324from __future__ import annotationsimport inspectfrom typing import Any, Annotated, get_args, get_origin, get_type_hints9 unmodified linesreturn str(tp)def _extract_base_and_meta(annotation: Any) -> tuple[Any, str | None]:"""Given an annotation, return (base_type, first string metadata) if Annotated, else (annotation, None)."""try:if get_origin(annotation) is Annotated:args = get_args(annotation)base = args[0] if args else annotation12345679 unmodified lines171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061from __future__ import annotationsimport inspectimport refrom typing import Any, Annotated, get_args, get_origin, get_type_hints9 unmodified linesreturn str(tp)def _parse_string_annotation(annot_str: str) -> tuple[str | None, str | None]:"""Parse a string annotation like "Annotated[Optional[str], 'description']"and extract the base type name and the description metadata.Returns (base_type_name, description) or (None, None) if parsing fails."""if not isinstance(annot_str, str):return None, None# Match Annotated[..., 'description'] or Annotated[..., "description"]# Pattern: Annotated[<base_type>, '<description>'] or with double quotesmatch = re.match(r"^Annotated\[(.+?),\s*['\"](.+?)['\"]\s*\]$",annot_str.strip(),re.DOTALL,)if match:base_type_str = match.group(1).strip()description = match.group(2)# Simplify Optional[X] -> just the base type for displayopt_match = re.match(r"^Optional\[(.+)\]$", base_type_str)if opt_match:base_type_str = opt_match.group(1).strip()return base_type_str, descriptionreturn None, Nonedef _extract_base_and_meta(annotation: Any) -> tuple[Any, str | None]:"""Given an annotation, return (base_type, first string metadata) if Annotated, else (annotation, None)."""try:# Handle string annotations from PEP 563 (__future__.annotations)if isinstance(annotation, str):base_str, meta = _parse_string_annotation(annotation)if meta:return base_str or annotation, metareturn annotation, Noneif get_origin(annotation) is Annotated:args = get_args(annotation)base = args[0] if args else annotation
Modules/_docstrings.py+37