Commit Graph

10232 Commits

Author SHA1 Message Date
Mike Bayer 01c50c64e3 Remove all remaining removed_in_20 warnings slated for removal
Finalize all remaining removed-in-2.0 changes so that we
can begin doing pep-484 typing without old things
getting in the way (we will also have to do public_factory).

note there are a few "moved_in_20()" and "became_legacy_in_20()"
warnings still in place.  The SQLALCHEMY_WARN_20 variable
is now removed.

Also removed here are the legacy "in place mutators" for Select
statements, and some keyword-only argument signatures in Core
have been added.

Also in the big change department, the ORM mapper() function
is removed entirely; the Mapper class is otherwise unchanged,
just the public-facing API function.  Mappers are now always
given a registry in which to participate, however the
argument signature of Mapper is not changed. ideally "registry"
would be the first positional argument.

Fixes: #7257

Change-Id: Ic70c57b9f1cf7eb996338af5183b11bdeb3e1623
2022-01-05 19:28:49 -05:00
Hugo van Kemenade 146a349d81 Update Black's target-version to py37
<!-- Provide a general summary of your proposed changes in the Title field above -->

### Description
<!-- Describe your changes in detail -->

Black's `target-version` was still set to `['py27', 'py36']`. Set it to `[py37]` instead.

Also update Black and other pre-commit hooks and re-format with Black.

### 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
- [ ] 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.

**Have a nice day!**

Closes: #7536
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7536
Pull-request-sha: b3aedf5570

Change-Id: I8be85636fd2c9449b07a8626050c8bd35bd119d5
2022-01-05 12:41:32 -05:00
mike bayer 028f9b5cac Merge "implement python_impl to custom_op for basic ORM evaluator extensibility" into main 2022-01-04 22:32:48 +00:00
Mike Bayer 63eeec396e implement python_impl to custom_op for basic ORM evaluator extensibility
Added new parameter :paramref:`_sql.Operators.op.python_impl`, available
from :meth:`_sql.Operators.op` and also when using the
:class:`_sql.Operators.custom_op` constructor directly, which allows an
in-Python evaluation function to be provided along with the custom SQL
operator. This evaluation function becomes the implementation used when the
operator object is used given plain Python objects as operands on both
sides, and in particular is compatible with the
``synchronize_session='evaluate'`` option used with
:ref:`orm_expression_update_delete`.

Fixes: #3162
Change-Id: If46ba6a0e303e2180a177ba418a8cafe9b42608e
2022-01-04 16:40:35 -05:00
Mike Bayer 9298ce03e1 update stale documentation on Mapper class
don't get into details here that have changed,
point to the main narrative documentation instead.

Change-Id: I5bc0829f6ff282ca104f18deb9ec9857b6e1c8ac
2022-01-04 15:38:15 -05:00
Federico Caselli 4dfc24cda5 Merge "Imrpove MySQL/MariaDB dialect initialization." into main 2022-01-04 20:30:16 +00:00
Federico Caselli 5a76f52256 Merge "Improve array of enum handling." into main 2022-01-04 20:28:41 +00:00
Federico Caselli 94afc4f5fc Improve array of enum handling.
Fixed handling of array of enum values which require escape characters.

Fixes: #7418
Change-Id: I50525846f6029dfea9a8ad1cb913424d168d5f62
2022-01-04 14:33:46 -05:00
Mike Bayer 709239f4a6 ensure correlate_except is checked for empty tuple
Fixed issue where :meth:`_sql.Select.correlate_except` method, when passed
either the ``None`` value or no arguments, would not correlate any elements
when used in an ORM context (that is, passing ORM entities as FROM
clauses), rather than causing all FROM elements to be considered as
"correlated" in the same way which occurs when using Core-only constructs.

Fixes: #7514
Change-Id: Ic4a5252c8f3c1140aba6c308264948f3a91f33f5
2022-01-03 17:39:33 -05:00
Federico Caselli 5e06f0f579 Imrpove MySQL/MariaDB dialect initialization.
Replace ``SHOW VARIABLES LIKE`` statement with equivalent
``SELECT @@variable`` in MySQL and MariaDB dialect initialization.
This should avoid mutex contention caused by ``SHOW VARIABLES``,
improving initialization performance.

Change-Id: Id836ef534fcc1473c7aaf9270d08a4da9b8f62cf
closes: #7518
2022-01-03 21:41:33 +01:00
Mike Bayer eee7a3add9 remove 2.0-removed Query elements
* :meth:`_orm.Query.join` no longer accepts the "aliased" and
"from_joinpoint" arguments

* :meth:`_orm.Query.join` no longer accepts chains of multiple join
targets in one method call.

* ``Query.from_self()`` and ``Query.with_polymorphic()``
are removed.

Change-Id: I534d04b53a538a4fc374966eb2bc8eb98a16497d
References: #7257
2022-01-01 11:28:24 -05:00
Federico Caselli e913ec8155 Properly type _generative, decorator, public_factory
Good new is that pylance likes it and copies over the
singature and everything.
Bad news is that mypy does not support this yet https://github.com/python/mypy/issues/8645
Other minor bad news is that non_generative is not typed. I've tried using a protocol
like the one in the comment but the signature is not ported over by pylance, so it's
probably best to just live without it to have the correct signature.

notes from mike:  these three decorators are at the core of getting
the library to be typed, more good news is that pylance will
do all the things we like re: public_factory, see
https://github.com/microsoft/pyright/issues/2758#issuecomment-1002788656
.

For @_generative, we will likely move to using pep 673 once mypy
supports it which may be soon.  but overall having the explicit
"return self" in the methods, while a little inconvenient, makes
the typing more straightforward and locally present in the files
rather than being decided at a distance.   having "return self"
present, or not, both have problems, so maybe we will be able
to change it again if things change as far as decorator support.
As it is, I feel like we are barely squeaking by with our decorators,
the typing is already pretty out there.

Change-Id: Ic77e13fc861def76a5925331df85c0aa48d77807
References: #6810
2021-12-30 18:07:26 -05:00
mike bayer 54875c2160 Merge "remove legacy select patterns" into main 2021-12-29 19:19:42 +00:00
Mike Bayer 97d81d346f remove legacy select patterns
Change-Id: If6e521a1eb461e08748a0432943b938528a2619e
References: #7257
2021-12-29 12:23:12 -05:00
Mike Bayer 9e3c8d0d71 replace Variant with direct feature inside of TypeEngine
The :meth:`_sqltypes.TypeEngine.with_variant` method now returns a copy of
the original :class:`_sqltypes.TypeEngine` object, rather than wrapping it
inside the ``Variant`` class, which is effectively removed (the import
symbol remains for backwards compatibility with code that may be testing
for this symbol). While the previous approach maintained in-Python
behaviors, maintaining the original type allows for clearer type checking
and debugging.

Fixes: #6980
Change-Id: I158c7e56306b886b5b82b040205c428a5c4a242c
2021-12-29 11:43:53 -05:00
Mike Bayer 3210348fd4 use long long for id() result
per https://stackoverflow.com/a/27055236/34549 apparently there
is "long long" in cython.

Change-Id: I08bfaaccbf87ec9a2dc0d4b154f9c9d454171342
2021-12-28 10:54:18 -05:00
mike bayer 1afa91fc9f Merge "Reflect included columns as dialect_options" into main 2021-12-27 22:07:10 +00:00
Federico Caselli c5b8f2f88b Merge "Replace raise_ with raise from" into main 2021-12-27 21:04:54 +00:00
Federico Caselli 13e6e56088 Replace raise_ with raise from
Change-Id: I7aaeb5bc130271624335b79cf586581d6c6c34c7
References: #4600
2021-12-27 21:03:33 +01:00
Gord Thompson 5ada58954a Reflect included columns as dialect_options
Fixed reflection of covering indexes to report ``include_columns`` as part
of the ``dialect_options`` entry in the reflected index dictionary, thereby
enabling round trips from reflection->create to be complete. Included
columns continue to also be present under the ``include_columns`` key for
backwards compatibility.

Fixes: #7382
Change-Id: I4f16b65caed3a36d405481690a3a92432b5efd62
2021-12-27 14:32:39 -05:00
mike bayer 9eb5e153c9 Merge "propose concurrency check for SessionTransaction" into main 2021-12-27 19:20:10 +00:00
mike bayer 42a49ea1d7 Merge "cursor tweaks" into main 2021-12-27 19:15:40 +00:00
mike bayer d736f8bc45 Merge "factor out UnboundLoad and rearchitect strategy_options.py" into main 2021-12-27 18:14:31 +00:00
Mike Bayer cc46ea711d propose concurrency check for SessionTransaction
the discussion at #7387 refers to a condition that seems
to happen in the wild also, such as [1] [2] [3], it's not
entirely clear why this specific spot is how this occurs,
however it's maybe that when the connection is being acquired
from the pool, under load there might be a wait on the connection
pool, leading to more time for another errant thread to be
calling .close(), just a theory.

in this patch we propose using decorators and context managers
along with declarative state declarations to block reentrant
or concurrent calls to methods that conflict with expected
state changes.

The :class:`_orm.Session` (and by extension :class:`.AsyncSession`) now has
new state-tracking functionality that will proactively trap any unexpected
state changes which occur as a particular transactional method proceeds.
This is to allow situations where the :class:`_orm.Session` is being used
in a thread-unsafe manner, where event hooks or similar may be calling
unexpected methods within operations, as well as potentially under other
concurrency situations such as asyncio or gevent to raise an informative
message when the illegal access first occurs, rather than passing silently
leading to secondary failures due to the :class:`_orm.Session` being in an
invalid state.

[1] https://stackoverflow.com/questions/25768428/sqlalchemy-connection-errors
[2] https://groups.google.com/g/sqlalchemy/c/n5oVX3v4WOw
[3] https://github.com/cosmicpython/code/issues/23

Fixes: #7433
Change-Id: I699b935c0ec4e5a63f12cf878af6f7a92a30a3aa
2021-12-27 13:13:02 -05:00
Mike Bayer 91501e06a1 factor out UnboundLoad and rearchitect strategy_options.py
The architecture of Load is mostly rewritten here.

The change includes removal of the "pluggable" aspect
of the loader options, which would patch new methods onto
Load.  This has been replaced by normal methods that
respond normally to typing annotations.  As part of this
change, the bake_loaders() and unbake_loaders() options,
which have no effect since 1.4 and were unlikely to be
in any common use, have been removed.

Additionally, to support annotations for methods that
make use of @decorator, @generative etc., modified
format_argspec_plus to no longer return "args", instead
returns "grouped_args" which is always grouped and
allows return annotations to format correctly.

Fixes: #6986
Change-Id: I6117c642345cdde65a64389bba6057ddd5374427
2021-12-27 12:30:38 -05:00
Mike Bayer e5664b0a30 cursor tweaks
tighten up creation of dictionaries and conditional logic
within the creation of CursorResultMetaData objects

Change-Id: I5538ecc343ab0cabcf58d7c92ae0a552d5ac1d8a
2021-12-27 12:01:16 -05:00
mike bayer fd99a4aa80 Merge "consider truediv as truediv; support floordiv operator" into main 2021-12-27 16:29:23 +00:00
mike bayer 4a12848a1c Merge "implement cython for cache_anon_map, prefix_anon_map" into main 2021-12-27 15:26:31 +00:00
Mike Bayer 6d589ffbb5 consider truediv as truediv; support floordiv operator
Implemented full support for "truediv" and "floordiv" using the
"/" and "//" operators.  A "truediv" operation between two expressions
using :class:`_types.Integer` now considers the result to be
:class:`_types.Numeric`, and the dialect-level compilation will cast
the right operand to a numeric type on a dialect-specific basis to ensure
truediv is achieved.  For floordiv, conversion is also added for those
databases that don't already do floordiv by default (MySQL, Oracle) and
the ``FLOOR()`` function is rendered in this case, as well as for
cases where the right operand is not an integer (needed for PostgreSQL,
others).

The change resolves issues both with inconsistent behavior of the
division operator on different backends and also fixes an issue where
integer division on Oracle would fail to be able to fetch a result due
to inappropriate outputtypehandlers.

Fixes: #4926
Change-Id: Id54cc018c1fb7a49dd3ce1216d68d40f43fe2659
2021-12-26 19:32:53 -05:00
mike bayer 2bb6cfc7c9 Merge "include empty intermediary tables in optimized get" into main 2021-12-27 00:24:24 +00:00
Mike Bayer c1d2fbac4c include empty intermediary tables in optimized get
Fixed issue in joined-inheritance load of additional attributes
functionality in deep multi-level inheritance where an intermediary table
that contained no columns would not be included in the tables joined,
instead linking those tables to their primary key identifiers. While this
works fine, it nonetheless in 1.4 began producing the cartesian product
compiler warning. The logic has been changed so that these intermediary
tables are included regardless. While this does include additional tables
in the query that are not technically necessary, this only occurs for the
highly unusual case of deep 3+ level inheritance with intermediary tables
that have no non primary key columns, potential performance impact is
therefore expected to be negligible.

Fixes: #7507
Change-Id: Id2073773e97a0853b744b51feeb2bc4437032e51
2021-12-26 15:01:02 -05:00
Mike Bayer 818d62be00 restore graceful degrade of subqueryload w from_statement
Fixed regression from 1.3 where the "subqueryload" loader strategy would
fail with a stack trace if used against a query that made use of
:meth:`_orm.Query.from_statement` or :meth:`_sql.Select.from_statement`. As
subqueryload requires modifying the original statement, it's not compatible
with the "from_statement" use case, especially for statements made against
the :func:`_sql.text` construct. The behavior now is equivalent to that of
1.3 and previously, which is that the loader strategy silently degrades to
not be used for such statements, typically falling back to using the
lazyload strategy.

Fixes: #7505
Change-Id: I950800dc86a77f8320a5e696edce1ff2c84b1eb9
2021-12-26 14:45:45 -05:00
mike bayer 60b637a5c1 Merge "add recursion check for with_loader_criteria() option" into main 2021-12-23 00:38:36 +00:00
Kai Mueller 1c73759353 Fix missing class attributes when using __class_getitem__
Fixed issue where the ``__class_getitem__()`` method of the generated
declarative base class by :func:`_orm.as_declarative` would lead to
inaccessible class attributes such as ``__table__``, for cases where a
``Generic[T]`` style typing declaration were used in the class hierarchy.
This is in continuation from the basic addition of ``__class_getitem__()``
in 🎫`7368`. Pull request courtesy Kai Mueller.

Fixes: #7462
Closes: #7470
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7470
Pull-request-sha: d5e5765e0e

Change-Id: I6418af6d34532ff181343884bd419d9c2684e617
2021-12-22 18:24:15 -05:00
Mike Bayer aded8b11d9 use fully qualified, locatable names for all use of api.named_type()
Fixed mypy regression where the release of mypy 0.930 added additional
internal checks to the format of "named types", requiring that they be
fully qualified and locatable. This broke the mypy plugin for SQLAlchemy,
raising an assertion error, as there was use of symbols such as
``__builtins__`` and other un-locatable or unqualified names that
previously had not raised any assertions.

Fixes: #7496
Change-Id: I037680606a1d51158ef6503508ec76c5d5adc946
2021-12-22 16:22:49 -05:00
Mike Bayer c66c6d1aef add recursion check for with_loader_criteria() option
Fixed recursion overflow which could occur within ORM statement compilation
when using either the :func:`_orm.with_loader_criteria` feature or the the
:meth:`_orm.PropComparator.and_` method within a loader strategy in
conjunction with a subquery which referred to the same entity being altered
by the criteria option, or loaded by the loader strategy.  A check for
coming across the same loader criteria option in a recursive fashion has
been added to accommodate for this scenario.

Fixes: #7491
Change-Id: I8701332717c45a21948ea4788a3058c0fbbf03a7
2021-12-22 15:46:48 -05:00
Mike Bayer 267e9cbf6e accommodate for "clone" of ColumnClause
for use with the ClauseElement.params() method,
altered ColumnClause._clone() so that while the element
stays immutable, if the column is associated with a subquery,
it returns a new version of itself as corresponding to a
clone of the subquery.  this allows processing functions
to access the parameters in the subquery and produce a
copy of it.  The use case here is the expanded use of
.params() within loader strategies that use
HasCacheKey._apply_params_to_element().

Fixed issue in new "loader criteria" method
:meth:`_orm.PropComparator.and_` where usage with a loader strategy like
:func:`_orm.selectinload` against a column that was a member of the ``.c.``
collection of a subquery object, where the subquery would be dynamically
added to the FROM clause of the statement, would be subject to stale
parameter values within the subquery in the SQL statement cache, as the
process used by the loader strategy to replace the parameters at execution
time would fail to accommodate the subquery when received in this form.

Fixes: #7489
Change-Id: Ibb3b6af140b8a62a2c8d05b2ac92e86ca3013c46
2021-12-21 20:11:00 -05:00
Mike Bayer e6cd36fc51 implement cython for cache_anon_map, prefix_anon_map
These are small bits where cache_anon_map in particular
is part of the cache key generation scheme which is a key
target for cython.

changing such a tiny element of the cache key gen is
doing basically nothing yet, as the cython impl is
mostly the exact same speed as the python one.  I guess for
cython to be effective we'd need to redo the whole cache key
generation and possibly not use the same kinds of structures,
which might not be very easy to do.

Additionally, some cython runtime import errors are being
observed on jenkins, add an upfront check to the test suite
to indicate if the expected build succeeded when REQUIRE_SQLALCHEMY_CEXT
is set.

Running case CacheAnonMap
Running python .... Done
Running cython .... Done
                    | python      | cython      | cy / py     |
test_get_anon_non_present| 0.301266758 | 0.231203834 | 0.767438915 |
test_get_anon_present| 0.300919362 | 0.227336695 | 0.755473803 |
test_has_key_non_present| 0.152725077 | 0.133191719 | 0.872101171 |
test_has_key_present| 0.152689778 | 0.133673095 | 0.875455428 |
Running case PrefixAnonMap
Running python .. Done
Running cython .. Done
                    | python      | cython      | cy / py     |
test_apply_non_present| 0.358715744 | 0.335245703 | 0.934572034 |
test_apply_present  | 0.354434996 | 0.338579782 | 0.955266229 |

Change-Id: I0d3f1dd285c044afc234479141d831b2ee0455be
2021-12-21 12:20:04 -05:00
mike bayer 1221407282 Merge "use load_scalar_attributes() for undefer" into main 2021-12-20 14:09:27 +00:00
Mike Bayer 736f93473d use load_scalar_attributes() for undefer
At some point, undeferral of attributes started emitting
a full ORM query for the entity, including that subclass columns
would use a JOIN.   Seems to be at least in 1.3 / 1.4 and possibly
earlier.    This JOINs to the superclass table unnecessarily.
Use load_scalar_attributes() here which should handle the whole
thing and emits a more efficient query for joined inheritance.

As this behavior seems to have been throughout 1.3 and 1.4
at least, targeting at 2.0 is likely best.

Fixes: #7463
Change-Id: Ie4bae767747bba0d03fb13eaff579d4bab0b1bc2
2021-12-19 13:49:38 -05:00
Federico Caselli 76fa211620 Replace c extension with cython versions.
Re-implement c version immutabledict / processors / resultproxy / utils with cython.
Performance is in general in par or better than the c version
Added a collection module that has cython version of OrderedSet and IdentitySet

Added a new test/perf file to compare the implementations.
Run ``python test/perf/compiled_extensions.py all`` to execute the comparison test.

See results here: https://docs.google.com/document/d/1nOcDGojHRtXEkuy4vNXcW_XOJd9gqKhSeALGG3kYr6A/edit?usp=sharing

Fixes: #7256
Change-Id: I2930ef1894b5048210384728118e586e813f6a76
Signed-off-by: Federico Caselli <cfederico87@gmail.com>
2021-12-17 21:29:05 +01:00
Mike Bayer 3a33fa8b0a include InterfaceError for mariadb disconnect check
Corrected the error classes inspected for the "is_disconnect" check for the
``mariadbconnector`` dialect, which was failing for disconnects that
occurred due to common MySQL/MariaDB error codes such as 2006; the DBAPI
appears to currently use the ``mariadb.InterfaceError`` exception class for
disconnect errors such as error code 2006, which has been added to the list
of classes checked.

For the current "real reconnect test", shutting down the mariadb
connection from the client side produces
ProgrammingError("Connection isn't valid anymore") which we also
continue to intercept.

Fixes: #7457
Change-Id: I0b37cd7a73359a23ad756ff2af0a9333c841221b
2021-12-16 09:17:04 -05:00
mike bayer f34542b37b Merge "implement correct errors for Row immutability" into main 2021-12-12 23:29:11 +00:00
Mike Bayer 04421c8bed use the options from the cached statement for propagate_options
Fixed caching-related issue where the use of a loader option of the form
``lazyload(aliased(A).bs).joinedload(B.cs)`` would fail to result in the
joinedload being invoked for runs subsequent to the query being cached, due
to a mismatch for the options / object path applied to the objects loaded
for a query with a lead entity that used ``aliased()``.

Fixes: #7447
Change-Id: I4e9c34654b7d3668cd8878decbd688afe2af5f81
2021-12-12 14:23:03 -05:00
Federico Caselli 6528a5fbd1 Merge "Add execution options to `Session.get`" into main 2021-12-11 08:09:20 +00:00
Federico Caselli 33fd0e29a7 Merge "Add async_engine_from_config()" into main 2021-12-11 08:08:04 +00:00
mike bayer 2d777e1607 Merge "Removals: strings for join(), loader_options()." into main 2021-12-10 16:21:09 +00:00
Nils Philippsen 98b1650efc Add async_engine_from_config()
Added :func:`_asyncio.async_engine_config` function to create
an async engine from a configuration dict.  This otherwise
behaves the same as :func:`_sa.engine_from_config`.

Fixes: #7301
Closes: #7302
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7302
Pull-request-sha: c7c758833b

Change-Id: I64feadf95b5015c24fe0fa0dbae6755b72d1713e
2021-12-10 17:20:31 +01:00
Federico Caselli a7c004dff8 Add execution options to `Session.get`
Fixes: #7410
Change-Id: Iab6427b8b4c2ada8c31ef69f92d27c1185dbb6b1
2021-12-10 15:33:50 +01:00
Leo Singer 786bc09974 Update references to tables in PostgreSQL documentation
Since the table numbers change from one version of PostgreSQL to
the next, refer to the tables by name rather than by number.
2021-12-10 14:27:19 +01:00