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_api.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
|
||||
@@ -14,7 +14,6 @@ import re
|
||||
import typing
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
from typing import cast
|
||||
from typing import ClassVar
|
||||
from typing import Dict
|
||||
from typing import FrozenSet
|
||||
@@ -72,13 +71,16 @@ from ..sql.selectable import FromClause
|
||||
from ..util import hybridmethod
|
||||
from ..util import hybridproperty
|
||||
from ..util import typing as compat_typing
|
||||
from ..util import warn_deprecated
|
||||
from ..util.typing import CallableReference
|
||||
from ..util.typing import de_optionalize_union_types
|
||||
from ..util.typing import flatten_newtype
|
||||
from ..util.typing import is_generic
|
||||
from ..util.typing import is_literal
|
||||
from ..util.typing import is_newtype
|
||||
from ..util.typing import is_pep695
|
||||
from ..util.typing import Literal
|
||||
from ..util.typing import LITERAL_TYPES
|
||||
from ..util.typing import Self
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -207,7 +209,7 @@ def synonym_for(
|
||||
:paramref:`.orm.synonym.descriptor` parameter::
|
||||
|
||||
class MyClass(Base):
|
||||
__tablename__ = 'my_table'
|
||||
__tablename__ = "my_table"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
_job_status = Column("job_status", String(50))
|
||||
@@ -373,20 +375,21 @@ class declared_attr(interfaces._MappedAttribute[_T], _declared_attr_common):
|
||||
for subclasses::
|
||||
|
||||
class Employee(Base):
|
||||
__tablename__ = 'employee'
|
||||
__tablename__ = "employee"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
type: Mapped[str] = mapped_column(String(50))
|
||||
|
||||
@declared_attr.directive
|
||||
def __mapper_args__(cls) -> Dict[str, Any]:
|
||||
if cls.__name__ == 'Employee':
|
||||
if cls.__name__ == "Employee":
|
||||
return {
|
||||
"polymorphic_on":cls.type,
|
||||
"polymorphic_identity":"Employee"
|
||||
"polymorphic_on": cls.type,
|
||||
"polymorphic_identity": "Employee",
|
||||
}
|
||||
else:
|
||||
return {"polymorphic_identity":cls.__name__}
|
||||
return {"polymorphic_identity": cls.__name__}
|
||||
|
||||
|
||||
class Engineer(Employee):
|
||||
pass
|
||||
@@ -485,6 +488,7 @@ def declarative_mixin(cls: Type[_T]) -> Type[_T]:
|
||||
from sqlalchemy.orm import declared_attr
|
||||
from sqlalchemy.orm import declarative_mixin
|
||||
|
||||
|
||||
@declarative_mixin
|
||||
class MyMixin:
|
||||
|
||||
@@ -492,10 +496,11 @@ def declarative_mixin(cls: Type[_T]) -> Type[_T]:
|
||||
def __tablename__(cls):
|
||||
return cls.__name__.lower()
|
||||
|
||||
__table_args__ = {'mysql_engine': 'InnoDB'}
|
||||
__mapper_args__= {'always_refresh': True}
|
||||
__table_args__ = {"mysql_engine": "InnoDB"}
|
||||
__mapper_args__ = {"always_refresh": True}
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
|
||||
class MyModel(MyMixin, Base):
|
||||
name = Column(String(1000))
|
||||
@@ -638,10 +643,10 @@ class DeclarativeBase(
|
||||
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
The above ``Base`` class is now usable as the base for new declarative
|
||||
mappings. The superclass makes use of the ``__init_subclass__()``
|
||||
method to set up new classes and metaclasses aren't used.
|
||||
@@ -664,11 +669,12 @@ class DeclarativeBase(
|
||||
bigint = Annotated[int, "bigint"]
|
||||
my_metadata = MetaData()
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
metadata = my_metadata
|
||||
type_annotation_map = {
|
||||
str: String().with_variant(String(255), "mysql", "mariadb"),
|
||||
bigint: BigInteger()
|
||||
bigint: BigInteger(),
|
||||
}
|
||||
|
||||
Class-level attributes which may be specified include:
|
||||
@@ -1221,42 +1227,34 @@ class registry:
|
||||
|
||||
self.type_annotation_map.update(
|
||||
{
|
||||
sub_type: sqltype
|
||||
de_optionalize_union_types(typ): sqltype
|
||||
for typ, sqltype in type_annotation_map.items()
|
||||
for sub_type in compat_typing.expand_unions(
|
||||
typ, include_union=True, discard_none=True
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
def _resolve_type(
|
||||
self, python_type: _MatchedOnType
|
||||
self, python_type: _MatchedOnType, _do_fallbacks: bool = True
|
||||
) -> Optional[sqltypes.TypeEngine[Any]]:
|
||||
search: Iterable[Tuple[_MatchedOnType, Type[Any]]]
|
||||
python_type_type: Type[Any]
|
||||
search: Iterable[Tuple[_MatchedOnType, Type[Any]]]
|
||||
|
||||
if is_generic(python_type):
|
||||
if is_literal(python_type):
|
||||
python_type_type = cast("Type[Any]", python_type)
|
||||
python_type_type = python_type # type: ignore[assignment]
|
||||
|
||||
search = ( # type: ignore[assignment]
|
||||
search = (
|
||||
(python_type, python_type_type),
|
||||
(Literal, python_type_type),
|
||||
*((lt, python_type_type) for lt in LITERAL_TYPES), # type: ignore[arg-type] # noqa: E501
|
||||
)
|
||||
else:
|
||||
python_type_type = python_type.__origin__
|
||||
search = ((python_type, python_type_type),)
|
||||
elif is_newtype(python_type):
|
||||
python_type_type = flatten_newtype(python_type)
|
||||
search = ((python_type, python_type_type),)
|
||||
elif is_pep695(python_type):
|
||||
python_type_type = python_type.__value__
|
||||
flattened = None
|
||||
search = ((python_type, python_type_type),)
|
||||
else:
|
||||
python_type_type = cast("Type[Any]", python_type)
|
||||
flattened = None
|
||||
elif isinstance(python_type, type):
|
||||
python_type_type = python_type
|
||||
search = ((pt, pt) for pt in python_type_type.__mro__)
|
||||
else:
|
||||
python_type_type = python_type # type: ignore[assignment]
|
||||
search = ((python_type, python_type_type),)
|
||||
|
||||
for pt, flattened in search:
|
||||
# we search through full __mro__ for types. however...
|
||||
@@ -1280,6 +1278,39 @@ class registry:
|
||||
if resolved_sql_type is not None:
|
||||
return resolved_sql_type
|
||||
|
||||
# 2.0 fallbacks
|
||||
if _do_fallbacks:
|
||||
python_type_to_check: Any = None
|
||||
kind = None
|
||||
if is_pep695(python_type):
|
||||
# NOTE: assume there aren't type alias types of new types.
|
||||
python_type_to_check = python_type
|
||||
while is_pep695(python_type_to_check):
|
||||
python_type_to_check = python_type_to_check.__value__
|
||||
python_type_to_check = de_optionalize_union_types(
|
||||
python_type_to_check
|
||||
)
|
||||
kind = "TypeAliasType"
|
||||
if is_newtype(python_type):
|
||||
python_type_to_check = flatten_newtype(python_type)
|
||||
kind = "NewType"
|
||||
|
||||
if python_type_to_check is not None:
|
||||
res_after_fallback = self._resolve_type(
|
||||
python_type_to_check, False
|
||||
)
|
||||
if res_after_fallback is not None:
|
||||
assert kind is not None
|
||||
warn_deprecated(
|
||||
f"Matching the provided {kind} '{python_type}' on "
|
||||
"its resolved value without matching it in the "
|
||||
"type_annotation_map is deprecated; add this type to "
|
||||
"the type_annotation_map to allow it to match "
|
||||
"explicitly.",
|
||||
"2.0",
|
||||
)
|
||||
return res_after_fallback
|
||||
|
||||
return None
|
||||
|
||||
@property
|
||||
@@ -1472,6 +1503,7 @@ class registry:
|
||||
|
||||
Base = mapper_registry.generate_base()
|
||||
|
||||
|
||||
class MyClass(Base):
|
||||
__tablename__ = "my_table"
|
||||
id = Column(Integer, primary_key=True)
|
||||
@@ -1484,6 +1516,7 @@ class registry:
|
||||
|
||||
mapper_registry = registry()
|
||||
|
||||
|
||||
class Base(metaclass=DeclarativeMeta):
|
||||
__abstract__ = True
|
||||
registry = mapper_registry
|
||||
@@ -1649,9 +1682,10 @@ class registry:
|
||||
|
||||
mapper_registry = registry()
|
||||
|
||||
|
||||
@mapper_registry.mapped
|
||||
class Foo:
|
||||
__tablename__ = 'some_table'
|
||||
__tablename__ = "some_table"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String)
|
||||
@@ -1691,15 +1725,17 @@ class registry:
|
||||
|
||||
mapper_registry = registry()
|
||||
|
||||
|
||||
@mapper_registry.as_declarative_base()
|
||||
class Base:
|
||||
@declared_attr
|
||||
def __tablename__(cls):
|
||||
return cls.__name__.lower()
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
|
||||
class MyMappedClass(Base):
|
||||
# ...
|
||||
|
||||
class MyMappedClass(Base): ...
|
||||
|
||||
All keyword arguments passed to
|
||||
:meth:`_orm.registry.as_declarative_base` are passed
|
||||
@@ -1729,12 +1765,14 @@ class registry:
|
||||
|
||||
mapper_registry = registry()
|
||||
|
||||
|
||||
class Foo:
|
||||
__tablename__ = 'some_table'
|
||||
__tablename__ = "some_table"
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String)
|
||||
|
||||
|
||||
mapper = mapper_registry.map_declaratively(Foo)
|
||||
|
||||
This function is more conveniently invoked indirectly via either the
|
||||
@@ -1787,12 +1825,14 @@ class registry:
|
||||
my_table = Table(
|
||||
"my_table",
|
||||
mapper_registry.metadata,
|
||||
Column('id', Integer, primary_key=True)
|
||||
Column("id", Integer, primary_key=True),
|
||||
)
|
||||
|
||||
|
||||
class MyClass:
|
||||
pass
|
||||
|
||||
|
||||
mapper_registry.map_imperatively(MyClass, my_table)
|
||||
|
||||
See the section :ref:`orm_imperative_mapping` for complete background
|
||||
@@ -1839,15 +1879,17 @@ def as_declarative(**kw: Any) -> Callable[[Type[_T]], Type[_T]]:
|
||||
|
||||
from sqlalchemy.orm import as_declarative
|
||||
|
||||
|
||||
@as_declarative()
|
||||
class Base:
|
||||
@declared_attr
|
||||
def __tablename__(cls):
|
||||
return cls.__name__.lower()
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
|
||||
class MyMappedClass(Base):
|
||||
# ...
|
||||
|
||||
class MyMappedClass(Base): ...
|
||||
|
||||
.. seealso::
|
||||
|
||||
|
Reference in New Issue
Block a user