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.
Mike Bayer [Wed, 26 Jul 2023 16:10:48 +0000 (12:10 -0400)]
dont rely on default_isolation_level to restore engine-level iso
Fixed critical issue where setting
:paramref:`_sa.create_engine.isolation_level` to ``AUTOCOMMIT`` (as opposed
to using the :meth:`_engine.Engine.execution_options` method) would fail to
restore "autocommit" to a pooled connection if an alternate isolation level
were temporarily selected using
:paramref:`_engine.Connection.execution_options.isolation_level`.
Mike Bayer [Tue, 25 Jul 2023 15:26:03 +0000 (11:26 -0400)]
implement join_depth for immediateload, fix expunged loading
Fixed issue where the ``lazy="immediateload"`` loader strategy would place
an internal loading token into the ORM mapped attribute under circumstances
where the load should not occur, such as in a recursive self-referential
load. As part of this change, the ``lazy="immediateload"`` strategy now
honors the :paramref:`_orm.relationship.join_depth` parameter for
self-referential eager loads in the same way as that of other eager
loaders, where leaving it unset or set at zero will lead to a
self-referential immediateload not occurring, setting it to a value of one
or greater will immediateload up until that given depth.
Mike Bayer [Thu, 20 Jul 2023 17:06:22 +0000 (13:06 -0400)]
have token load flatten aliases unconditionally
Fixed issue where chaining :func:`_orm.load_only` or other wildcard use of
:func:`_orm.defer` from another eager loader using a :func:`_orm.aliased`
against a joined inheritance subclass would fail to take effect for columns
local to the superclass.
Mike Bayer [Thu, 20 Jul 2023 16:36:35 +0000 (12:36 -0400)]
de-clone FROM objects placed into from_linter.froms
Fixed issue where internal cloning used by the ORM for expressions like
:meth:`_orm.relationship.Comparator.any` to produce correlated EXISTS
constructs would interfere with the "cartesian product warning" feature of
the SQL compiler, leading the SQL compiler to warn when all elements of the
statement were correctly joined.
Rename Row t and tuples with underscored versions.
Renamed :attr:`_result.Row.t` and :meth:`_result.Row.tuple` to
:attr:`_result.Row._t` and :meth:`_result.Row._tuple`; this is to suit the
policy that all methods and pre-defined attributes on :class:`.Row` should
be in the style of Python standard library ``namedtuple`` where all fixed
names have a leading underscore, to avoid name conflicts with existing
column names. The previous method/attribute is now deprecated and will
emit a deprecation warning.
Mike Bayer [Fri, 14 Jul 2023 16:08:24 +0000 (12:08 -0400)]
test for None plugin_subject
Fixed additional regression caused by :ticket:`9805` where more aggressive
propagation of the "ORM" flag on statements could lead to an internal
attribute error when embedding an ORM :class:`.Query` construct that
nonetheless contained no ORM entities within a Core SQL statement, in this
case ORM-enabled UPDATE and DELETE statements.
<!-- Provide a general summary of your proposed changes in the Title field above -->
### Description
<!-- Describe your changes in detail -->
### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)
-->
This pull request is:
- [ ] A documentation / typographical error fix
- Good to go, no issue or tests are needed
- [X] A short code fix
- please include the issue number, and create an issue if none exists, which
must include a complete example of the issue. one line code fixes without an
issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests. one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.
Fixes #10079
**Have a nice day!**
Mike Bayer [Wed, 12 Jul 2023 13:32:10 +0000 (09:32 -0400)]
ensure CITEXT is not cast as VARCHAR
Fixed issue where comparisons to the :class:`_postgresql.CITEXT` datatype
would cast the right side to ``VARCHAR``, leading to the right side not
being interpreted as a ``CITEXT`` datatype, for the asyncpg, psycopg3 and
pg80000 dialects. This led to the :class:`_postgresql.CITEXT` type being
essentially unusable for practical use; this is now fixed and the test
suite has been corrected to properly assert that expressions are rendered
correctly.
Federico Caselli [Fri, 23 Jun 2023 18:09:29 +0000 (20:09 +0200)]
Improved typing
- correctly inspect orm classes and instances
- add missing generics in run_sync on async connection and session
- minor fixes to relationship order params and shift overload