Mike Bayer [Tue, 19 Sep 2023 21:57:50 +0000 (17:57 -0400)]
accommodate all mapped_column() parameters in Annotated transfer
Fixed a wide range of :func:`_orm.mapped_column` parameters that were not
being transferred when using the :func:`_orm.mapped_column` object inside
of a pep-593 ``Annotated`` object, including
:paramref:`_orm.mapped_column.sort_order`,
:paramref:`_orm.mapped_column.deferred`,
:paramref:`_orm.mapped_column.autoincrement`,
:paramref:`_orm.mapped_column.system`, :paramref:`_orm.mapped_column.info`
etc.
Additionally, it remains not supported to have dataclass arguments, such as
:paramref:`_orm.mapped_column.kw_only`,
:paramref:`_orm.mapped_column.default_factory` etc. indicated within the
:func:`_orm.mapped_column` received by ``Annotated``, as this is not
supported with pep-681 Dataclass Transforms. A warning is now emitted when
these parameters are used within ``Annotated`` in this way (and they
continue to be ignored).
Mike Bayer [Tue, 19 Sep 2023 12:58:52 +0000 (08:58 -0400)]
Ensure loader criteria used for ORM join with expression condition
Fixed bug where ORM :func:`_orm.with_loader_criteria` would not apply
itself to a :meth:`_sql.Select.join` where the ON clause were given as a
plain SQL comparison, rather than as a relationship target or similar.
Mike Bayer [Tue, 12 Sep 2023 15:05:48 +0000 (11:05 -0400)]
parse for parenthesis in referenced tablename, columnname
Fixed a series of reflection issues affecting the PostgreSQL,
MySQL/MariaDB, and SQLite dialects when reflecting foreign key constraints
where the target column contained parenthesis in one or both of the table
name or column name.
Janek Nouvertné [Wed, 16 Aug 2023 15:01:39 +0000 (11:01 -0400)]
Update type annotations for loading options
Update type annotations for ORM loading options, restricting them to accept
only `"*"` instead of any string for string arguments. Pull request
courtesy Janek Nouvertné.
Fix typing to ensure that InstrumentedAttribute is hashable
Repaired the core "SQL element" class ``SQLCoreOperations`` to support the
``__hash__()`` method from a typing perspective, as objects like
:class:`.Column` and ORM :class:`.InstrumentedAttribute` are hashable and
are used as dictionary keys in the public API for the :class:`_dml.Update`
and :class:`_dml.Insert` constructs. Previously, type checkers were not
aware the root SQL element was hashable.
Mike Bayer [Thu, 14 Sep 2023 14:48:03 +0000 (10:48 -0400)]
consider _ClassStrategyLoad as part of endpoint path
Added new capability to the :func:`_orm.selectin_polymorphic` loader option
which allows other loader options to be bundled as siblings, referring to
one of its subclasses, within the sub-options of parent loader option.
Previously, this pattern was only supported if the
:func:`_orm.selectin_polymorphic` were at the top level of the options for
the query. See new documentation section for example.
As part of this change, improved the behavior of the
:meth:`_orm.Load.selectin_polymorphic` method / loader strategy so that the
subclass load does not load most already-loaded columns from the parent
table, when the option is used against a class that is already being
relationship-loaded. Previously, the logic to load only the subclass
columns worked only for a top level class load.
Mike Bayer [Tue, 12 Sep 2023 16:24:14 +0000 (12:24 -0400)]
qualify hashlib.md5() with usedforsecurity=False
Qualified the use of ``hashlib.md5()`` within the DDL compiler, which is
used to generate deterministic four-character suffixes for long index and
constraint names in DDL statements, to include the Python 3.9+
``usedforsecurity=False`` parameter so that Python interpreters built for
restricted environments such as FIPS do not consider this call to be
related to security concerns.
Mike Bayer [Tue, 12 Sep 2023 12:01:54 +0000 (08:01 -0400)]
allow any key for naming_convention dict, typing is not possible
Fixed regression introduced in 2.0.20 via :ticket:`9600` fix which
attempted to add more formal typing to
:paramref:`_schema.MetaData.naming_convention`. This change prevented basic
naming convention dictionaries from passing typing and has been adjusted so
that a plain dictionary of strings for keys as well as dictionaries that
use constraint types as keys or a mix of both, are again accepted.
As part of this change, lesser used forms of the naming convention
dictionary are also typed, including that it currently allows for
``Constraint`` type objects as keys as well.
Mike Bayer [Thu, 7 Sep 2023 21:37:13 +0000 (17:37 -0400)]
ensure all modules are importable without pytest harnesses
Fixed very old issue where the full extent of SQLAlchemy modules, including
``sqlalchemy.testing.fixtures``, could not be imported outside of a pytest
run. This suits inspection utilities such as ``pkgutil`` that attempt to
import all installed modules in all packages.
RomeoDespres [Tue, 29 Aug 2023 10:11:11 +0000 (06:11 -0400)]
Make `Mapped` covariant
Made the contained type for :class:`.Mapped` covariant; this is to allow
greater flexibility for end-user typing scenarios, such as the use of
protocols to represent particular mapped class structures that are passed
to other functions. As part of this change, the contained type was also
made covariant for dependent and related types such as
:class:`_orm.base.SQLORMOperations`, :class:`_orm.WriteOnlyMapped`, and
:class:`_sql.SQLColumnExpression`. Pull request courtesy Roméo Després.
within the change, there is a bit of adjustment to ``__radd__()`` to
match the typing of ``__add__()``, which previously was slightly
different for some reason and not passing on mypy with this change.
Mike Bayer [Fri, 25 Aug 2023 14:48:59 +0000 (10:48 -0400)]
automatically create proxy col for already-used col in values
The :class:`.Values` construct will now automatically create a proxy (i.e.
a copy) of a :class:`_sql.column` if the column were already associated
with an existing FROM clause. This allows that an expression like
``values_obj.c.colname`` will produce the correct FROM clause even in the
case that ``colname`` was passed as a :class:`_sql.column` that was already
used with a previous :class:`.Values` or other table construct.
Originally this was considered to be a candidate for an error condition,
however it's likely this pattern is already in widespread use so it's
now added to support.
* adjust unrelated dml test recently added for update..returning *
case to not rely upon ordering
Mike Bayer [Fri, 25 Aug 2023 16:12:06 +0000 (12:12 -0400)]
add explicit support for aliased ORM models in UPDATE/DELETE
Adjusted the ORM's interpretation of UPDATE/DELETE targets to not interfere
with the target table passed to the statement, such as for
:class:`_orm.aliased` constructs. Cases like ORM session synchonize using
"SELECT" statements such as with MySQL/ MariaDB will still have issues with
UPDATE/DELETE of this form so it's best to disable synchonize_session when
using DML statements of this type.
A separate issue to identify RETURNING should be used for
ORM UPDATE of an aliased() with fetch strategy and that these columns
should come back was attempted here, but is failing tests and is beyond
the scope of the immediate issue.
also updates the mssql URL in config to suit current preferences :)
Eugene Toder [Tue, 22 Aug 2023 17:54:02 +0000 (13:54 -0400)]
Allow using Enum with length=None
Adjusted the :class:`_types.Enum` datatype to accept an argument of
``None`` for the :paramref:`_types.Enum.length` parameter, resulting in a
VARCHAR or other textual type with no length in the resulting DDL. This
allows for new elements of any length to be added to the type after it
exists in the schema. Pull request courtesy Eugene Toder.
Jordan Macdonald [Wed, 16 Aug 2023 15:00:59 +0000 (11:00 -0400)]
Fix type hint of `key` on `Visitable.__class_getitem__()`
Fixed the type annotation for ``__class_getitem__()`` as applied to the
``Visitable`` class at the base of expression constructs to accept ``Any``
for a key, rather than ``str``, which helps with some IDEs such as PyCharm
when attempting to write typing annotations for SQL constructs which
include generic selectors. Pull request courtesy Jordan Macdonald.
Mike Bayer [Mon, 21 Aug 2023 14:40:09 +0000 (10:40 -0400)]
adjust concat precedence to match that of string comparison
Adjusted the operator precedence for the string concatenation operator to
be equal to that of string matching operators, such as
:meth:`.ColumnElement.like`, :meth:`.ColumnElement.regexp_match`,
:meth:`.ColumnElement.match`, etc., as well as plain ``==`` which has the
same precedence as string comparison operators, so that parenthesis will be
applied to a string concatenation expression that follows a string match
operator. This provides for backends such as PostgreSQL where the "regexp
match" operator is apparently of higher precedence than the string
concatenation operator.
Mike Bayer [Wed, 16 Aug 2023 19:38:51 +0000 (15:38 -0400)]
place asyncpg do_ping within a transaction if one not begun already
Fixed regression which appeared in 2.0 due to :ticket:`8491` where the
revised "ping" used for PostgreSQL dialects when the
:paramref:`_sa.create_engine.pool_pre_ping` parameter is in use would
interfere with the use of asyncpg with PGBouncer "transaction" mode, as the
multiple PostgreSQL commands emitted by asnycpg could be broken out among
multiple connections leading to errors, due to the lack of any transaction
around this newly revised "ping". The ping is now invoked within a
transaction, in the same way that is implicit with all other backends that
are based on the pep-249 DBAPI; this guarantees that the series of PG
commands sent by asyncpg for this command are invoked on the same backend
connection without it jumping to a different connection mid-command. The
transaction is not used if the asyncpg dialect is used in "AUTOCOMMIT"
mode, which remains incompatible with pgbouncer transaction mode.
Mike Bayer [Sun, 13 Aug 2023 15:16:03 +0000 (11:16 -0400)]
propagate regular execution_options to secondary eager loaders
Fixed fairly major issue where execution options passed to
:meth:`_orm.Session.execute`, as well as execution options local to the ORM
executed statement itself, would not be propagated along to eager loaders
such as that of :func:`_orm.selectinload`, :func:`_orm.immediateload`, and
:meth:`_orm.subqueryload`, making it impossible to do things such as
disabling the cache for a single statement or using
``schema_translate_map`` for a single statement, as well as the use of
user-custom execution options. A change has been made where **all**
user-facing execution options present for :meth:`_orm.Session.execute` will
be propagated along to additional loaders.
As part of this change, the warning for "excessively deep" eager loaders
leading to caching being disabled can be silenced on a per-statement
basis by sending ``execution_options={"compiled_cache": None}`` to
:meth:`_orm.Session.execute`, which will disable caching for the full
series of statements within that scope.
Mike Bayer [Thu, 10 Aug 2023 22:26:45 +0000 (18:26 -0400)]
safe annotate QueryableAttribute inside of join() condition
Fixed fundamental issue which prevented some forms of ORM "annotations"
from taking place for subqueries which made use of :meth:`_sql.Select.join`
against a relationship target. These annotations are used whenever a
subquery is used in special situations such as within
:meth:`_orm.PropComparator.and_` and other ORM-specific scenarios.
Mike Bayer [Fri, 11 Aug 2023 19:30:58 +0000 (15:30 -0400)]
update slotscheck
we are getting intermittent crashes from slotscheck on CI.
actually I saw one just happen here with the latest version, it's
a core dump :(. anyway, try it anyway see if things improve
Mike Bayer [Wed, 9 Aug 2023 14:17:35 +0000 (10:17 -0400)]
implement custom setstate to work around implicit type/comparator
Fixed issue where unpickling of a :class:`_schema.Column` or other
:class:`_sql.ColumnElement` would fail to restore the correct "comparator"
object, which is used to generate SQL expressions specific to the type
object.
Mehdi Gmira [Mon, 7 Aug 2023 14:50:39 +0000 (10:50 -0400)]
Fix annotations
Typing improvements:
* :class:`.CursorResult` is returned for some forms of
:meth:`_orm.Session.execute` where DML without RETURNING is used
* fixed type for :paramref:`_orm.Query.with_for_update.of` parameter within
:meth:`_orm.Query.with_for_update`
* improvements to ``_DMLColumnArgument`` type used by some DML methods to
pass column expressions
* Add overload to :func:`_sql.literal` so that it is inferred that the
return type is ``BindParameter[NullType]`` where
:paramref:`_sql.literal.type_` param is None
* Add overloads to :meth:`_sql.ColumnElement.op` so that the inferred
type when :paramref:`_sql.ColumnElement.op.return_type` is not provided
is ``Callable[[Any], BinaryExpression[Any]]``
* Add missing overload to :meth:`_sql.ColumnElement.__add__`
Mike Bayer [Mon, 7 Aug 2023 14:47:11 +0000 (10:47 -0400)]
implement RETURNING * for ORM DML
Implemented the "RETURNING '*'" use case for ORM enabled DML statements.
This will render in as many cases as possible and return the unfiltered
result set, however is not supported for multi-parameter "ORM bulk INSERT"
statements that have specific column rendering requirements.
Mike Bayer [Fri, 4 Aug 2023 00:14:39 +0000 (20:14 -0400)]
apply correct type to orm connection.execution_options
Fixed issue in :class:`_orm.Session` and :class:`_asyncio.AsyncSession`
methods such as :meth:`_orm.Session.connection` where the
:paramref:`_orm.Session.connection.execution_options` parameter were
hardcoded to an internal type that is not user-facing.
Mike Bayer [Wed, 2 Aug 2023 17:34:03 +0000 (13:34 -0400)]
ensure collection adapter is serialized
Fixed issue where dictionary-based collections such as
:func:`_orm.attribute_keyed_dict` did not fully pickle/unpickle correctly,
leading to issues when attempting to mutate such a collection after
unpickling.
Mike Bayer [Mon, 31 Jul 2023 21:47:07 +0000 (17:47 -0400)]
expand out all columns for _all_column_expressions
Fixed issue where the ORM's generation of a SELECT from a joined
inheritance model with same-named columns in superclass and subclass would
somehow not send the correct list of column names to the :class:`.CTE`
construct, when the RECURSIVE column list were generated.
Added new methods :meth:`_asyncio.AsyncConnection.aclose` as a synonym for
:meth:`_asyncio.AsyncConnection.close` and
:meth:`_asyncio.AsyncSession.aclose` as a synonym for
:meth:`_asyncio.AsyncSession.close` to the
:class:`_asyncio.AsyncConnection` and :class:`_asyncio.AsyncSession`
objects, to provide compatibility with Python standard library
``@contextlib.aclosing`` construct. Pull request courtesy Grigoriev Semyon.
Mike Bayer [Mon, 31 Jul 2023 15:17:20 +0000 (11:17 -0400)]
pass along independent CTE attributes in ORM context
Fixed issue where an ORM-enabled :func:`_sql.select` construct would not
render any CTEs added only via the :meth:`_sql.Select.add_cte` method that
were not otherwise referenced in the statement.