53 Commits

Author SHA1 Message Date
Federico Caselli 0a185a3bb6 ensure function classes are not shadowed
Ensure the _FunctionGenerator method do not shadow the function class
of the same name

Fixed a typing issue where the typed members of :data:`.func` would return
the appropriate class of the same name, however this creates an issue for
typecheckers such as Zuban and pyrefly that assume :pep:`749` style
typechecking even if the file states that it's a :pep:`563` file; they see
the returned name as indicating the method object and not the class object.
These typecheckers are actually following along with an upcoming test
harness that insists on :pep:`749` style name resolution for this case
unconditionally.  Since :pep:`749` is the way of the future regardless,
differently-named type aliases have been added for these return types.

Fixes: #13167
Change-Id: If58a3858001c78ab21b2ed343205dfd9ce868576
2026-03-16 15:47:06 -04:00
Fardin Alizadeh fb408b8e9d fix typos (#13038)
* fix typos

* fix typos

---------

Co-authored-by: fardyn <fa.alizadeh@pm.me>
2025-12-18 21:18:42 +01:00
Federico Caselli 8849e2b775 use cython for result
Use cython for the internal result methods. This improves the speed
of the row processing of about 15-30% depending on cases.

Change-Id: Ia19c1a61aa1a55405b8327f88b1ee02627c4217a
2025-12-08 21:45:41 +01:00
Mike Bayer 8383e3f48c add pyv to file template; use = for all custom args
adding "test/" to pytest doesnt work because then we can't indicate
a specific set of test files.  use = for all sqlalchemy-custom
parameters instead to avoid [1]

[1] https://github.com/pytest-dev/pytest/issues/13913

Change-Id: I9eb5bbfac734fcb145fcf3ae58fcc55df6bb6e71
2025-11-15 11:36:56 -05:00
Yannick PÉROUX d160cb5314 Typing: fix type of func.coalesce when used with hybrid properties
Fixed typing issue where :class:`.coalesce` would not return the correct
return type when a nullable form of that argument were passed, even though
this function is meant to select the non-null entry among possibly null
arguments.  Pull request courtesy Yannick PÉROUX.

Closes: #12963
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12963
Pull-request-sha: 05d0d9784d

Change-Id: Ife83a384ea57faf446c1fdb542df14627348f40f
2025-11-11 16:13:38 -05:00
Mike Bayer e3df597710 refactor common reporting options a bit
Change-Id: I4a7316867ceacabc4e641017c9f19e40fababdbe
2025-10-20 15:09:03 +00:00
Mike Bayer e1f3b43ad8 require nox 2025.10.16, remove python version workaround
the workaround for [1] no longer works now that the fix has
been committed.  slightly surprising but just require that version
of nox and carry on

[1] https://github.com/wntrblm/nox/pull/999

Change-Id: I4b4031c3d3d02399f55f9750237de61e5e90d179
2025-10-18 16:05:14 -04:00
Rebecca Chen 0ab0d0c980 Change typing tests to use assert_type instead of reveal_type
Closes: #12922
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12922
Pull-request-sha: 580f663816

Change-Id: I9f3bdb4c105971f53fa10ed8a934356203ddb080
2025-10-18 11:23:20 -04:00
Mike Bayer df899e94cf use nox
The top-level test runner has been changed to use ``nox``, adding a
``noxfile.py`` as well as some included modules.   The ``tox.ini`` file
remains in place so that ``tox`` runs will continue to function in the near
term, however it will be eventually removed and improvements and
maintenance going forward will be only towards ``noxfile.py``.

Change-Id: I66639991e1dc3db582e2ff13f9348a7d6241916e
2025-10-15 14:13:55 -04:00
Mike Bayer 9f16251b64 lock down format_docs_code.py to specific paths
the script too quick to pick up on dot directories,
random .py files I have in my local checkout, etc.

Change-Id: I6eb66ddd3598495a420d9e4fc0f644b60fb3ec6d
2025-09-29 11:56:05 -04:00
Mike Bayer 27c0dafbeb add session-wide execution_options
Added support for per-session execution options that are merged into all
queries executed within that session. The :class:`_orm.Session`,
:class:`_orm.sessionmaker`, :class:`_orm.scoped_session`,
:class:`_ext.asyncio.AsyncSession`, and
:class:`_ext.asyncio.async_sessionmaker` constructors now accept an
:paramref:`_orm.Session.execution_options` parameter that will be applied
to all explicit query executions (e.g. using :meth:`_orm.Session.execute`,
:meth:`_orm.Session.get`, :meth:`_orm.Session.scalars`) for that session
instance.

Fixes: #12659
Change-Id: I6e19e1567e0c04df32ba1d43baf420fb762f155c
2025-08-31 09:53:02 -04:00
Federico Caselli fb5e64c51a add example to update from that uses values
Change-Id: Ic02a722be9a30851a87e0da4759c728e86fb22c8
References: #11768
2025-06-24 17:43:43 -04:00
Mike Bayer be8ffcfa4d add future mode tests for MappedAsDataclass; more py314b1 regressions
for py314b2 all issues should be resolved

py314: yes
Change-Id: I498a1f623aeb5eb664289236e01e35d8a3dec99f
2025-06-03 11:01:42 -04:00
Federico Caselli 68cd3e8ec7 Fix type errors surfaced by mypy 1.16
Change-Id: I50bbd760577ff7c865c81153041e82bba068e5d8
2025-06-03 09:58:25 -04:00
Shamil 64f45d0a6b refactor(testing-and-utils): Remove unused code and fix style issues
This PR includes several small refactorings and style fixes aimed at improving code cleanliness, primarily within the test suite and tooling.

Key changes:

*   Removed assignments to unused variables in various test files (`test_dialect.py`, `test_reflection.py`, `test_select.py`).
*   Removed an unused variable in the pytest plugin (`pytestplugin.py`).
*   Removed an unused variable in the topological sort utility (`topological.py`).
*   Fixed a minor style issue (removed an extra blank line) in the `cython_imports.py` script.

Closes: #12539
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12539
Pull-request-sha: 837c1e6cb1

Change-Id: Ifa37fb956bc3cacd31967f08bdaa4254e16911c2
2025-04-21 12:35:43 -04:00
Federico Caselli 5cc6a65c61 improve overloads applied to generic functions
try again to remove the overloads to the generic functionn
generator (like coalesce, array_agg, etc).
As of mypy 1.15 it still does now work, but a simpler version
is added in this change

Change-Id: I8b97ae00298ec6f6bf8580090e5defff71e1ceb0
2025-03-25 23:39:05 +01:00
Denis Laxalde 543acbd8d1 Type array_agg()
The return type of `array_agg()` is declared as a `Sequence[T]` where `T` is bound to the type of input argument.

This is implemented by making `array_agg()` inheriting from `ReturnTypeFromArgs` which provides appropriate overloads of `__init__()` to support this.

This usage of ReturnTypeFromArgs is a bit different from previous ones as the return type of the function is not exactly the same as that of its arguments, but a "collection" (a generic, namely a Sequence here) of the argument types.  Accordingly, we adjust the code of `tools/generate_sql_functions.py` to retrieve the "collection" type from 'fn_class' annotation and generate expected return type.

Also add a couple of hand-written typing tests for PostgreSQL.

Related to #6810

Closes: #12461
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12461
Pull-request-sha: ba27cbb863

Change-Id: I3fd538cc7092a0492c26970f0b825bf70ddb66cd
2025-03-24 22:22:35 +01:00
Federico Caselli e6b0b421d6 General improvement on annotated declarative
Fix issue that resulted in inconsistent handing of unions
depending on how they were declared

Consistently support TypeAliasType. This has required a revision
of the implementation added in #11305 to have a consistent
behavior.

References: #11944
References: #11955
References: #11305
Change-Id: Iffc34fd42b9769f73ddb4331bd59b6b37391635d
2024-12-12 22:06:48 +01:00
Federico Caselli d539bc3a0f update the format_docs_code to also work on python files
Change-Id: I0a6c9610b3fd85365ed4c2c199e3cad87ee64022
2024-11-30 21:10:21 +01:00
Federico Caselli 73344fd0d3 Fix select.with_only_columns type hints
Fixes: #11782
Change-Id: Idce218a9730986d3ca70547c83aa1c0f8b5ee5b2
2024-08-28 21:52:30 +02:00
Federico Caselli 0be89aaa38 Switch to cython pure python mode
Replaces the pyx files with py files that can be both compiled
by cython or imported as is by python.
This avoids the need of duplicating the code to have a python
only fallback.
The cython files are also reorganized to be in the module they use
instead of all being in the cyextension package, that has been
removed.

The performance is pretty much equal between main and this change.
A detailed comparison is at this link
https://docs.google.com/spreadsheets/d/1jkmGpnCyEcPyy6aRK9alElGjxlNHu44Wxjr4VrD99so/edit?usp=sharing

Change-Id: Iaed232ea5dfb41534cc9f58f6ea2f912a93263af
2024-03-25 23:44:27 +01:00
Yurii Karabas 00072000c5 Add PEP 646 integration
The :class:`.Row` object now no longer makes use of an intermediary
``Tuple`` in order to represent its individual element types; instead,
the individual element types are present directly, via new :pep:`646`
integration, now available in more recent versions of Mypy.  Mypy
1.7 or greater is now required for statements, results and rows
to be correctly typed.   Pull request courtesy Yurii Karabas.

Fixes: #10635
Closes: #10634
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10634
Pull-request-sha: 430785c8a0

Change-Id: Ibd0ae31a98b4ea69dcb89f970e640920b2be6c48
2024-01-22 19:49:05 +01:00
Mike Bayer cc26af00e7 allow literals for function arguments
* Fixed the argument types passed to functions so that literal expressions
like strings and ints are again interpreted correctly (🎫`10818`)

this includes a reformatting of the changelog message from #10801
to read as a general "fixed regressions" list.

Fixes: #10818
Change-Id: I65ad86e096241863e833608d45f0bdb6069f5896
2024-01-02 19:07:34 -05:00
Mike Bayer 861b6ea5a4 2024 setup
Change-Id: I2853d99bbc19c94227e2b88d450873197013bdfb
2024-01-01 17:03:08 -05:00
Federico Caselli d8cab0694e Normalize all file headers to have consistent format
Change-Id: Idfa5f699280990aed3f6e46225d4202539d9e900
2023-11-30 23:09:09 +01:00
Mike Bayer 045732a738 fully type functions.py
Completed pep-484 typing for the ``sqlalchemy.sql.functions`` module.
:func:`_sql.select` constructs made against ``func`` elements should now
have filled-in return types.

References: #6810
Change-Id: I5121583c9c5b6f7151f811348c7a281c446cf0b8
2023-11-25 09:46:22 -05:00
Federico Caselli 416c5787b6 Enforce positional only attribute using /
Change-Id: Ia73c5b0a2d2a23ed2cf80154fbfb476f4dc84b5f
2023-11-13 19:34:24 +00:00
Carlos Sousa dc8b7cb5fd Add get_one to Session, AsyncSession, scoped, etc
Added method :meth:`_orm.Session.get_one` that behaves like
meth:`_orm.Session.get` but raises an exception instead of returning
None`` if no instance was found with the provided primary key.
Pull request courtesy of Carlos Sousa.

Fixed the :paramref:`_asyncio.AsyncSession.get.execution_options` parameter
which was not being propagated to the underlying :class:`_orm.Session` and
was instead being ignored.

Fixes #10292
Closes: #10376
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10376
Pull-request-sha: 70e4505e93

Change-Id: I78eb9816c26446757b6c6c171df2e400777a3d36
2023-10-01 09:07:44 -04:00
Mike Bayer d46dfd2e45 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.

Fixes: #10321
Change-Id: Ic2247c59b98f462036ad0d734aef9a96f290d778
2023-09-07 18:05:25 -04:00
Federico Caselli c3acf8a5d2 Improve typing tests
Extract a fixture to run mypy on files
Move the plain files to test/typing
Move test files from stubs repository
Transform the fixture module in a package

Change-Id: I23acaecb84e7c4b9010259d44395dc1df83a9385
2023-06-27 23:50:25 +02:00
Federico Caselli 058c230cea Update black to v23.3.0 and flake8 to v6
This change could be added to .git-blame-ignore-revs

Change-Id: I7ba10052b26bc3c178d23fb50a1123d0aae965ca
2023-05-30 21:44:26 +02:00
Mike Bayer cf6872d3bd add deterministic imv returning ordering using sentinel columns
Repaired a major shortcoming which was identified in the
:ref:`engine_insertmanyvalues` performance optimization feature first
introduced in the 2.0 series. This was a continuation of the change in
2.0.9 which disabled the SQL Server version of the feature due to a
reliance in the ORM on apparent row ordering that is not guaranteed to take
place. The fix applies new logic to all "insertmanyvalues" operations,
which takes effect when a new parameter
:paramref:`_dml.Insert.returning.sort_by_parameter_order` on the
:meth:`_dml.Insert.returning` or :meth:`_dml.UpdateBase.return_defaults`
methods, that through a combination of alternate SQL forms, direct
correspondence of client side parameters, and in some cases downgrading to
running row-at-a-time, will apply sorting to each batch of returned rows
using correspondence to primary key or other unique values in each row
which can be correlated to the input data.

Performance impact is expected to be minimal as nearly all common primary
key scenarios are suitable for parameter-ordered batching to be
achieved for all backends other than SQLite, while "row-at-a-time"
mode operates with a bare minimum of Python overhead compared to the very
heavyweight approaches used in the 1.x series. For SQLite, there is no
difference in performance when "row-at-a-time" mode is used.

It's anticipated that with an efficient "row-at-a-time" INSERT with
RETURNING batching capability, the "insertmanyvalues" feature can be later
be more easily generalized to third party backends that include RETURNING
support but not necessarily easy ways to guarantee a correspondence
with parameter order.

Fixes: #9618
References: #9603
Change-Id: I1d79353f5f19638f752936ba1c35e4dc235a8b7c
2023-04-21 11:30:40 -04:00
Federico Caselli c39dc697c1 Add missing overload to Numeric
Added missing init overload to :class:`_sql.Numeric` to allow
type checkers to properly resolve the type var given the
``asdecimal`` parameter.

this fortunately fixes a glitch in the generate_sql_functions script
also

Fixes: #9391
Change-Id: I9cecc40c52711489e9dbe663f110c3b81c7285e4
2023-03-03 10:40:17 -05:00
Mike Bayer 6499098e36 generate stubs for func known functions
Added typing for the built-in generic functions that are available from the
:data:`_sql.func` namespace, which accept a particular set of arguments and
return a particular type, such as for :class:`_sql.count`,
:class:`_sql.current_timestamp`, etc.

Fixes: #9129
Change-Id: I1a2e0dcca3048c77e84dc786843a7df05c457dfa
2023-01-23 11:17:38 -05:00
Mike Bayer cd96ffe287 refactor code generation tools , include --check command
in particular it looks like CI was not picking up on the
"git diff" oriented commands, which were failing to run due
to pathing issues.  As we were setting cwd for black/zimports
relative to sqlalchemy library, and tox installs it in
the venv, black/zimports would fail to run from tox, and
since these are subprocess.run we didn't pick up the
failure.

This overall locks down how zimports/black are run
so that we are definitely from the source root, by using
the location of tools/ to determine the root.

Fixes: #8892
Change-Id: I7c54b747edd5a80e0c699b8456febf66d8b62375
2023-01-18 15:07:55 -05:00
Federico Caselli dce11383f8 Improve sql formatting
change {opensql} to {printsql} in prints, add missing markers

Change-Id: I07b72e6620bb64e329d6b641afa27631e91c4f16
2023-01-11 20:24:29 +01:00
Mike Bayer 66c6b8558a disable polymorphic adaption in most cases
Improved a fix first made in version 1.4 for 🎫`8456` which scaled
back the usage of internal "polymorphic adapters", that are used to render
ORM queries when the :paramref:`_orm.Mapper.with_polymorphic` parameter is
used. These adapters, which are very complex and error prone, are now used
only in those cases where an explicit user-supplied subquery is used for
:paramref:`_orm.Mapper.with_polymorphic`, which includes only the use case
of concrete inheritance mappings that use the
:func:`_orm.polymorphic_union` helper, as well as the legacy use case of
using an aliased subquery for joined inheritance mappings, which is not
needed in modern use.

For the most common case of joined inheritance mappings that use the
built-in polymorphic loading scheme, which includes those which make use of
the :paramref:`_orm.Mapper.polymorphic_load` parameter set to ``inline``,
polymorphic adapters are now no longer used. This has both a positive
performance impact on the construction of queries as well as a
substantial simplification of the internal query rendering process.

The specific issue targeted was to allow a :func:`_orm.column_property`
to refer to joined-inheritance classes within a scalar subquery, which now
works as intuitively as is feasible.

ORM context, mapper, strategies now use ORMAdapter in all cases
instead of straight ColumnAdapter; added some more parameters
to ORMAdapter to make this possible.  ORMAdapter now includes a
"trace" enumeration that identifies the use path for the
adapter and can aid in debugging.

implement __slots__ for the ExternalTraversal hierarchy up
to ORMAdapter.  Within this change, we have to change the
ClauseAdapter.wrap() method, which is only used in one polymorphic
codepath, to use copy.copy() instead of
`__dict__` access (apparently `__reduce_ex__` is implemented for
objects with `__slots__`), and we also remove pickling ability,
which should not be needed for adapters (this might have been needed
for 1.3 and earlier in order for Query to be picklable, but none
of that state is present within Query / select() / etc. anymore).

Fixes: #8168
Change-Id: I3f6593eb02ab5e5964807c53a9fa4894c826d017
2022-12-07 17:35:26 -05:00
Federico Caselli 25c2b7dd10 Add new script to sync test files adding future annotation
Create a new test file test_tm_future_annotations_sync.py that's obtained
from test_tm_future_annotations.py by using the new script sync_test_files.
This files includes the ``from __future__ import annotations``

It also turns out we had some bugs in there and we can also support some
additional typing things

Change-Id: Iac005df206d45a55345d9d88d67a80ce799d449f
2022-11-26 19:25:37 +01:00
Federico Caselli 4eb4ceca36 Try running pyupgrade on the code
command run is "pyupgrade --py37-plus --keep-runtime-typing --keep-percent-format <files...>"
pyupgrade will change assert_ to assertTrue. That was reverted since assertTrue does not
exists in sqlalchemy fixtures

Change-Id: Ie1ed2675c7b11d893d78e028aad0d1576baebb55
2022-11-16 23:03:04 +01:00
Federico Caselli d10b62f54e Improve typings of execution options
Fixes: #8605
Change-Id: I4aec83b9f321462427c3f4ac941c3b272255c088
2022-11-02 21:38:31 -04:00
Federico Caselli 3668889426 Improve proxy methods with the changes from the abandoned
I5c7f172e5224fa08d0675160ac919d0b045a26eb

Change-Id: Ieb7971297f959fd635a4627bd93154e28ef0ef2b
2022-10-18 19:57:19 +02:00
Mike Bayer d4b8b41832 update executemany for new features
also fixes issue in format_docs_code which didn't work with
pre-commit for more than one file.  will backport

Fixes: #8597
Change-Id: I21b2625514987b1cd90f7c00f06e72e57e257390
2022-10-11 15:29:15 -04:00
Federico Caselli 153a95a38d Add format docs to pre-commits
Change-Id: Ia41399155ee0ec1b878aebf18967eabe38f5afd1
2022-10-06 22:53:37 +02:00
mike bayer d5905ccd7d Merge "use black 22.8.0" into main 2022-10-03 22:27:20 +00:00
Mike Bayer a45e5f1754 use black 22.8.0
nothing happened moving from 22.3 to 22.8.  a full run of pre-commit
found some little things in the generate_proxy_methods script.

Change-Id: Ie6b4235b87bbfcb252759c0afb133ab8d0cf51db
2022-10-03 17:40:08 -04:00
Federico Caselli a5252fa366 Remove all formatting errors
Improve format docs script, replace {sql} with {opensql}

Change-Id: Ie1aaa8f3d8ff8f8e89b7b5149a1876d9f8a211ed
2022-10-03 17:44:20 +02:00
Mike Bayer 7051dc5842 detect {opensql} and {stop} sections
this so that I can still have
{opensql} and {stop} sections in non-console python.

this isn't the norm but I would prefer if I dont have to
be 100% strict about it

also maintaining {sql} / {stop} being at the start
of a code line.  this is more prevalent in 1.4.

Change-Id: Iaf748b7ff1120e21f729c2fd794d9b8a33d83170
2022-10-02 14:16:34 -04:00
Federico Caselli 04a72ff3a9 Add proper code block formatting
Change-Id: I63585eeae0b0bc78109da64520696928dfb3982c
2022-10-02 11:51:02 -04:00
Federico Caselli dcbda9f1ea Improvements to code formatter
Change-Id: I75cf7143f3ed3bbc09aa8bc18edbce5c8af0f0be
2022-10-02 10:55:42 -04:00
Federico Caselli 23dbf572ce Format code in the rst docs file
Added script to format code in the rst documentation using black.
This is also added to the lint tox job to ensure that the code
in the docs is properly formatted.

Change-Id: I799444f22da153484ca5f095d57755762348da40
2022-09-30 14:39:48 -04:00