From: Federico Caselli Date: Sat, 4 Nov 2023 20:32:16 +0000 (+0100) Subject: Various minor fixes X-Git-Tag: rel_2_0_24~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b6326ca06660c6954f781889af8963551b16a31c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Various minor fixes Fix typo in exported class in init. #10578 Improve warning for loaderes. #10579 Properly document ARRAY.contains. #10587 Mention how to set a schema to the automatically generated enums. #10583 Improve type of cache key dispatcher Change-Id: I86e4f01f5d897b257246fe5f970b78e3444aca3e (cherry picked from commit 1bb9c4b94483a25057bad3d78cf9956e8f292330) --- diff --git a/doc/build/changelog/changelog_13.rst b/doc/build/changelog/changelog_13.rst index 462511f3fd..74fc0c202d 100644 --- a/doc/build/changelog/changelog_13.rst +++ b/doc/build/changelog/changelog_13.rst @@ -3337,7 +3337,7 @@ :tags: change, orm :tickets: 4412 - Added a new function :func:`.close_all_sessions` which takes + Added a new function :func:`_orm.close_all_sessions` which takes over the task of the :meth:`.Session.close_all` method, which is now deprecated as this is confusing as a classmethod. Pull request courtesy Augustin Trancart. diff --git a/doc/build/orm/declarative_tables.rst b/doc/build/orm/declarative_tables.rst index 711fa11bbe..4a1cbd0da3 100644 --- a/doc/build/orm/declarative_tables.rst +++ b/doc/build/orm/declarative_tables.rst @@ -856,8 +856,23 @@ datatype:: Status: sqlalchemy.Enum(Status, length=50, native_enum=False) } +By default :class:`_sqltypes.Enum` that are automatically generated are not +associated with the :class:`_sql.MetaData` instance used by the ``Base``, so if +the metadata defines a schema it will not be automatically associated with the +enum. To automatically associate the enum with the schema in the metadata or +table they belong to the :paramref:`_sqltypes.Enum.inherit_schema` can be set:: + + from enum import Enum + import sqlalchemy as sa + from sqlalchemy.orm import DeclarativeBase + + + class Base(DeclarativeBase): + metadata = sa.MetaData(schema="my_schema") + type_annotation_map = {Enum: sa.Enum(Enum, inherit_schema=True)} + Linking Specific ``enum.Enum`` or ``typing.Literal`` to other datatypes -++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ The above examples feature the use of an :class:`_sqltypes.Enum` that is automatically configuring itself to the arguments / attributes present on diff --git a/lib/sqlalchemy/__init__.py b/lib/sqlalchemy/__init__.py index 472f01ad06..871e403a77 100644 --- a/lib/sqlalchemy/__init__.py +++ b/lib/sqlalchemy/__init__.py @@ -55,7 +55,7 @@ from .pool import Pool as Pool from .pool import PoolProxiedConnection as PoolProxiedConnection from .pool import PoolResetState as PoolResetState from .pool import QueuePool as QueuePool -from .pool import SingletonThreadPool as SingleonThreadPool +from .pool import SingletonThreadPool as SingletonThreadPool from .pool import StaticPool as StaticPool from .schema import BaseDDLElement as BaseDDLElement from .schema import BLANK_SCHEMA as BLANK_SCHEMA @@ -273,9 +273,7 @@ __version__ = "2.0.24" def __go(lcls: Any) -> None: - from . import util as _sa_util - - _sa_util.preloaded.import_prefix("sqlalchemy") + _util.preloaded.import_prefix("sqlalchemy") from . import exc @@ -283,3 +281,14 @@ def __go(lcls: Any) -> None: __go(locals()) + + +def __getattr__(name: str) -> Any: + if name == "SingleonThreadPool": + _util.warn_deprecated( + "SingleonThreadPool was a typo in the v2 series. " + "Please use the correct SingletonThreadPool name.", + "2.0.24", + ) + return SingletonThreadPool + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index 79b43f5fe7..2f5e4ce8b7 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -519,9 +519,9 @@ class ORMCompileState(AbstractORMCompileState): ): util.warn( "Loader depth for query is excessively deep; caching will " - "be disabled for additional loaders. Consider using the " - "recursion_depth feature for deeply nested recursive eager " - "loaders. Use the compiled_cache=None execution option to " + "be disabled for additional loaders. For recursive eager " + "loaders consider using the recursion_depth feature. " + "Use the compiled_cache=None execution option to " "skip this warning." ) execution_options = execution_options.union( diff --git a/lib/sqlalchemy/sql/cache_key.py b/lib/sqlalchemy/sql/cache_key.py index 500e3e4dd7..831b90809b 100644 --- a/lib/sqlalchemy/sql/cache_key.py +++ b/lib/sqlalchemy/sql/cache_key.py @@ -11,6 +11,7 @@ import enum from itertools import zip_longest import typing from typing import Any +from typing import Callable from typing import Dict from typing import Iterable from typing import Iterator @@ -43,7 +44,7 @@ if typing.TYPE_CHECKING: class _CacheKeyTraversalDispatchType(Protocol): def __call__( s, self: HasCacheKey, visitor: _CacheKeyTraversal - ) -> CacheKey: + ) -> _CacheKeyTraversalDispatchTypeReturn: ... @@ -75,6 +76,18 @@ class CacheTraverseTarget(enum.Enum): ANON_NAME, ) = tuple(CacheTraverseTarget) +_CacheKeyTraversalDispatchTypeReturn = Sequence[ + Tuple[ + str, + Any, + Union[ + Callable[..., Tuple[Any, ...]], + CacheTraverseTarget, + InternalTraversal, + ], + ] +] + class HasCacheKey: """Mixin for objects which can produce a cache key. @@ -324,7 +337,7 @@ class HasCacheKey: ), ) else: - result += meth( + result += meth( # type: ignore attrname, obj, self, anon_map, bindparams ) return result diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index ddee7767bc..7e866cc032 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -2908,6 +2908,13 @@ class ARRAY( return operators.getitem, index, return_type def contains(self, *arg, **kw): + """``ARRAY.contains()`` not implemented for the base ARRAY type. + Use the dialect-specific ARRAY type. + + .. seealso:: + + :class:`_postgresql.ARRAY` - PostgreSQL specific version. + """ raise NotImplementedError( "ARRAY.contains() not implemented for the base " "ARRAY type; please use the dialect-specific ARRAY type"