mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
Проверка 09.02.2025
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
# orm/decl_base.py
|
||||
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
||||
# Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
@@ -65,11 +65,11 @@ from ..sql.schema import Column
|
||||
from ..sql.schema import Table
|
||||
from ..util import topological
|
||||
from ..util.typing import _AnnotationScanType
|
||||
from ..util.typing import get_args
|
||||
from ..util.typing import is_fwd_ref
|
||||
from ..util.typing import is_literal
|
||||
from ..util.typing import Protocol
|
||||
from ..util.typing import TypedDict
|
||||
from ..util.typing import typing_get_args
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ._typing import _ClassDict
|
||||
@@ -431,7 +431,7 @@ class _ImperativeMapperConfig(_MapperConfig):
|
||||
class _CollectedAnnotation(NamedTuple):
|
||||
raw_annotation: _AnnotationScanType
|
||||
mapped_container: Optional[Type[Mapped[Any]]]
|
||||
extracted_mapped_annotation: Union[Type[Any], str]
|
||||
extracted_mapped_annotation: Union[_AnnotationScanType, str]
|
||||
is_dataclass: bool
|
||||
attr_value: Any
|
||||
originating_module: str
|
||||
@@ -453,6 +453,7 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
"tablename",
|
||||
"mapper_args",
|
||||
"mapper_args_fn",
|
||||
"table_fn",
|
||||
"inherits",
|
||||
"single",
|
||||
"allow_dataclass_fields",
|
||||
@@ -759,7 +760,7 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
_include_dunders = self._include_dunders
|
||||
mapper_args_fn = None
|
||||
table_args = inherited_table_args = None
|
||||
|
||||
table_fn = None
|
||||
tablename = None
|
||||
fixed_table = "__table__" in clsdict_view
|
||||
|
||||
@@ -840,6 +841,22 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
)
|
||||
if not tablename and (not class_mapped or check_decl):
|
||||
tablename = cls_as_Decl.__tablename__
|
||||
elif name == "__table__":
|
||||
check_decl = _check_declared_props_nocascade(
|
||||
obj, name, cls
|
||||
)
|
||||
# if a @declared_attr using "__table__" is detected,
|
||||
# wrap up a callable to look for "__table__" from
|
||||
# the final concrete class when we set up a table.
|
||||
# this was fixed by
|
||||
# #11509, regression in 2.0 from version 1.4.
|
||||
if check_decl and not table_fn:
|
||||
# don't even invoke __table__ until we're ready
|
||||
def _table_fn() -> FromClause:
|
||||
return cls_as_Decl.__table__
|
||||
|
||||
table_fn = _table_fn
|
||||
|
||||
elif name == "__table_args__":
|
||||
check_decl = _check_declared_props_nocascade(
|
||||
obj, name, cls
|
||||
@@ -856,9 +873,10 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
if base is not cls:
|
||||
inherited_table_args = True
|
||||
else:
|
||||
# skip all other dunder names, which at the moment
|
||||
# should only be __table__
|
||||
continue
|
||||
# any other dunder names; should not be here
|
||||
# as we have tested for all four names in
|
||||
# _include_dunders
|
||||
assert False
|
||||
elif class_mapped:
|
||||
if _is_declarative_props(obj) and not obj._quiet:
|
||||
util.warn(
|
||||
@@ -1031,6 +1049,7 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
self.table_args = table_args
|
||||
self.tablename = tablename
|
||||
self.mapper_args_fn = mapper_args_fn
|
||||
self.table_fn = table_fn
|
||||
|
||||
def _setup_dataclasses_transforms(self) -> None:
|
||||
dataclass_setup_arguments = self.dataclass_setup_arguments
|
||||
@@ -1048,6 +1067,16 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
"'@registry.mapped_as_dataclass'"
|
||||
)
|
||||
|
||||
# can't create a dataclass if __table__ is already there. This would
|
||||
# fail an assertion when calling _get_arguments_for_make_dataclass:
|
||||
# assert False, "Mapped[] received without a mapping declaration"
|
||||
if "__table__" in self.cls.__dict__:
|
||||
raise exc.InvalidRequestError(
|
||||
f"Class {self.cls} already defines a '__table__'. "
|
||||
"ORM Annotated Dataclasses do not support a pre-existing "
|
||||
"'__table__' element"
|
||||
)
|
||||
|
||||
warn_for_non_dc_attrs = collections.defaultdict(list)
|
||||
|
||||
def _allow_dataclass_field(
|
||||
@@ -1279,10 +1308,8 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
type(attr_value),
|
||||
required=False,
|
||||
is_dataclass_field=is_dataclass_field,
|
||||
expect_mapped=expect_mapped
|
||||
and not is_dataclass, # self.allow_dataclass_fields,
|
||||
expect_mapped=expect_mapped and not is_dataclass,
|
||||
)
|
||||
|
||||
if extracted is None:
|
||||
# ClassVar can come out here
|
||||
return None
|
||||
@@ -1290,9 +1317,9 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
extracted_mapped_annotation, mapped_container = extracted
|
||||
|
||||
if attr_value is None and not is_literal(extracted_mapped_annotation):
|
||||
for elem in typing_get_args(extracted_mapped_annotation):
|
||||
if isinstance(elem, str) or is_fwd_ref(
|
||||
elem, check_generic=True
|
||||
for elem in get_args(extracted_mapped_annotation):
|
||||
if is_fwd_ref(
|
||||
elem, check_generic=True, check_for_plain_string=True
|
||||
):
|
||||
elem = de_stringify_annotation(
|
||||
self.cls,
|
||||
@@ -1687,7 +1714,11 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
|
||||
manager = attributes.manager_of_class(cls)
|
||||
|
||||
if "__table__" not in clsdict_view and table is None:
|
||||
if (
|
||||
self.table_fn is None
|
||||
and "__table__" not in clsdict_view
|
||||
and table is None
|
||||
):
|
||||
if hasattr(cls, "__table_cls__"):
|
||||
table_cls = cast(
|
||||
Type[Table],
|
||||
@@ -1733,7 +1764,12 @@ class _ClassScanMapperConfig(_MapperConfig):
|
||||
)
|
||||
else:
|
||||
if table is None:
|
||||
table = cls_as_Decl.__table__
|
||||
if self.table_fn:
|
||||
table = self.set_cls_attribute(
|
||||
"__table__", self.table_fn()
|
||||
)
|
||||
else:
|
||||
table = cls_as_Decl.__table__
|
||||
if declared_columns:
|
||||
for c in declared_columns:
|
||||
if not table.c.contains_column(c):
|
||||
|
Reference in New Issue
Block a user