Commit Graph

15667 Commits

Author SHA1 Message Date
mike bayer 372ea0893b Merge "Add recommendation for URL.create() re: escaping" into main 2022-11-27 16:32:53 +00:00
Gord Thompson 14c73685ba Add recommendation for URL.create() re: escaping
Let users know that URL.create() can build the
whole connection URL instead of making them
escape things like passwords ad-hoc.

includes some general cleanup of URL docstring
by mike

Change-Id: Ic71bb0201fecf30e1db11e006c269f2d041b5439
2022-11-27 10:41:33 -05:00
Federico Caselli 61443aa62b Implement ScalarValue
Added :class:`_expression.ScalarValues` that can be used as a column
element allowing using :class:`_expression.Values` inside IN clauses
or in conjunction with ``ANY`` or ``ALL`` collection aggregates.
This new class is generated using the method
:meth:`_expression.Values.scalar_values`.
The :class:`_expression.Values` instance is now coerced to a
:class:`_expression.ScalarValues` when used in a ``IN`` or ``NOT IN``
operation.

Fixes: #6289
Change-Id: Iac22487ccb01553684b908e54d01c0687fa739f1
2022-11-26 18:49:06 -05:00
Mike Bayer 5cc3825da3 add new variation helper
I'm using a lot of @testing.combinations with either
a boolean True/False, or a series of string names, each indicating
some case to switch on.  I want a descriptive name in the test
run (not True/False) and I don't want to compare strings.

So make a new helper around @combinations that provides an
object interface that has booleans inside of it, prints nicely
in the test output, raises an error if you name the case
incorrectly.

Before:

test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[False-False-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[False-False-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[False-False-name] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[False-True-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[False-True-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[False-True-name] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[True-False-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[True-False-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[True-False-name] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[True-True-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[True-True-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[True-True-name] PASSED

After:

test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[not_use_add_property-deferred-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[not_use_add_property-deferred-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[not_use_add_property-deferred-name] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[not_use_add_property-not_deferred-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[not_use_add_property-not_deferred-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[not_use_add_property-not_deferred-name] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[use_add_property-deferred-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[use_add_property-deferred-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[use_add_property-deferred-name] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[use_add_property-not_deferred-both] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[use_add_property-not_deferred-key] PASSED
test/orm/declarative/test_typed_mapping.py::MappedColumnTest::test_separate_name[use_add_property-not_deferred-name] PASSED

Change-Id: Idde87632581ee69e0f47360966758583dfd8baab
2022-11-26 17:24:04 -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
Mike Bayer 34e29f7b22 sort for addresess relationship in new merge test
saw a random failure under py2 on 1.4, will backport

Change-Id: I3e2b037bf4211be44e28f85f5e51ffdc218eeb5a
2022-11-26 10:14:02 -05:00
mike bayer 0386228f21 Merge "assert unique constraints received back" into main 2022-11-26 02:32:20 +00:00
Mike Bayer df3bd8d297 add "merge" to viewonly cascades; propagate NO_RAISE when merging
Fixed bug where :meth:`_orm.Session.merge` would fail to preserve the
current loaded contents of relationship attributes that were indicated with
the :paramref:`_orm.relationship.viewonly` parameter, thus defeating
strategies that use :meth:`_orm.Session.merge` to pull fully loaded objects
from caches and other similar techniques. In a related change, fixed issue
where an object that contains a loaded relationship that was nonetheless
configured as ``lazy='raise'`` on the mapping would fail when passed to
:meth:`_orm.Session.merge`; checks for "raise" are now suspended within
the merge process assuming the :paramref:`_orm.Session.merge.load`
parameter remains at its default of ``True``.

Overall, this is a behavioral adjustment to a change introduced in the 1.4
series as of 🎫`4994`, which took "merge" out of the set of cascades
applied by default to "viewonly" relationships. As "viewonly" relationships
aren't persisted under any circumstances, allowing their contents to
transfer during "merge" does not impact the persistence behavior of the
target object. This allows :meth:`_orm.Session.merge` to correctly suit one
of its use cases, that of adding objects to a :class:`.Session` that were
loaded elsewhere, often for the purposes of restoring from a cache.

Fixes: #8862
Change-Id: I8731c7810460e6a71f8bf5e8ded59142b9b02956
2022-11-24 11:50:48 -05:00
Mike Bayer f99300c4e8 assert unique constraints received back
in #8867 we can see our existing uq reflection test is
broken, not detecting a failure to detect constraints

Change-Id: Icada02bc0547c5a3d8c471b80a78a2e72f02647d
2022-11-24 09:52:12 -05:00
Mike Bayer fbec926c47 flake8 has very courteously referred us to another pyqca project and closed all comments, thank you! I'll get on that right away
pyqca/flake8-import-order does not seem to have a release or a commit
in the past two years, so while I have created an issue and PR [1] [2],
for now vendor our fork so we can get on with things.

Also fix one issue for flake8 6.0

[1] https://github.com/PyCQA/flake8-import-order/issues/189
[2] https://github.com/PyCQA/flake8-import-order/pull/190

Change-Id: I53045f65b8716cceaf2104fccc1d26f80b398fef
References: https://github.com/PyCQA/flake8/issues/1751
2022-11-23 17:44:30 -05:00
Mike Bayer 1c9aa8c046 improve docs for event listening with asyncio
add examples, including how to listen on an async_sessionmaker

Change-Id: I1d68324458c23bc830508c8ebfd3224d434c16a5
2022-11-23 13:11:26 -05:00
mike bayer 3b6a5fb05c Merge "ensure implicit_returning is checked on a Table instance" into main 2022-11-22 23:30:36 +00:00
Mike Bayer ab84559ca0 ensure implicit_returning is checked on a Table instance
Fixed regression where flushing a mapped class that's mapped against a
subquery, such as a direct mapping or some forms of concrete table
inheritance, would fail if the :paramref:`_orm.Mapper.eager_defaults`
parameter were used.

mapper.local_table can still be a non-table instance.
correct the check first added in 466ed5b53a.

Fixes: #8812
Change-Id: I1bb2b83c31b910fbd96fcd3ac43e789b657aebf7
2022-11-22 14:06:09 -05:00
mike bayer 38db017a2c Merge "Type annotations for sqlalchemy.ext.mutable" into main 2022-11-22 16:42:45 +00:00
mike bayer cbc42334b1 Merge "fix optionalized forms for dict[]" into main 2022-11-22 16:07:49 +00:00
Gleb Kisenkov ba0e508141 Type annotations for sqlalchemy.ext.mutable
The ``sqlalchemy.ext.mutable`` extension is now fully pep-484 typed. Huge
thanks to Gleb Kisenkov for their efforts on this.

Fixes: #8667
Closes: #8775
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8775
Pull-request-sha: b907888ec6

Change-Id: Id9224e03201e6970b1ec56eb546ece4b2f3e0edd
2022-11-22 10:39:41 -05:00
Mike Bayer 509ffeedef fix optionalized forms for dict[]
Fixed a suite of issues involving :class:`.Mapped` use with dictionary
types, such as ``Mapped[dict[str, str] | None]``, would not be correctly
interpreted in Declarative ORM mappings. Support to correctly
"de-optionalize" this type including for lookup in type_annotation_map has
been fixed.

Fixes: #8777
Change-Id: Iaba90791dea341d00eaff788d40b0a4e48dab02e
2022-11-22 09:49:02 -05:00
Federico Caselli 447249e862 Add security warning to serializer extension
Change-Id: I5c7c076bc93fc250c05f7996e83359d19d1f3214
2022-11-22 00:07:53 +01:00
mike bayer 4f0da8f07a Merge "Fix inferred type of async_sessionmaker" into main 2022-11-21 21:24:11 +00:00
mike bayer 5ff55f7078 Merge "add common base class for all SQL col expression objects" into main 2022-11-21 21:23:12 +00:00
Sam Bull e43e240a00 Fix inferred type of async_sessionmaker
Improved the typing for :class:`.sessionmaker` and
:class:`.asyncsessionmaker`, so that the default type of their return value
will be :class:`.Session` or :class:`.AsyncSession`, without the need to
type this explicitly. Previously, Mypy would not automaticaly infer these
return types from its generic base.

As part of this change, arguments for :class:`.Session`,
:class:`.AsyncSession`, :class:`.sessionmaker` and
:class:`.asyncsessionmaker` beyond the initial "bind" argument have been
made keyword-only, which includes parameters that have always been
documented as keyword arguments, such as :paramref:`.Session.autoflush`,
:paramref:`.Session.class_`, etc.

Pull request courtesy Sam Bull.

Closes: #8842
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8842
Pull-request-sha: fa6d1a8468

Change-Id: Iaaabc4572a87489d61617d970f62b9b50db4fac7
2022-11-21 12:01:56 -05:00
Mike Bayer caa9293e2e add common base class for all SQL col expression objects
Added a new type :class:`.SQLColumnExpression` which may be indicated in
user code to represent any SQL column oriented expression, including both
those based on :class:`.ColumnElement` as well as on ORM
:class:`.QueryableAttribute`. This type is a real class, not an alias, so
can also be used as the foundation for other objects.

Fixes: #8847
Change-Id: I3161bdff1c9f447793fce87864e1774a90cd4146
2022-11-21 11:55:23 -05:00
Mike Bayer ece524c005 fall back to eval() for names that have dots
Fixed regression in 2.0.0b3 caused by 🎫`8759` where indicating the
:class:`.Mapped` name using a qualified name such as
``sqlalchemy.orm.Mapped`` would fail to be recognized by Declarative as
indicating the :class:`.Mapped` construct.

Fixes: #8853
Change-Id: Iddb6efaae864d4545e80c54658244670f81ef6cc
2022-11-21 08:52:12 -05:00
Mike Bayer 46e6693cb3 clarify role of __allow_unmapped__
A different note will be needed in the 1.4 version
of these docs.

Fixes: #8845
Change-Id: I48651c64d511684077c80a317349dd230424b575
2022-11-19 13:10:31 -05:00
mike bayer 1fd68ac20a Merge "Issue #8765: implement missing methods on PG Range" into main 2022-11-17 14:37:57 +00:00
mike bayer 60cc662861 Merge "Try running pyupgrade on the code" into main 2022-11-17 14:35:10 +00:00
Mike Bayer 200e70b974 accommodate NULL format_type()
Made an adjustment to how the PostgreSQL dialect considers column types
when it reflects columns from a table, to accommodate for alternative
backends which may return NULL from the PG ``format_type()`` function.

Fixes: #8748
Change-Id: I6178287aac567210a76afaa5805b825daa7fa4db
2022-11-16 20:11:18 -05: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
mike bayer 3fc6c40ea7 Merge "perf improvements related to corresponding_column (2)" into main 2022-11-16 00:03:08 +00:00
mike bayer 073553be44 Merge "don't invoke fromclause.c when creating an annotated" into main 2022-11-16 00:01:33 +00:00
Lele Gaifax bc696220bb Issue #8765: implement missing methods on PG Range
### Description
This PR implements missing methods on the PG `Range` class, as described by issue #8765.

### Checklist
This pull request is:

- [ ] A documentation / typographical error fix
- [ ] A short code fix
- [x] A new feature implementation

Closes: #8766
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8766
Pull-request-sha: 21c0df86cc

Change-Id: I86fabd966ad1f14a3a86132be741df46965b9aa9
2022-11-15 15:27:34 -05:00
Mike Bayer d5be2cc139 perf improvements related to corresponding_column (2)
commit two of two. this reorganizes ColumnCollection
to build a new index up front that's used to optimize
the corresponding_column() method.

Additional performance enhancements within ORM-enabled SQL statements,
specifically targeting callcounts within the construction of ORM
statements, using combinations of :func:`_orm.aliased` with
:func:`_sql.union` and similar "compound" constructs, in addition to direct
performance improvements to the ``corresponding_column()`` internal method
that is used heavily by the ORM by constructs like :func:`_orm.aliased` and
similar.

Fixes: #8796
Change-Id: I4a76788007d5a802b9a4081e6a0f6e4b52497b50
2022-11-15 14:16:06 -05:00
Mike Bayer 93dc7ea150 don't invoke fromclause.c when creating an annotated
The ``aliased()`` constructor calls upon ``__clause_element__()``,
which internally annotates a ``FromClause``, like a subquery.
This became expensive as ``AnnotatedFromClause`` has for
many years called upon ``element.c`` so that the full ``.c``
collection is transferred to the Annotated.

Taking this out proved to be challenging. A straight remove
seemed to not break any tests except for the one that
tested the exact condition.  Nevertheless this seemed
"spooky" so I instead moved the get of ``.c`` to be in a
memoized proxy method.   However, that then exposed
a recursion issue related to loader_criteria; so the
source of that behavior, which was an accidental behavioral
artifact, is now made into an explcicit option that
loader_criteria uses directly.

The accidental behavioral artifact in question is still
kind of strange since I was not able to fully trace out
how it works, but the end result is that fixing the
artifact to be "correct" causes loader_criteria, within
the particular test for #7491, creates a select/
subquery structure with a cycle in it, so compilation fails
with recursion overflow.
The "solution" is to cause the artifact to occur in this
case, which is that the ``AnnotatedFromClause`` will have a
different ``.c`` collection than its element, which is a
subquery.  It's not totally clear how a cycle is generated
when this is not done.

This is commit one of two, which goes through
some hoops to make essentially a one-line change.

The next commit will rework ColumnCollection to optimize
the corresponding_column() method significantly.

Fixes: #8796
Change-Id: Id58ae6554db62139462c11a8be7313a3677456ad
2022-11-15 13:46:02 -05:00
mike bayer e723db6c46 Merge "Fix relationship.order_by typing" into main 2022-11-15 18:14:24 +00:00
Andy Garfield 36954a3dbb Small tutorial rewording
The language is this sentence took me a few reads to understand. This is just a rewording.
2022-11-15 11:28:58 -05:00
Bryan Forbes 2fdfb5527e Fix relationship.order_by typing
Fixes: #8776

### Checklist

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.

Closes: #8779
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8779
Pull-request-sha: 2bc64555a1

Change-Id: I52fe6db5ba954a6039836b45f0e89ad96049df03
2022-11-15 09:03:25 -05:00
Eitan Mosenkis 9237bf15e6 Explicitly state what happens if order_by is called more than once. (#8791)
* Explicitly state what happens if `order_by` is called more than once.

The existing docs cover how to clear existing `order_by` clauses but don't actually describe the behavior of calling `order_by` multiple times with different clauses.

* Also update Select.order_by.
2022-11-14 22:11:15 +01:00
Mike Bayer eea0f44bbd add informative exception context for literal render
An informative re-raise is now thrown in the case where any "literal
bindparam" render operation fails, indicating the value itself and
the datatype in use, to assist in debugging when literal params
are being rendered in a statement.

Fixes: #8800
Change-Id: Id658f8b03359312353ddbb0c7563026239579f7b
2022-11-14 10:28:40 -05:00
Federico Caselli 9f4ac8d155 Repair test in 32bit python builds.
Change-Id: I8287f3e1a975534c8a01a41c9dcc7e5e9f08bb52
2022-11-12 20:15:26 +01:00
Mike Bayer 1a81cc3f39 cherry-pick changelog update for 1.4.45 2022-11-12 09:13:38 -05:00
Mike Bayer d89659aef8 cherry-pick changelog from 1.4.44 2022-11-12 09:13:36 -05:00
Mike Bayer 5fc22d0e64 port mypy 0.990 fixes changelog from 1.4 to 2.0
for 2.0, we fixed for mypy 0.990 in ebb54e80a5.
In 1.4, we did a smaller version specific to the
test runner in 910504b67c.  Since people
run the tests sometimes, include that this was fixed
for the 1.4/2.0 series.

Change-Id: I18b2aa10bd8a128b5aa093af5e219bc987271139
2022-11-11 21:13:06 -05:00
mike bayer 6b140afe7d Merge "ensure anon_map is passed for most annotated traversals" into main 2022-11-11 21:30:43 +00:00
mike bayer 1dd0f23e8d Merge "establish consistency for RETURNING column labels" into main 2022-11-11 21:00:21 +00:00
Mike Bayer 1d8833a9c1 ensure anon_map is passed for most annotated traversals
We can cache the annotated cache key for Table, but
for selectables it's not safe, as it fails to pass the
anon_map along and creates many redudant structures in
observed test scenario.  It is likely safe for a
Column that's mapped to a Table also, however this is
not implemented here.   Will have to see if that part
needs adjusting.

Fixed critical memory issue identified in cache key generation, where for
very large and complex ORM statements that make use of lots of ORM aliases
with subqueries, cache key generation could produce excessively large keys
that were orders of magnitude bigger than the statement itself. Much thanks
to Rollo Konig Brock for their very patient, long term help in finally
identifying this issue.

Also within TypeEngine objects, when we generate elements
for instance variables, skip the None elements at least.
this also saves on tuple complexity.

Fixes: #8790
Change-Id: I448ddbfb45ae0a648815be8dad4faad7d1977427
2022-11-11 15:25:35 -05:00
Mike Bayer 604611e7e5 repair --disable-asyncio parameter
Fixed issue where the ``--disable-asyncio`` parameter to the test suite
would fail to not actually run greenlet tests and would also not prevent
the suite from using a "wrapping" greenlet for the whole suite. This
parameter now ensures that no greenlet or asyncio use will occur within the
entire run when set.

Fixes: #8793
Change-Id: I87b510846b2cc24413cd54e7b7136e91aad3c309
2022-11-11 11:20:34 -05:00
Mike Bayer 8e91cfe529 establish consistency for RETURNING column labels
The RETURNING clause now renders columns using the routine as that of the
:class:`.Select` to generate labels, which will include disambiguating
labels, as well as that a SQL function surrounding a named column will be
labeled using the column name itself. This is a more comprehensive change
than a similar one made for the 1.4 series that adjusted the function label
issue only.

includes 1.4's changelog for the backported version which also
fixes an Oracle issue independently of the 2.0 series.

Fixes: #8770
Change-Id: I2ab078a214a778ffe1720dbd864ae4c105a0691d
2022-11-11 16:20:00 +00:00
Mike Bayer e3a8d19891 work around Python 3.11 IntEnum issue; update FastIntFlag
in [1], Python 3.11 seems to have changed the behavior of
IntEnum.  We didn't notice this because we have our own
workaround class already, but typing did.   Ensure we remain
compatible with IntFlag.

This change also modifies FastIntFlag to no longer use
global symbols; this is unnecessary as we assign FastIntFlag
members explicitly.  Use of ``symbol()`` should probably
be phased out.

[1] https://github.com/python/cpython/issues/99304
Fixes: #8783

Change-Id: I8ae2e871ff1467ae5ca1f63e66b5dae45d4a6c93
2022-11-10 12:19:02 -05:00
Mike Bayer ebb54e80a5 try to support mypy 0.990
mypy introduces a crash we need to work around, also
some new rules.  It also has either a behavioral change
regarding how output is rendered in relationship to
files being within sys.path or not, so work around
that for test_mypy_plugin_py3k.py

References: https://github.com/python/mypy/issues/14027
Change-Id: I689c7fe27dc52abee932de9e0fb23b2a2eba76fa
2022-11-09 15:02:29 -05:00
Mike Bayer 8745dcf3e1 test run w/ oracledb on pypi, disable mariadb-connector
mariadb is still failing:

https://jira.mariadb.org/projects/CONPY/issues/CONPY-236

Change-Id: I63a82ce8b33acb7f550d509cd6ed0b8e7a2bd56c
2022-11-08 19:14:21 -05:00