MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _collect_annotation

Function _collect_annotation

lib/sqlalchemy/sql/_annotated_cols.py:360–391  ·  view source on GitHub ↗
(
    cls: type[Any], name: str, module: str, raw_annotation: _AnnotationScanType
)

Source from the content-addressed store, hash-verified

358
359@util.preload_module("sqlalchemy.sql.schema")
360def _collect_annotation(
361 cls: type[Any], name: str, module: str, raw_annotation: _AnnotationScanType
362) -> _AnnotationScanType | Literal[_NoArg.NO_ARG]:
363 Column = util.preloaded.sql_schema.Column
364
365 _locals = {"Column": Column, "Named": Named}
366 # _ClassScanAbstractConfig._collect_annotation & _extract_mapped_subtype
367 try:
368 annotation = sa_typing.de_stringify_annotation(
369 cls, raw_annotation, module, _locals
370 )
371 except Exception as e:
372 raise ArgumentError(
373 f"Could not interpret annotation {raw_annotation} for "
374 f"attribute '{cls.__name__}.{name}'"
375 ) from e
376
377 if (
378 not sa_typing.is_generic(annotation)
379 and isinstance(annotation, type)
380 and issubclass(annotation, (Column, Named))
381 ):
382 # no generic information, ignore
383 return _NoArg.NO_ARG
384 elif not sa_typing.is_origin_of_cls(annotation, (Column, Named)):
385 raise ArgumentError(
386 f"Annotation {raw_annotation} for attribute "
387 f"'{cls.__name__}.{name}' is not of type Named/Column[...]"
388 )
389 else:
390 assert len(annotation.__args__) == 1 # Column[int, int] raises
391 return annotation.__args__[0] # type: ignore[no-any-return]
392
393
394def _get_sqltype(annotation: _AnnotationScanType) -> TypeEngine[Any] | None:

Callers 1

Calls 1

ArgumentErrorClass · 0.85

Tested by

no test coverage detected