module documentation

Visitor/traversal interface and library functions.

Class anon_map A map that creates new keys for missing key access.
Class CloningExternalTraversal Base class for visitor objects which can traverse using the :func:`.visitors.cloned_traverse` function.
Class ExternallyTraversible No class docstring; 0/1 class variable, 2/4 methods documented
Class ExternalTraversal Base class for visitor objects which can traverse externally using the :func:`.visitors.traverse` function.
Class HasTraversalDispatch Define infrastructure for classes that perform internal traversals
Class HasTraverseInternals base for classes that have a "traverse internals" element, which defines all kinds of ways of traversing the elements of an object.
Class InternalTraversal Defines visitor symbols used for internal traversal.
Class ReplacingExternalTraversal Base class for visitor objects which can traverse using the :func:`.visitors.replacement_traverse` function.
Class Visitable Base class for visitable objects.
Function cloned_traverse Clone the given expression structure, allowing modifications by visitors for mutable objects.
Function iterate Traverse the given expression structure, returning an iterator.
Function replacement_traverse Clone the given expression structure, allowing element replacement by a given replacement function.
Function traverse Traverse and visit the given expression structure using the default iterator.
Function traverse_using Visit the given expression structure using the given iterator of objects.
Class _CloneCallableType Undocumented
Class _CompilerDispatchType Undocumented
Class _InternalTraversalDispatchType Undocumented
Class _TraverseTransformCallableType Undocumented
Function _generate_traversal_dispatch Undocumented
Type Variable _CE Undocumented
Type Variable _ET Undocumented
Type Variable _ExtT Undocumented
Type Alias _TraverseCallableType Undocumented
Type Alias _TraverseInternalsType a structure that defines how a HasTraverseInternals should be traversed.
@overload
def cloned_traverse(obj: Literal[None], opts: Mapping[str, Any], visitors: Mapping[str, _TraverseCallableType[Any]]):
@overload
def cloned_traverse(obj: _ET, opts: Mapping[str, Any], visitors: Mapping[str, _TraverseCallableType[Any]]) -> _ET:
(source)

Clone the given expression structure, allowing modifications by visitors for mutable objects. Traversal usage is the same as that of :func:`.visitors.traverse`. The visitor functions present in the ``visitors`` dictionary may also modify the internals of the given structure as the traversal proceeds. The :func:`.cloned_traverse` function does **not** provide objects that are part of the :class:`.Immutable` interface to the visit methods (this primarily includes :class:`.ColumnClause`, :class:`.Column`, :class:`.TableClause` and :class:`.Table` objects). As this traversal is only intended to allow in-place mutation of objects, :class:`.Immutable` objects are skipped. The :meth:`.Immutable._clone` method is still called on each object to allow for objects to replace themselves with a different object based on a clone of their sub-internals (e.g. a :class:`.ColumnClause` that clones its subquery to return a new :class:`.ColumnClause`). .. versionchanged:: 2.0 The :func:`.cloned_traverse` function omits objects that are part of the :class:`.Immutable` interface. The central API feature used by the :func:`.visitors.cloned_traverse` and :func:`.visitors.replacement_traverse` functions, in addition to the :meth:`_expression.ClauseElement.get_children` function that is used to achieve the iteration, is the :meth:`_expression.ClauseElement._copy_internals` method. For a :class:`_expression.ClauseElement` structure to support cloning and replacement traversals correctly, it needs to be able to pass a cloning function into its internal members in order to make copies of them. .. seealso:: :func:`.visitors.traverse` :func:`.visitors.replacement_traverse`

Traverse the given expression structure, returning an iterator. Traversal is configured to be breadth-first. The central API feature used by the :func:`.visitors.iterate` function is the :meth:`_expression.ClauseElement.get_children` method of :class:`_expression.ClauseElement` objects. This method should return all the :class:`_expression.ClauseElement` objects which are associated with a particular :class:`_expression.ClauseElement` object. For example, a :class:`.Case` structure will refer to a series of :class:`_expression.ColumnElement` objects within its "whens" and "else\_" member variables. :param obj: :class:`_expression.ClauseElement` structure to be traversed :param opts: dictionary of iteration options. This dictionary is usually empty in modern usage.

@overload
def replacement_traverse(obj: Literal[None], opts: Mapping[str, Any], replace: _TraverseTransformCallableType[Any]):
@overload
def replacement_traverse(obj: _CE, opts: Mapping[str, Any], replace: _TraverseTransformCallableType[Any]) -> _CE:
@overload
def replacement_traverse(obj: ExternallyTraversible, opts: Mapping[str, Any], replace: _TraverseTransformCallableType[Any]) -> ExternallyTraversible:
(source)

Clone the given expression structure, allowing element replacement by a given replacement function. This function is very similar to the :func:`.visitors.cloned_traverse` function, except instead of being passed a dictionary of visitors, all elements are unconditionally passed into the given replace function. The replace function then has the option to return an entirely new object which will replace the one given. If it returns ``None``, then the object is kept in place. The difference in usage between :func:`.visitors.cloned_traverse` and :func:`.visitors.replacement_traverse` is that in the former case, an already-cloned object is passed to the visitor function, and the visitor function can then manipulate the internal state of the object. In the case of the latter, the visitor function should only return an entirely different object, or do nothing. The use case for :func:`.visitors.replacement_traverse` is that of replacing a FROM clause inside of a SQL structure with a different one, as is a common use case within the ORM.

@overload
def traverse(obj: Literal[None], opts: Mapping[str, Any], visitors: Mapping[str, _TraverseCallableType[Any]]):
@overload
(source)

Traverse and visit the given expression structure using the default iterator. e.g.:: from sqlalchemy.sql import visitors stmt = select(some_table).where(some_table.c.foo == 'bar') def visit_bindparam(bind_param): print("found bound value: %s" % bind_param.value) visitors.traverse(stmt, {}, {"bindparam": visit_bindparam}) The iteration of objects uses the :func:`.visitors.iterate` function, which does a breadth-first traversal using a stack. :param obj: :class:`_expression.ClauseElement` structure to be traversed :param opts: dictionary of iteration options. This dictionary is usually empty in modern usage. :param visitors: dictionary of visit functions. The dictionary should have strings as keys, each of which would correspond to the ``__visit_name__`` of a particular kind of SQL expression object, and callable functions as values, each of which represents a visitor function for that kind of object.

Visit the given expression structure using the given iterator of objects. :func:`.visitors.traverse_using` is usually called internally as the result of the :func:`.visitors.traverse` function. :param iterator: an iterable or sequence which will yield :class:`_expression.ClauseElement` structures; the iterator is assumed to be the product of the :func:`.visitors.iterate` function. :param obj: the :class:`_expression.ClauseElement` that was used as the target of the :func:`.iterate` function. :param visitors: dictionary of visit functions. See :func:`.traverse` for details on this dictionary. .. seealso:: :func:`.traverse`

def _generate_traversal_dispatch(): (source)

Undocumented

Undocumented

Value
TypeVar('_CE',
        bound='ColumnElement[Any]')

Undocumented

Value
TypeVar('_ET',
        bound=ExternallyTraversible)

Undocumented

Value
TypeVar('_ExtT',
        bound='ExternalTraversal')
_TraverseCallableType = (source)

Undocumented

Value
Callable[[_ET], None]
_TraverseInternalsType = (source)

a structure that defines how a HasTraverseInternals should be traversed. This structure consists of a list of (attributename, internaltraversal) tuples, where the "attributename" refers to the name of an attribute on an instance of the HasTraverseInternals object, and "internaltraversal" refers to an :class:`.InternalTraversal` enumeration symbol defining what kind of data this attribute stores, which indicates to the traverser how it should be handled.

Value
List[Tuple[str, InternalTraversal]]