- fix mypy and linter issues
- expand BitString tests for int and bytes conversion
- Only run BIT test types on postgresql dialect
- Fix problems in `BitString.from_int` and `BitString.from_bytes`
- Add `BitString.to_bytes` method to allow specifying the byte
length of the returned instances
- Fixed problems testing operators in BIT type tests
- Added custom `BitString` implementation which inherits from `str`
and overrides methods and operators appropriately and
implements the bitwise operations in a manner consistent with
postgresql. Also exposes class and instance methods ensuring
instances can be converted to/from `int` and `bytes` sensibly.
- Added default implementations of `bind_processor` and
`result_processor` to `postgresql.BIT` type to convert `BitString`
to/from string for PG drivers.
- Override `bind_processor` and `result_processor` in AsyncpgBit
in order to convert `BitString` to/from `asynpg.BitString`
in `asyncpg` driver.
- Added support for bitwise comparators to postgresql `BIT` instances
Fixes: 10556
Fixed and added test support for a few SQLite SQL functions hardcoded into
the compiler most notably the "localtimestamp" function which rendered with
incorrect internal quoting.
Fixes: #12566
Change-Id: Id5bd8dc7841f0afab7df031ba5c0854dab845a1d
Redundant variables and unnecessary conditions were removed across several modules. Improved readability and reduced code complexity without changing functionality.
Closes: #12537
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12537
Pull-request-sha: ab53f8c348
Change-Id: I910d65729fdbc96933f9822c553924d37e89e201
Identified some unnecessary cycles and overhead in how
this is implemented. since we want to add this to Select,
needs these improvements.
Change-Id: I4324db14aaf52ab87a8b7fa49ebf1b6624bc2dcb
Added support for the pow operator (``**``), with a default SQL
implementation of the ``POW()`` function. On Oracle Database, PostgreSQL
and MSSQL it renders as ``POWER()``. As part of this change, the operator
routes through a new first class ``func`` member :class:`_functions.pow`,
which renders on Oracle Database, PostgreSQL and MSSQL as ``POWER()``.
Fixes: #8579Closes: #8580
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8580
Pull-request-sha: 041b2ef474
Change-Id: I371bd44ed3e58f2d55ef705aeec7d04710c97f23
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
Removed unused variables to improve code clarity and maintainability. This change simplifies logic in `base.py`, `default.py`, and `result.py`. No functionality was altered.
Closes: #12535
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12535
Pull-request-sha: a9d849f3a4
Change-Id: If78b18dbd33733c631f8b5aad7d55261fbc4817b
**Title:** Removed unused variables and redundant functions across multiple dialects. Improves code readability and reduces maintenance complexity without altering functionality.
### Description
This pull request introduces several minor refactorings across different dialect modules:
- **MSSQL:**
- Simplified the initialization of the `fkeys` dictionary in `_get_foreign_keys` using `util.defaultdict` directly.
- **MySQL:** Removed the unused variable in `_get_table_comment`. `rp`
- **PostgreSQL (_psycopg_common):** Removed the unused variable `cursor` in `do_ping`.
- **PostgreSQL (base):** Removed the unused variable `args` in `_get_column_info`.
- **SQLite:** Removed the unused variable `new_filename` in `generate_driver_url`.
These changes focus purely on code cleanup and simplification, removing dead code and improving clarity. They do not alter the existing logic or functionality of the dialects.
### Checklist
This pull request is:
- [ ] A documentation / typographical / small typing error fix
- [x] A short code fix
- _Note: This is a general cleanup refactor rather than a fix for a specific reported issue._
- [ ] A new feature implementation
**Have a nice day!**
Closes: #12534
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12534
Pull-request-sha: 2c7ae17b73
Change-Id: I1ec3b48f42aea7e45bc20f81add03051eb30bb98
Just as we got this driver "working", a new regression is introduced
in version 9.3.0 which prevents basic binary string persistence [1].
I would say we need to leave this driver off for another few years
until something changes with its upstream maintenance.
[1] https://bugs.mysql.com/bug.php?id=118025
Change-Id: If876f63ebb9a6f7dfa0b316df044afa469a154f2
towards some refactorings I will need to do for #12496, this
factors out the "_ordered_values" list of tuples that was used to
track UPDATE VALUES in a specific order. The rationale for this
separate collection was due to Python dictionaries not maintaining
insert order. Now that this is standard behavior in Python 3
we can use the same `statement._values` for param-ordered and
table-column-ordered UPDATE rendering.
Change-Id: Id6024ab06e5e3ba427174e7ba3630ff83d81f603
### Description
util.decorator uses code generation + eval to create signature matching wrapper.
It consumes some CPU because we can not use pyc cache.
Additionally, each wrapped function has own globals for function annotations.
By stripping function annotations from eval-ed code, compile time and memory usage are saved.
```python
from sqlalchemy.util import decorator
from sqlalchemy import *
import timeit
import tracemalloc
import sqlalchemy.orm._orm_constructors
@decorator
def with_print(fn, *args, **kwargs):
res = fn(*args, **kwargs)
print(f"{fn.__name__}(*{args}, **{kwargs}) => {res}")
return res
# test
PI = 3.14
def f():
@with_print
def add(x: int|float, *, y: int|float=PI) -> int|float:
return x + y
return add
add = f()
add(1)
print(add.__annotations__)
# benchmark
print(timeit.timeit(f, number=1000)*1000, "us")
# memory
tracemalloc.start(1)
[f() for _ in range(1000)]
mem, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print(f"{mem=}, {peak=}")
```
Result:
```
$ .venv/bin/python -VV
Python 3.14.0a6 (main, Mar 17 2025, 21:27:10) [Clang 20.1.0 ]
$ .venv/bin/python sample.py
add(*(1,), **{'y': 3.14}) => 4.140000000000001
{'x': int | float, 'y': int | float, 'return': int | float}
35.93937499681488 us
mem=9252896, peak=9300808
$ git switch -
Switched to branch 'opt-decorator'
$ .venv/bin/python sample.py
add(*(1,), **{'y': 3.14}) => 4.140000000000001
{'x': int | float, 'y': int | float, 'return': int | float}
23.32574996398762 us
mem=1439032, peak=1476423
```
### 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 / small typing 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.
Closes: #12502
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12502
Pull-request-sha: 34409cbbfd
Change-Id: I88b88eb6eb018608bc2881459f58564881d06641
Fixed regression caused by the DEFAULT rendering changes in 2.0.40
🎫`12425` where using lowercase `on update` in a MySQL server default
would incorrectly apply parenthesis, leading to errors when MySQL
interpreted the rendered DDL. Pull request courtesy Alexander Ruehe.
Fixes: #12488Closes: #12489
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12489
Pull-request-sha: b9008f747d
Change-Id: If5281c52415e4ddb6c2f8aee191d2335f6673b35
This is supported both for schema definition and reflection.
Fixes#10665.
Closes: #12485
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12485
Pull-request-sha: 1aabea7b55ece9fc0c6e069b777d4404ac01f964
Change-Id: I81d23966f84390dd1b03f0d13284ce6d883ee24e
Fixed issue where :meth:`.AsyncSession.get_transaction` and
:meth:`.AsyncSession.get_nested_transaction` would fail with
``NotImplementedError`` if the "proxy transaction" used by
:class:`.AsyncSession` were garbage collected and needed regeneration.
Fixes: #12471
Change-Id: Ia8055524618df706d7958786a500cdd25d9d8eaf
Implemented support for the GROUPS frame specification in window functions
by adding :paramref:`_sql.over.groups` option to :func:`_sql.over`
and :meth:`.FunctionElement.over`. Pull request courtesy Kaan Dikmen.
Fixes: #12450Closes: #12445
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12445
Pull-request-sha: c0808e135f
Change-Id: I9ff504a9c9650485830c4a0eaf44162898a3a2ad
Fixed regression caused by ``typing_extension==4.13.0`` that introduced
a different implementation for ``TypeAliasType`` while SQLAlchemy assumed
that it would be equivalent to the ``typing`` version.
Added test regarding generic TypeAliasType
Fixes: #12473Closes: #12472
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12472
Pull-request-sha: 8861a5acfb
Change-Id: I053019a222546a625ed6d588314ae9f5b34c2f8a
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
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 #6810Closes: #12461
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12461
Pull-request-sha: ba27cbb863
Change-Id: I3fd538cc7092a0492c26970f0b825bf70ddb66cd