Deep copy the given ClauseElement, annotating each element with the given annotations dictionary. Elements within the exclude collection will be cloned but not annotated.
(
element: _SA,
annotations: _AnnotationDict,
exclude: Optional[Sequence[SupportsAnnotations]] = None,
*,
detect_subquery_cols: bool = False,
ind_cols_on_fromclause: bool = False,
annotate_callable: Optional[
Callable[[SupportsAnnotations, _AnnotationDict], SupportsAnnotations]
] = None,
)
| 411 | |
| 412 | |
| 413 | def _deep_annotate( |
| 414 | element: _SA, |
| 415 | annotations: _AnnotationDict, |
| 416 | exclude: Optional[Sequence[SupportsAnnotations]] = None, |
| 417 | *, |
| 418 | detect_subquery_cols: bool = False, |
| 419 | ind_cols_on_fromclause: bool = False, |
| 420 | annotate_callable: Optional[ |
| 421 | Callable[[SupportsAnnotations, _AnnotationDict], SupportsAnnotations] |
| 422 | ] = None, |
| 423 | ) -> _SA: |
| 424 | """Deep copy the given ClauseElement, annotating each element |
| 425 | with the given annotations dictionary. |
| 426 | |
| 427 | Elements within the exclude collection will be cloned but not annotated. |
| 428 | |
| 429 | """ |
| 430 | |
| 431 | # annotated objects hack the __hash__() method so if we want to |
| 432 | # uniquely process them we have to use id() |
| 433 | |
| 434 | cloned_ids: Dict[int, SupportsAnnotations] = {} |
| 435 | |
| 436 | def clone(elem: SupportsAnnotations, **kw: Any) -> SupportsAnnotations: |
| 437 | # ind_cols_on_fromclause means make sure an AnnotatedFromClause |
| 438 | # has its own .c collection independent of that which its proxying. |
| 439 | # this is used specifically by orm.LoaderCriteriaOption to break |
| 440 | # a reference cycle that it's otherwise prone to building, |
| 441 | # see test_relationship_criteria-> |
| 442 | # test_loader_criteria_subquery_w_same_entity. logic here was |
| 443 | # changed for #8796 and made explicit; previously it occurred |
| 444 | # by accident |
| 445 | |
| 446 | kw["detect_subquery_cols"] = detect_subquery_cols |
| 447 | id_ = id(elem) |
| 448 | |
| 449 | if id_ in cloned_ids: |
| 450 | return cloned_ids[id_] |
| 451 | |
| 452 | if ( |
| 453 | exclude |
| 454 | and hasattr(elem, "proxy_set") |
| 455 | and elem.proxy_set.intersection(exclude) |
| 456 | ): |
| 457 | newelem = elem._clone(clone=clone, **kw) |
| 458 | elif annotations != elem._annotations: |
| 459 | if detect_subquery_cols and elem._is_immutable: |
| 460 | to_annotate = elem._clone(clone=clone, **kw) |
| 461 | else: |
| 462 | to_annotate = elem |
| 463 | if annotate_callable: |
| 464 | newelem = annotate_callable(to_annotate, annotations) |
| 465 | else: |
| 466 | newelem = _safe_annotate(to_annotate, annotations) |
| 467 | else: |
| 468 | newelem = elem |
| 469 | |
| 470 | newelem._copy_internals( |