(annotation: Any)
| 247 | |
| 248 | |
| 249 | def _annotation_to_schema(annotation: Any) -> Dict[str, Any]: |
| 250 | if annotation is inspect._empty or annotation is Any: |
| 251 | return {"type": "string"} |
| 252 | |
| 253 | origin = get_origin(annotation) |
| 254 | if origin is None: |
| 255 | return _primitive_schema(annotation) |
| 256 | |
| 257 | if origin is list or origin is List or origin is abc.Sequence or origin is abc.MutableSequence: |
| 258 | item_annotation = get_args(annotation)[0] if get_args(annotation) else Any |
| 259 | return { |
| 260 | "type": "array", |
| 261 | "items": _annotation_to_schema(item_annotation), |
| 262 | } |
| 263 | |
| 264 | if origin in {dict, Dict, abc.Mapping, abc.MutableMapping}: |
| 265 | return {"type": "object"} |
| 266 | |
| 267 | if origin is Union: |
| 268 | literals = [arg for arg in get_args(annotation) if arg is not type(None)] # noqa: E721 |
| 269 | literal_schema = _try_literal_schema(literals) |
| 270 | if literal_schema: |
| 271 | return literal_schema |
| 272 | return {"type": "string"} |
| 273 | |
| 274 | if origin is Literal: |
| 275 | values = list(get_args(annotation)) |
| 276 | return _literal_schema(values) |
| 277 | |
| 278 | return {"type": "string"} |
| 279 | |
| 280 | |
| 281 | def _primitive_schema(annotation: Any) -> Dict[str, Any]: |
no test coverage detected