+
    }¯³jò-  ã                  óð   € R t ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIH	t	 ^ RIH
t
 ^R	IHt ^R
IHt ^RIHt ]'       d   ^RIHt ^RIHt R.t]	! R4      t ! R R]],          4      tR# )a7  Define attributes on ORM-mapped classes that have "index" attributes for
columns with :class:`_types.Indexable` types.

"index" means the attribute is associated with an element of an
:class:`_types.Indexable` column with the predefined index to access it.
The :class:`_types.Indexable` types include types such as
:class:`_types.ARRAY`, :class:`_types.JSON` and
:class:`_postgresql.HSTORE`.



The :mod:`~sqlalchemy.ext.indexable` extension provides
:class:`_schema.Column`-like interface for any element of an
:class:`_types.Indexable` typed column. In simple cases, it can be
treated as a :class:`_schema.Column` - mapped attribute.

Synopsis
========

Given ``Person`` as a model with a primary key and JSON data field.
While this field may have any number of elements encoded within it,
we would like to refer to the element called ``name`` individually
as a dedicated attribute which behaves like a standalone column::

    from sqlalchemy import Column, JSON, Integer
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.ext.indexable import index_property

    Base = declarative_base()


    class Person(Base):
        __tablename__ = "person"

        id = Column(Integer, primary_key=True)
        data = Column(JSON)

        name = index_property("data", "name")

Above, the ``name`` attribute now behaves like a mapped column.   We
can compose a new ``Person`` and set the value of ``name``::

    >>> person = Person(name="Alchemist")

The value is now accessible::

    >>> person.name
    'Alchemist'

Behind the scenes, the JSON field was initialized to a new blank dictionary
and the field was set::

    >>> person.data
    {'name': 'Alchemist'}

The field is mutable in place::

    >>> person.name = "Renamed"
    >>> person.name
    'Renamed'
    >>> person.data
    {'name': 'Renamed'}

When using :class:`.index_property`, the change that we make to the indexable
structure is also automatically tracked as history; we no longer need
to use :class:`~.mutable.MutableDict` in order to track this change
for the unit of work.

Deletions work normally as well::

    >>> del person.name
    >>> person.data
    {}

Above, deletion of ``person.name`` deletes the value from the dictionary,
but not the dictionary itself.

A missing key will produce ``AttributeError``::

    >>> person = Person()
    >>> person.name
    AttributeError: 'name'

Unless you set a default value::

    >>> class Person(Base):
    ...     __tablename__ = "person"
    ...
    ...     id = Column(Integer, primary_key=True)
    ...     data = Column(JSON)
    ...
    ...     name = index_property("data", "name", default=None)  # See default

    >>> person = Person()
    >>> print(person.name)
    None


The attributes are also accessible at the class level.
Below, we illustrate ``Person.name`` used to generate
an indexed SQL criteria::

    >>> from sqlalchemy.orm import Session
    >>> session = Session()
    >>> query = session.query(Person).filter(Person.name == "Alchemist")

The above query is equivalent to::

    >>> query = session.query(Person).filter(Person.data["name"] == "Alchemist")

Multiple :class:`.index_property` objects can be chained to produce
multiple levels of indexing::

    from sqlalchemy import Column, JSON, Integer
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.ext.indexable import index_property

    Base = declarative_base()


    class Person(Base):
        __tablename__ = "person"

        id = Column(Integer, primary_key=True)
        data = Column(JSON)

        birthday = index_property("data", "birthday")
        year = index_property("birthday", "year")
        month = index_property("birthday", "month")
        day = index_property("birthday", "day")

Above, a query such as::

    q = session.query(Person).filter(Person.year == "1980")

On a PostgreSQL backend, the above query will render as:

.. sourcecode:: sql

    SELECT person.id, person.data
    FROM person
    WHERE person.data -> %(data_1)s -> %(param_1)s = %(param_2)s

Default Values
==============

:class:`.index_property` includes special behaviors for when the indexed
data structure does not exist, and a set operation is called:

* For an :class:`.index_property` that is given an integer index value,
  the default data structure will be a Python list of ``None`` values,
  at least as long as the index value; the value is then set at its
  place in the list.  This means for an index value of zero, the list
  will be initialized to ``[None]`` before setting the given value,
  and for an index value of five, the list will be initialized to
  ``[None, None, None, None, None]`` before setting the fifth element
  to the given value.   Note that an existing list is **not** extended
  in place to receive a value.

* for an :class:`.index_property` that is given any other kind of index
  value (e.g. strings usually), a Python dictionary is used as the
  default data structure.

* The default data structure can be set to any Python callable using the
  :paramref:`.index_property.datatype` parameter, overriding the previous
  rules.


Subclassing
===========

:class:`.index_property` can be subclassed, in particular for the common
use case of providing coercion of values or SQL expressions as they are
accessed.  Below is a common recipe for use with a PostgreSQL JSON type,
where we want to also include automatic casting plus ``astext()``::

    class pg_json_property(index_property):
        def __init__(self, attr_name, index, cast_type):
            super(pg_json_property, self).__init__(attr_name, index)
            self.cast_type = cast_type

        def expr(self, model):
            expr = super(pg_json_property, self).expr(model)
            return expr.astext.cast(self.cast_type)

The above subclass can be used with the PostgreSQL-specific
version of :class:`_postgresql.JSON`::

    from sqlalchemy import Column, Integer
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.dialects.postgresql import JSON

    Base = declarative_base()


    class Person(Base):
        __tablename__ = "person"

        id = Column(Integer, primary_key=True)
        data = Column(JSON)

        age = pg_json_property("data", "age", Integer)

The ``age`` attribute at the instance level works as before; however
when rendering SQL, PostgreSQL's ``->>`` operator will be used
for indexed access, instead of the usual index operator of ``->``::

    >>> query = session.query(Person).filter(Person.age < 20)

The above query will render:

.. sourcecode:: sql

    SELECT person.id, person.data
    FROM person
    WHERE CAST(person.data ->> %(data_1)s AS INTEGER) < %(param_1)s

)Úannotations)ÚAny)ÚCallable)Úcast)ÚOptional)ÚTYPE_CHECKING)ÚTypeVar)ÚUnion)Úinspect)Úhybrid_property)Úflag_modified)ÚSQLColumnExpression)Ú_HasClauseElementÚindex_propertyÚ_Tc                  óš   a € ] tR t^ûtRt]! ]]! 4       4      t]RRR3R V 3R lllt	RR R llt
R R	 ltR
 R ltR R ltR R ltRtV ;t# )r   z¸A property generator. The generated property describes an object
attribute that corresponds to an :class:`_types.Indexable`
column.

.. seealso::

    :mod:`sqlalchemy.ext.indexable`

NTc               ó0   € V ^8„  d   QhRRRRRRRRR	R
RR
/# )é   Ú	attr_nameÚstrÚindexzUnion[int, str]Údefaultr   ÚdatatypezOptional[Callable[[], Any]]ÚmutableÚboolÚonebased© )Úformats   "ÚY/home/luca/screen-checking/.venv/lib/python3.14/site-packages/sqlalchemy/ext/indexable.pyÚ__annotate__Úindex_property.__annotate__  sF   € ÷ 1!ñ 1!àð1!ð ð1!ð ð	1!ð
 .ð1!ð ð1!ð ñ1!ó    c                óš  <a€ V'       d<   \         SV `  V P                  V P                  V P                  V P
                  4       M&\         SV `  V P                  RRV P
                  4       Wn        SV n        W0n        \        S\        4      pT;'       d    TpVe   W@n        MV'       d   V3R lV n        M\        V n        W`n        R# )aÝ  Create a new :class:`.index_property`.

:param attr_name:
    An attribute name of an `Indexable` typed column, or other
    attribute that returns an indexable structure.
:param index:
    The index to be used for getting and setting this value.  This
    should be the Python-side index value for integers.
:param default:
    A value which will be returned instead of `AttributeError`
    when there is not a value at given index.
:param datatype: default datatype to use when the field is empty.
    By default, this is derived from the type of index used; a
    Python list for an integer index, or a Python dictionary for
    any other style of index.   For a list, the list will be
    initialized to a list of None values that is at least
    ``index`` elements long.
:param mutable: if False, writes and deletes to the attribute will
    be disallowed.
:param onebased: assume the SQL representation of this value is
    one-based; that is, the first index in SQL is 1, not zero.
Nc                 óR   <€ \        S^,           4       U u. uF  p RNK  	  up # u up i )é   N)Úrange)Úxr   s    €r   Ú<lambda>Ú)index_property.__init__.<locals>.<lambda>6  s"   ø€ ´u¸UÀQ½YÔ7GÓ(HÑ7G°!«Ñ7GÒ(HùÒ(Hs   –$)ÚsuperÚ__init__ÚfgetÚfsetÚfdelÚexprr   r   r   Ú
isinstanceÚintr   Údictr   )	Úselfr   r   r   r   r   r   Ú
is_numericÚ	__class__s	   &&f&&&& €r   r*   Úindex_property.__init__  s’   ù€ ÷@ Ü‰GÑ˜TŸY™Y¨¯	©	°4·9±9¸d¿i¹iÕHä‰GÑ˜TŸY™Y¨¨d°D·I±IÔ>Ø"ŒØˆŒ
ØŒÜ ¤sÓ+ˆ
Ø×*Ð* (ˆàÒØ$�MçÜ H�•ä $�”Ø Žr!   c               ó    € V ^8„  d   QhRRRR/# )r   ÚerrzOptional[BaseException]Úreturnr   r   )r   s   "r   r   r    ;  s   € ÷  ñ  Ð!8ð  ÀBñ  r!   c                	ó|   € V P                   V P                  8X  d   \        V P                  4      VhV P                   # ©N)r   Ú_NO_DEFAULT_ARGUMENTÚAttributeErrorr   )r2   r7   s   &&r   Ú_fget_defaultÚindex_property._fget_default;  s/   € Ø�<‰<˜4×4Ñ4Ô4Ü  §¡Ó0°cÐ9à—<‘<Ðr!   c               ó    € V ^8„  d   QhRRRR/# )r   Ú_index_property__instancer   r8   r   r   )r   s   "r   r   r    A  s   € ÷ 
ñ 
˜sð 
 rñ 
r!   c                	óà   € V P                   p\        W4      pVf   V P                  4       #  W0P                  ,          pV#   \        \
        3 d   pT P                  T4      u R p?# R p?ii ; ir:   )r   Úgetattrr=   r   ÚKeyErrorÚ
IndexError)r2   r@   r   Úcolumn_valueÚvaluer7   s   &&    r   r+   Úindex_property.fgetA  sj   € Ø—N‘Nˆ	Ü˜zÓ5ˆØÒØ×%Ñ%Ó'Ð'ð	Ø §¡Õ,ˆEð ˆLøô œ*Ð%ô 	+Ø×%Ñ% cÓ*Õ*ûð	+ús   ­A ÁA-ÁA(Á"A-Á(A-c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úinstancer   rF   r   r8   ÚNoner   )r   s   "r   r   r    M  s!   € ÷ 	/ñ 	/˜Sð 	/¨ð 	/°ñ 	/r!   c                	ó  € V P                   p\        WR 4      pVf   V P                  4       p\        WV4       W$V P                  &   \        WV4       V\        V4      P                  P                  9   d   \        W4       R # R # r:   )	r   rB   r   Úsetattrr   r
   ÚmapperÚattrsr   )r2   rI   rF   r   rE   s   &&&  r   r,   Úindex_property.fsetM  sn   € Ø—N‘Nˆ	Ü˜x°DÓ9ˆØÒØŸ=™=›?ˆLÜ�H¨Ô6Ø#(�T—Z‘ZÑ Ü� \Ô2Øœ Ó)×0Ñ0×6Ñ6Ô6Ü˜(Ö.ñ 7r!   c               ó    € V ^8„  d   QhRRRR/# )r   rI   r   r8   rJ   r   )r   s   "r   r   r    X  s   € ÷ /ñ /˜Sð / Tñ /r!   c                	ó   € V P                   p\        W4      pVf   \        V P                   4      h W0P                   \	        WV4       \        W4       R #   \         d   p\        T P                   4      ThR p?ii ; ir:   )r   rB   r<   r   rL   r   rC   )r2   rI   r   rE   r7   s   &&   r   r-   Úindex_property.fdelX  sp   € Ø—N‘Nˆ	Ü˜xÓ3ˆØÒÜ  §¡Ó0Ð0ð	/ØŸZ™ZÐ(ô �H¨Ô6Ü˜(Ö.øô	 ô 	:Ü  §¡Ó0°cÐ9ûð	:ús   ²A ÁA=Á"A8Á8A=c               ó    € V ^8„  d   QhRRRR/# )r   Úmodelr   r8   z5Union[_HasClauseElement[_T], SQLColumnExpression[_T]]r   )r   s   "r   r   r    e  s   € ÷ ñ Øðà	>ñr!   c                	óŠ   € \        WP                  4      pV P                  pV P                  '       d
   V^,          pW#,          # )r$   )rB   r   r   r   )r2   rT   Úcolumnr   s   &&  r   r.   Úindex_property.expre  s5   € ô ˜§¡Ó/ˆØ—
‘
ˆØ�=�=ˆ=Ø�Q�JˆEØ�}Ðr!   )r   r   r   r   r   r:   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   Úobjectr;   r*   r=   r+   r,   r-   r.   Ú__static_attributes__Ú__classcell__)r4   s   @r   r   r   û   sP   ø† ññ   ¡F£HÓ-Ðð +Ø04ØØ÷1!ò 1!÷f õ
õ	/õ/÷ó r!   N)r\   Ú
__future__r   Útypingr   r   r   r   r   r   r	   Ú r
   Ú
ext.hybridr   Úorm.attributesr   Úsqlr   Úsql._typingr   Ú__all__r   r   r   r!   r   Ú<module>rh      s^   ðñYõv #å Ý Ý Ý Ý  Ý Ý å Ý (Ý *çÝ)Ý/ð Ð
€áˆTƒ]€ôq�_ RÕ(ö qr!   