| 2026 | |
| 2027 | |
| 2028 | class _DeferredMapperConfig(_ClassScanMapperConfig): |
| 2029 | _cls: weakref.ref[Type[Any]] |
| 2030 | |
| 2031 | is_deferred = True |
| 2032 | |
| 2033 | _configs: util.OrderedDict[ |
| 2034 | weakref.ref[Type[Any]], _DeferredMapperConfig |
| 2035 | ] = util.OrderedDict() |
| 2036 | |
| 2037 | def _early_mapping(self, mapper_kw: _MapperKwArgs) -> None: |
| 2038 | pass |
| 2039 | |
| 2040 | @property |
| 2041 | def cls(self) -> Type[Any]: |
| 2042 | return self._cls() # type: ignore |
| 2043 | |
| 2044 | @cls.setter |
| 2045 | def cls(self, class_: Type[Any]) -> None: |
| 2046 | self._cls = weakref.ref(class_, self._remove_config_cls) |
| 2047 | self._configs[self._cls] = self |
| 2048 | |
| 2049 | @classmethod |
| 2050 | def _remove_config_cls(cls, ref: weakref.ref[Type[Any]]) -> None: |
| 2051 | cls._configs.pop(ref, None) |
| 2052 | |
| 2053 | @classmethod |
| 2054 | def has_cls(cls, class_: Type[Any]) -> bool: |
| 2055 | # 2.6 fails on weakref if class_ is an old style class |
| 2056 | return isinstance(class_, type) and weakref.ref(class_) in cls._configs |
| 2057 | |
| 2058 | @classmethod |
| 2059 | def raise_unmapped_for_cls(cls, class_: Type[Any]) -> NoReturn: |
| 2060 | if hasattr(class_, "_sa_raise_deferred_config"): |
| 2061 | class_._sa_raise_deferred_config() |
| 2062 | |
| 2063 | raise orm_exc.UnmappedClassError( |
| 2064 | class_, |
| 2065 | msg=( |
| 2066 | f"Class {orm_exc._safe_cls_name(class_)} has a deferred " |
| 2067 | "mapping on it. It is not yet usable as a mapped class." |
| 2068 | ), |
| 2069 | ) |
| 2070 | |
| 2071 | @classmethod |
| 2072 | def config_for_cls(cls, class_: Type[Any]) -> _DeferredMapperConfig: |
| 2073 | return cls._configs[weakref.ref(class_)] |
| 2074 | |
| 2075 | @classmethod |
| 2076 | def classes_for_base( |
| 2077 | cls, base_cls: Type[Any], sort: bool = True |
| 2078 | ) -> List[_DeferredMapperConfig]: |
| 2079 | classes_for_base = [ |
| 2080 | m |
| 2081 | for m, cls_ in [(m, m.cls) for m in cls._configs.values()] |
| 2082 | if cls_ is not None and issubclass(cls_, base_cls) |
| 2083 | ] |
| 2084 | |
| 2085 | if not sort: |