docstr: Construct a ShardedSession.
  
  :param shard_chooser: A callable which, passed a Mapper, a mapped
    instance, and possibly a SQL clause, returns a shard ID.  This id
    may be based off of the attributes present within the object, or on
    some round-robin scheme. If the scheme is based on a selection, it
    should set whatever state on the instance to mark it in the future as
    participating in that shard.
  
  :param identity_chooser: A callable, passed a Mapper and primary key
   argument, which should return a list of shard ids where this
   primary key might reside.
  
    .. versionchanged:: 2.0  The ``identity_chooser`` parameter
       supersedes the ``id_chooser`` parameter.
  
  :param execute_chooser: For a given :class:`.ORMExecuteState`,
    returns the list of shard_ids
    where the query should be issued.  Results from all shards returned
    will be combined together into a single listing.
  
    .. versionchanged:: 1.4  The ``execute_chooser`` parameter
       supersedes the ``query_chooser`` parameter.
  
  :param shards: A dictionary of string shard names
    to :class:`~sqlalchemy.engine.Engine` objects.
docstr_tok: ["Construct","a","ShardedSession",".",":","param","shard_chooser",":","A","callable","which",",","passed","a","Mapper",",","a","mapped","instance",",","and","possibly","a","SQL","clause",",","returns","a","shard","ID",".","This","id","may","be","based","off","of","the","attributes","present","within","the","object",",","or","on","some","round-robin","scheme",".","If","the","scheme","is","based","on","a","selection",",","it","should","set","whatever","state","on","the","instance","to","mark","it","in","the","future","as","participating","in","that","shard",".",":","param","identity_chooser",":","A","callable",",","passed","a","Mapper","and","primary","key","argument",",","which","should","return","a","list","of","shard","ids","where","this","primary","key","might","reside",".","..","versionchanged",":",":","2.0","The","``","identity_chooser","``","parameter","supersedes","the","``","id_chooser","``","parameter",".",":","param","execute_chooser",":","For","a","given",":","class",":","`",".ORMExecuteState","`",",","returns","the","list","of","shard_ids","where","the","query","should","be","issued",".","Results","from","all","shards","returned","will","be","combined","together","into","a","single","listing",".","..","versionchanged",":",":","1.4","The","``","execute_chooser","``","parameter","supersedes","the","``","query_chooser","``","parameter",".",":","param","shards",":","A","dictionary","of","string","shard","names","to",":","class",":","`","~sqlalchemy.engine.Engine","`","objects","."]
code: def __init__(
      self,
      shard_chooser: ShardChooser,
      identity_chooser: Optional[IdentityChooser] = None,
      execute_chooser: Optional[
          Callable[[ORMExecuteState], Iterable[Any]]
      ] = None,
      shards: Optional[Dict[str, Any]] = None,
      query_cls: Type[Query[_T]] = ShardedQuery,
      *,
      id_chooser: Optional[
          Callable[[Query[_T], Iterable[_T]], Iterable[Any]]
      ] = None,
      query_chooser: Optional[
          Callable[[Executable], Iterable[Any]]
      ] = None,
      **kwargs: Any,
  ) -> None:
      """Construct a ShardedSession.
  
      :param shard_chooser: A callable which, passed a Mapper, a mapped
        instance, and possibly a SQL clause, returns a shard ID.  This id
        may be based off of the attributes present within the object, or on
        some round-robin scheme. If the scheme is based on a selection, it
        should set whatever state on the instance to mark it in the future as
        participating in that shard.
  
      :param identity_chooser: A callable, passed a Mapper and primary key
       argument, which should return a list of shard ids where this
       primary key might reside.
  
        .. versionchanged:: 2.0  The ``identity_chooser`` parameter
           supersedes the ``id_chooser`` parameter.
  
      :param execute_chooser: For a given :class:`.ORMExecuteState`,
        returns the list of shard_ids
        where the query should be issued.  Results from all shards returned
        will be combined together into a single listing.
  
        .. versionchanged:: 1.4  The ``execute_chooser`` parameter
           supersedes the ``query_chooser`` parameter.
  
      :param shards: A dictionary of string shard names
        to :class:`~sqlalchemy.engine.Engine` objects.
  
      """
      super().__init__(query_cls=query_cls, **kwargs)
  
      event.listen(
          self, "do_orm_execute", execute_and_instances, retval=True
      )
      self.shard_chooser = shard_chooser
  
      if id_chooser:
          _id_chooser = id_chooser
          util.warn_deprecated(
              "The ``id_chooser`` parameter is deprecated; "
              "please use ``identity_chooser``.",
              "2.0",
          )
  
          def _legacy_identity_chooser(
              mapper: Mapper[_T],
              primary_key: _PKIdentityArgument,
              *,
              lazy_loaded_from: Optional[InstanceState[Any]],
              execution_options: OrmExecuteOptionsParameter,
              bind_arguments: _BindArguments,
              **kw: Any,
          ) -> Any:
              q = self.query(mapper)
              if lazy_loaded_from:
                  q = q._set_lazyload_from(lazy_loaded_from)
              return _id_chooser(q, primary_key)
  
          self.identity_chooser = _legacy_identity_chooser
      elif identity_chooser:
          self.identity_chooser = identity_chooser
      else:
          raise exc.ArgumentError(
              "identity_chooser or id_chooser is required"
          )
  
      if query_chooser:
          _query_chooser = query_chooser
          util.warn_deprecated(
              "The ``query_chooser`` parameter is deprecated; "
              "please use ``execute_chooser``.",
              "1.4",
          )
          if execute_chooser:
              raise exc.ArgumentError(
                  "Can't pass query_chooser and execute_chooser "
                  "at the same time."
              )
  
          def _default_execute_chooser(
              orm_context: ORMExecuteState,
          ) -> Iterable[Any]:
              return _query_chooser(orm_context.statement)
  
          if execute_chooser is None:
              execute_chooser = _default_execute_chooser
  
      if execute_chooser is None:
          raise exc.ArgumentError(
              "execute_chooser or query_chooser is required"
          )
      self.execute_chooser = execute_chooser
      self.__shards: Dict[ShardIdentifier, _SessionBind] = {}
      if shards is not None:
          for k in shards:
              self.bind_shard(k, shards[k])
  
imports: from __future__ import annotations
  from typing import Any
  from typing import Callable
  from typing import Dict
  from typing import Iterable
  from typing import Optional
  from typing import Protocol
  from typing import Tuple
  from typing import Type
  from typing import TYPE_CHECKING
  from typing import TypeVar
  from typing import Union
  from ..None import event
  from ..None import exc
  from ..None import inspect
  from ..None import util
  from ..orm import PassiveFlag
  from ..orm._typing import OrmExecuteOptionsParameter
  from ..orm.interfaces import ORMOption
  from ..orm.mapper import Mapper
  from ..orm.query import Query
  from ..orm.session import _BindArguments
  from ..orm.session import _PKIdentityArgument
  from ..orm.session import Session
  from ..util.typing import Self
label_desc: Use image node_id 1 to create a new ShardedSession object from inherited base classes: Session with example: obj = ShardedSession(shard_chooser, identity_chooser, execute_chooser, shards, query_cls)