summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorStephen Rosen <sirosen@globus.org>2023-03-14 14:18:14 -0500
committerGitHub <noreply@github.com>2023-03-14 20:18:14 +0100
commit79b6a41d257b0fda6e8dbb42547c050b0fd1e5de (patch)
tree14793b993691376a366d9b05eec9e359848e0b9e /lib/sqlalchemy
parent9f1b6ae053f5513bbf0ce221ed9ab4fb9d8e0ca7 (diff)
downloadsqlalchemy-79b6a41d257b0fda6e8dbb42547c050b0fd1e5de.tar.gz
Document generic type parameters to FunctionElement and GenericFunction (#9079)
* Document type parameters to FunctionElement Add a note to FunctionElement which indicates that the type is a typing.Generic class and points at GenericFunction examples for a specific example usage. A minimal reference is made to type checkers and IDEs as use-cases in order to try to contextualize this as an optional feature which supports particular use cases. Append to the GenericFunction examples a case which uses `DateTime` but also includes the generic type parameter (`datetime.datetime`). * Fix type annotated function usage example
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/sql/functions.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py
index 5f2e67288..0952ca0f1 100644
--- a/lib/sqlalchemy/sql/functions.py
+++ b/lib/sqlalchemy/sql/functions.py
@@ -100,6 +100,11 @@ def register_function(identifier, fn, package="_default"):
class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative):
"""Base for SQL function-oriented constructs.
+ This is a `generic type <https://peps.python.org/pep-0484/#generics>`_,
+ meaning that type checkers and IDEs can be instructed on the types to
+ expect in a :class:`_engine.Result` for this function. See
+ :class:`.GenericFunction` for an example of how this is done.
+
.. seealso::
:ref:`tutorial_functions` - in the :ref:`unified_tutorial`
@@ -1249,6 +1254,19 @@ class GenericFunction(Function[_T]):
>>> print(func.geo.buffer())
{printsql}"ST_Buffer"()
+ Type parameters for this class as a
+ `generic type <https://peps.python.org/pep-0484/#generics>`_ can be passed
+ and should match the type seen in a :class:`_engine.Result`. For example::
+
+ class as_utc(GenericFunction[datetime.datetime]):
+ type = DateTime()
+ inherit_cache = True
+
+ The above indicates that the following expression returns a ``datetime``
+ object::
+
+ connection.scalar(select(func.as_utc()))
+
.. versionadded:: 1.3.13 The :class:`.quoted_name` construct is now
recognized for quoting when used with the "name" attribute of the
object, so that quoting can be forced on or off for the function