(
name: str,
rel_info: "RelationshipInfo",
annotation: Any,
)
| 120 | |
| 121 | |
| 122 | def get_relationship_to( |
| 123 | name: str, |
| 124 | rel_info: "RelationshipInfo", |
| 125 | annotation: Any, |
| 126 | ) -> Any: |
| 127 | origin = get_origin(annotation) |
| 128 | use_annotation = annotation |
| 129 | # Direct relationships (e.g. 'Team' or Team) have None as an origin |
| 130 | if origin is None: |
| 131 | if isinstance(use_annotation, ForwardRef): |
| 132 | use_annotation = use_annotation.__forward_arg__ |
| 133 | else: |
| 134 | return use_annotation |
| 135 | # If Union (e.g. Optional), get the real field |
| 136 | elif _is_union_type(origin): |
| 137 | use_annotation = get_args(annotation) |
| 138 | if len(use_annotation) > 2: |
| 139 | raise ValueError("Cannot have a (non-optional) union as a SQLAlchemy field") |
| 140 | arg1, arg2 = use_annotation |
| 141 | if arg1 is NoneType and arg2 is not NoneType: |
| 142 | use_annotation = arg2 |
| 143 | elif arg2 is NoneType and arg1 is not NoneType: |
| 144 | use_annotation = arg1 |
| 145 | else: |
| 146 | raise ValueError( |
| 147 | "Cannot have a Union of None and None as a SQLAlchemy field" |
| 148 | ) |
| 149 | |
| 150 | # If a list, then also get the real field |
| 151 | elif origin is list: |
| 152 | use_annotation = get_args(annotation)[0] |
| 153 | |
| 154 | return get_relationship_to(name=name, rel_info=rel_info, annotation=use_annotation) |
| 155 | |
| 156 | |
| 157 | def is_field_noneable(field: "FieldInfo") -> bool: |
no test coverage detected
searching dependent graphs…