Ë
    ðmxi*<  ã                  óŽ  — U d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
mZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZ e	r@ddlZddlZddlZddl Z!ddl"m#Z$ ddl%m&Z&m'Z'm(Z( e$e
e
e
e
e
f   Z) ed	«      Z*d
Z+de,d<   e
Z-de,d<   g d¢Z. G d„ de«      Z/ G d„ dee/e«      Z0 G d„ de/e«      Z1 G d„ deee
   e«      Z2 G d„ dee«      Z3 G d„ de0e3e«      Z4 G d„ de2e3e«      Z5 G d„ de1e«      Z6 G d„ d e4e«      Z7 G d!„ d"e5e«      Z8 G d#„ d$e/e«      Z9 G d%„ d&e4e«      Z: G d'„ d(e5e«      Z; G d)„ d*e1e«      Z<d+Z=de,d,<   d-Z>de,d.<   d/Z?de,d0<   d1Z@de,d2<   d3ZAde,d4<   d5ZBde,d6<   d7ZCde,d8<   d9ZDde,d:<   d;ZEde,d<<   d=ZFde,d><   e<ZGde,d?<   e<ZHde,d@<   dAZIde,dB<   dCZJde,dD<   dEZKde,dF<   dGZLde,dH<   e0ZMde,dI<   	 ee1e9f   ZNde,dJ<   eeMeNf   ZOde,dK<   	 e2ZPde,dL<   	  edMeO¬N«      ZQ	  edOeM¬N«      ZR	  edPeN¬N«      ZS edQeP¬N«      ZT	 dadR„ZUdbdS„ZV edTe«      ZWeZXdUe,dV<   eZYdWe,dX<    edYe«      ZZ edZe«      Z[ ed[e«      Z\dcd\„Z]ddd]„Z^ded^„Z_dfd_„Z`dgd`„Zay)huõ  The home for *mostly* [structural] counterparts to [nominal] native types.

If you find yourself being yelled at by a typechecker and ended up here - **do not fear!**

We have 5 funky flavors, which tackle two different problem spaces.

How do we describe [Native types] when ...
- ... **wrapping in** a [Narwhals type]?
- ... **matching to** an [`Implementation`]?

## Wrapping in a Narwhals type
[//]: # (TODO @dangotbanned: Replace `Thing` with a better name)

The following examples use the placeholder type `Thing` which represents one of:
- `DataFrame`: (Eager) 2D data structure representing data as a table with rows and columns.
- `LazyFrame`: (Lazy) Computation graph/query against a DataFrame/database.
- `Series`: 1D data structure representing a single column.

Our goal is to **wrap** a *partially-unknown* native object **in** a [generic class]:

    def wrapping_in_df(native: IntoDataFrameT) -> DataFrame[IntoDataFrameT]: ...
    def wrapping_in_lf(native: IntoLazyFrameT) -> LazyFrame[IntoLazyFrameT]: ...
    def wrapping_in_ser(native: IntoSeriesT) -> Series[IntoSeriesT]: ...

### (1) `Native<Thing>`
Minimal [`Protocol`]s that are [assignable to] *almost any* supported native type of that group:

    class NativeThing(Protocol):
        def something_common(self, *args: Any, **kwargs: Any) -> Any: ...

Note:
    This group is primarily a building block for more useful types.

### (2) `Into<Thing>`
*Publicly* exported [`TypeAlias`]s of **(1)**:

    IntoThing: TypeAlias = NativeThing

**But**, occasionally, there'll be an edge-case which we can spell like:

    IntoThing: TypeAlias = Union[<type that does not fit the protocol>, NativeThing]

Tip:
    Reach for these when there **isn't a need to preserve** the original native type.

### (3) `Into<Thing>T`
*Publicly* exported [`TypeVar`]s, bound to **(2)**:

    IntoThingT = TypeVar("IntoThingT", bound=IntoThing)

Important:
    In most situations, you'll want to use these as they **do preserve** the original native type.

Putting it all together, we can now add a *narwhals-level* wrapper:

    class Thing(Generic[IntoThingT]):
        def to_native(self) -> IntoThingT: ...

## Matching to an `Implementation`
This problem differs as we need to *create* a relationship between *otherwise-unrelated* types.

Comparing the problems side-by-side, we can more clearly see this difference:

    def wrapping_in_df(native: IntoDataFrameT) -> DataFrame[IntoDataFrameT]: ...
    def matching_to_polars(native: pl.DataFrame) -> Literal[Implementation.POLARS]: ...

### (4) `Native<Backend>`
If we want to describe a set of specific types and **match** them in [`@overload`s], then these the tools we need.

For common and easily-installed backends, [`TypeAlias`]s are composed of the native type(s):

    NativePolars: TypeAlias = pl.DataFrame | pl.LazyFrame | pl.Series

Otherwise, we need to define a [`Protocol`] which the native type(s) can **match** against *when* installed:

    class NativeDask(NativeLazyFrame, Protocol):
        _partition_type: type[pd.DataFrame]

Tip:
    The goal is to be as minimal as possible, while still being *specific-enough* to **not match** something else.

Important:
    See [ibis#9276 comment] for a more *in-depth* example that doesn't fit here ðŸ˜„

### (5) `is_native_<backend>`
[Type guards] for **(4)**, *similar* to those found in `nw.dependencies`.

They differ by checking **all** native types/protocols in a single-call and using ``Native<Backend>`` aliases.

[structural]: https://typing.python.org/en/latest/spec/glossary.html#term-structural
[nominal]: https://typing.python.org/en/latest/spec/glossary.html#term-nominal
[Native types]: https://narwhals-dev.github.io/narwhals/how_it_works/#polars-and-other-implementations
[Narwhals type]: https://narwhals-dev.github.io/narwhals/api-reference/dataframe/
[`Implementation`]: https://narwhals-dev.github.io/narwhals/api-reference/implementation/
[`Protocol`]: https://typing.python.org/en/latest/spec/protocol.html
[assignable to]: https://typing.python.org/en/latest/spec/glossary.html#term-assignable
[`TypeAlias`]: https://mypy.readthedocs.io/en/stable/kinds_of_types.html#type-aliases
[`TypeVar`]: https://mypy.readthedocs.io/en/stable/generics.html#type-variables-with-upper-bounds
[generic class]: https://docs.python.org/3/library/typing.html#user-defined-generic-types
[`@overload`s]: https://typing.python.org/en/latest/spec/overload.html
[ibis#9276 comment]: https://github.com/ibis-project/ibis/issues/9276#issuecomment-3292016818
[Type guards]: https://typing.python.org/en/latest/spec/narrowing.html
é    )Úannotations)ÚCallableÚ
CollectionÚIterableÚSized)ÚTYPE_CHECKINGÚAnyÚProtocolÚTypeVarÚUnionÚcast)Úget_cudfÚ	get_modinÚ
get_pandasÚ
get_polarsÚget_pyarrowÚis_dask_dataframeÚis_duckdb_relationÚis_ibis_tableÚis_pyspark_connect_dataframeÚis_pyspark_dataframeÚis_sqlframe_dataframeN)ÚBaseDataFrame)ÚSelfÚ	TypeAliasÚTypeIsÚTzCallable[[Any], TypeIs[T]]r   Ú_GuardÚ
Incomplete)+ÚIntoDataFrameÚIntoDataFrameTÚ	IntoFrameÚ
IntoFrameTÚIntoLazyFrameÚIntoLazyFrameTÚ
IntoSeriesÚIntoSeriesTÚ	NativeAnyÚNativeArrowÚ
NativeCuDFÚ
NativeDaskÚNativeDataFrameÚNativeDuckDBÚNativeFrameÚ
NativeIbisÚNativeKnownÚNativeLazyFrameÚNativeModinÚNativePandasÚNativePandasLikeÚNativePandasLikeDataFrameÚNativePandasLikeSeriesÚNativePolarsÚNativePySparkÚNativePySparkConnectÚNativeSQLFrameÚNativeSeriesÚNativeSparkLikeÚNativeUnknownÚis_native_arrowÚis_native_cudfÚis_native_daskÚis_native_duckdbÚis_native_ibisÚis_native_modinÚis_native_pandasÚis_native_pandas_likeÚis_native_polarsÚis_native_pysparkÚis_native_pyspark_connectÚis_native_spark_likeÚis_native_sqlframec                  ó&   — e Zd Zedd„«       Zdd„Zy)r.   c                 ó   — y ©N© ©Úselfs    úG/home/htdocs/ttos/venv/lib/python3.12/site-packages/narwhals/_native.pyÚcolumnszNativeFrame.columns¼   s   € Ø!ó    c                 ó   — y rM   rN   ©rP   ÚargsÚkwargss      rQ   ÚjoinzNativeFrame.join¾   ó   � rS   N©Úreturnr	   ©rV   r	   rW   r	   r[   r	   )Ú__name__Ú
__module__Ú__qualname__ÚpropertyrR   rX   rN   rS   rQ   r.   r.   »   s   „ ØÚ!ó Ø!Ü9rS   r.   c                  ó   — e Zd Zdd„Zy)r,   c                 ó   — y rM   rN   rU   s      rQ   ÚdropzNativeDataFrame.dropÂ   rY   rS   Nr\   )r]   r^   r_   rc   rN   rS   rQ   r,   r,   Á   s   „ Ü9rS   r,   c                  ó   — e Zd Zdd„Zy)r1   c                 ó   — y rM   rN   rU   s      rQ   ÚexplainzNativeLazyFrame.explainÆ   rY   rS   Nr\   )r]   r^   r_   rf   rN   rS   rQ   r1   r1   Å   s   „ Ü<rS   r1   c                  ó$   — e Zd Zdd„Zdd„Zdd„Zy)r;   c                 ó   — y rM   rN   rU   s      rQ   ÚfilterzNativeSeries.filterÊ   rY   rS   c                 ó   — y rM   rN   rU   s      rQ   Úvalue_countszNativeSeries.value_countsË   rY   rS   c                 ó   — y rM   rN   rU   s      rQ   ÚuniquezNativeSeries.uniqueÌ   rY   rS   Nr\   )r]   r^   r_   ri   rk   rm   rN   rS   rQ   r;   r;   É   s   „ Û;ÛAÜ;rS   r;   c                  óx   — e Zd ZU ded<   	 dd„Zdd„Zdd„Zedd„«       Zedd„«       Z	ddd	œdd
„Z
ddd„Zdd„Zy)Ú_BasePandasLiker	   Úindexc                ó   — y rM   rN   )rP   Úkeys     rQ   Ú__getitem__z_BasePandasLike.__getitem__Ó   rY   rS   c                ó   — y rM   rN   ©rP   Úothers     rQ   Ú__mul__z_BasePandasLike.__mul__Ô   rY   rS   c                ó   — y rM   rN   ru   s     rQ   Ú__floordiv__z_BasePandasLike.__floordiv__Õ   rY   rS   c                 ó   — y rM   rN   rO   s    rQ   Úlocz_BasePandasLike.locÖ   s   € ØrS   c                 ó   — y rM   rN   rO   s    rQ   Úshapez_BasePandasLike.shapeØ   s   € Ø(+rS   .)ÚaxisÚcopyc                ó   — y rM   rN   )rP   Úlabelsr~   r   s       rQ   Úset_axisz_BasePandasLike.set_axisÚ   rY   rS   c                 ó   — y rM   rN   )rP   Údeeps     rQ   r   z_BasePandasLike.copyÛ   rY   rS   c                 ó   — y)z·`mypy` & `pyright` disagree on overloads.

        `Incomplete` used to fix [more important issue](https://github.com/narwhals-dev/narwhals/pull/3016#discussion_r2296139744).
        NrN   ©rP   rV   Úkwdss      rQ   Úrenamez_BasePandasLike.renameÜ   rY   rS   N)rr   r	   r[   r	   )rv   z float | Collection[float] | Selfr[   r   rZ   )r[   ztuple[int, ...])r�   r	   r~   r	   r   Úboolr[   r   ©.)r„   r‰   r[   r   )rV   r	   r‡   r	   r[   úSelf | Incomplete)r]   r^   r_   Ú__annotations__rs   rw   ry   r`   r{   r}   r‚   r   rˆ   rN   rS   rQ   ro   ro   Ï   s?   … ØƒJØKã2ÛNÛSØÚó ØØÚ+ó Ø+Ø36ÀSÕVÜ1ôrS   ro   c                  ó   — e Zd Zy)Ú_BasePandasLikeFrameN)r]   r^   r_   rN   rS   rQ   rŽ   rŽ   ã   s   … rS   rŽ   c                  óN   — e Zd ZU ded<   	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdd	d„Zy)
Ú_BasePandasLikeSeriesú
Any | NoneÚnameNc                 ó   — y rM   rN   )rP   Údatarp   Údtyper’   rV   rW   s          rQ   Ú__init__z_BasePandasLikeSeries.__init__é   s   € ð rS   c                ó   — y rM   rN   )rP   Úcondrv   s      rQ   Úwherez_BasePandasLikeSeries.whereò   rY   rS   )NNNN)r”   úIterable[Any] | Nonerp   rš   r•   r‘   r’   r‘   rV   r	   rW   r	   r[   ÚNonerŠ   )r˜   r	   rv   r	   r[   r‹   )r]   r^   r_   rŒ   r–   r™   rN   rS   rQ   r�   r�   æ   se   … Ø
Óð &*Ø&*Ø Øðà"ðð $ðð ð	ð
 ðð ðð ðð 
óõ NrS   r�   c                  ó   — e Zd ZU ded<   y)r+   útype[pd.DataFrame]Ú_partition_typeN©r]   r^   r_   rŒ   rN   rS   rQ   r+   r+   õ   s   … Ø'Ô'rS   r+   c                  ó   — e Zd Zdd„Zy)Ú_CuDFDataFramec                 ó   — y rM   rN   r†   s      rQ   Úto_pylibcudfz_CuDFDataFrame.to_pylibcudfú   rY   rS   N©rV   r	   r‡   r	   r[   r	   ©r]   r^   r_   r£   rN   rS   rQ   r¡   r¡   ù   ó   „ Ü?rS   r¡   c                  ó   — e Zd Zdd„Zy)Ú_CuDFSeriesc                 ó   — y rM   rN   r†   s      rQ   r£   z_CuDFSeries.to_pylibcudfþ   rY   rS   Nr¤   r¥   rN   rS   rQ   r¨   r¨   ý   r¦   rS   r¨   c                  ó,   — e Zd Zdd„Zdd„Zdd„Zdd„Zy)r/   c                 ó   — y rM   rN   r†   s      rQ   ÚsqlzNativeIbis.sql  rY   rS   c                 ó   — y rM   rN   r†   s      rQ   Ú__pyarrow_result__zNativeIbis.__pyarrow_result__  rY   rS   c                 ó   — y rM   rN   r†   s      rQ   Ú__pandas_result__zNativeIbis.__pandas_result__  rY   rS   c                 ó   — y rM   rN   r†   s      rQ   Ú__polars_result__zNativeIbis.__polars_result__  rY   rS   Nr¤   )r]   r^   r_   r¬   r®   r°   r²   rN   rS   rQ   r/   r/     s   „ Û6ÛEÛDÜDrS   r/   c                  ó   — e Zd ZU ded<   y)Ú_ModinDataFramer�   Ú_pandas_classNrŸ   rN   rS   rQ   r´   r´     s   … Ø%Ô%rS   r´   c                  ó   — e Zd ZU ded<   y)Ú_ModinSeriesztype[pd.Series[Any]]rµ   NrŸ   rN   rS   rQ   r·   r·     s   … Ø'Ô'rS   r·   c                  ó   — e Zd Zdd„Zy)Ú_PySparkDataFramec                 ó   — y rM   rN   )rP   ÚargrW   s      rQ   ÚdropDuplicatesWithinWatermarkz/_PySparkDataFrame.dropDuplicatesWithinWatermark  rY   rS   N)r»   r	   rW   r	   r[   r	   )r]   r^   r_   r¼   rN   rS   rQ   r¹   r¹     s   „ ÜQrS   r¹   z'pl.DataFrame | pl.LazyFrame | pl.Seriesr7   zpa.Table | pa.ChunkedArray[Any]r)   zduckdb.DuckDBPyRelationr-   zpd.DataFrame | pd.Series[Any]r3   z_ModinDataFrame | _ModinSeriesr2   z_CuDFDataFrame | _CuDFSeriesr*   z+pd.Series[Any] | _CuDFSeries | _ModinSeriesr6   z/pd.DataFrame | _CuDFDataFrame | _ModinDataFramer5   z2NativePandasLikeDataFrame | NativePandasLikeSeriesr4   z'_BaseDataFrame[Any, Any, Any, Any, Any]r:   r8   r9   z5NativeSQLFrame | NativePySpark | NativePySparkConnectr<   zhNativePolars | NativeArrow | NativePandasLike | NativeSparkLike | NativeDuckDB | NativeDask | NativeIbisr0   z0NativeDataFrame | NativeSeries | NativeLazyFramer=   zNativeKnown | NativeUnknownr(   r    r$   r"   r&   r#   )Úboundr!   r%   r'   c                ó|   — t        «       x}d uxr- t        | |j                  |j                  |j                  f«      S rM   )r   Ú
isinstanceÚ	DataFrameÚSeriesÚ	LazyFrame)ÚobjÚpls     rQ   rF   rF   |  s:   € Ü“,ÐˆB tÐ+ò ´
Øˆb�l‰l˜BŸI™I r§|¡|Ð4ó1ð rS   c                óf   — t        «       x}d uxr" t        | |j                  |j                  f«      S rM   )r   r¿   ÚTableÚChunkedArray)rÃ   Úpas     rQ   r>   r>   ‚  s4   € Ü“-ÐˆB¨Ð,ò ´Øˆb�h‰h˜Ÿ™Ð(ó2ð rS   z_Guard[NativeDask]z_Guard[NativeDuckDB]rA   z_Guard[NativeSQLFrame]rJ   z_Guard[NativePySpark]z_Guard[NativePySparkConnect]z_Guard[NativeIbis]c                óf   — t        «       x}d uxr" t        | |j                  |j                  f«      S rM   )r   r¿   rÀ   rÁ   )rÃ   Úpds     rQ   rD   rD   ’  s-   € Ü“,ÐˆB tÐ+ÒZ´
¸3ÀÇÁÈrÏyÉyÐ@YÓ0ZÐZrS   c                óf   — t        «       x}d uxr" t        | |j                  |j                  f«      S rM   )r   r¿   rÀ   rÁ   )rÃ   Úmpds     rQ   rC   rC   –  s4   € Ü“;ÐˆC tÐ+ò ´
Øˆc�m‰m˜SŸZ™ZÐ(ó1ð rS   c                óf   — t        «       x}d uxr" t        | |j                  |j                  f«      S rM   )r   r¿   rÀ   rÁ   )rÃ   Úcudfs     rQ   r?   r?   œ  s4   € Ü“JÐˆD tÐ+ò ´
Øˆd�n‰n˜dŸk™kÐ*ó1ð rS   c                óL   — t        | «      xs t        | «      xs t        | «      S rM   )rD   r?   rC   ©rÃ   s    rQ   rE   rE   ¢  s!   € Ü˜CÓ ÒO¤N°3Ó$7ÒO¼?È3Ó;OÐOrS   c                óL   — t        | «      xs t        | «      xs t        | «      S rM   )rJ   rG   rH   rÐ   s    rQ   rI   rI   ¦  s)   € ä˜3Óò 	*Ü˜SÓ!ò	*ä$ SÓ)ðrS   )rÃ   r	   r[   zTypeIs[NativePolars])rÃ   r	   r[   zTypeIs[NativeArrow])rÃ   r	   r[   zTypeIs[NativePandas])rÃ   r	   r[   zTypeIs[NativeModin])rÃ   r	   r[   zTypeIs[NativeCuDF])rÃ   r	   r[   zTypeIs[NativePandasLike])rÃ   r	   r[   zTypeIs[NativeSparkLike])bÚ__doc__Ú
__future__r   Úcollections.abcr   r   r   r   Útypingr   r	   r
   r   r   r   Únarwhals.dependenciesr   r   r   r   r   r   r   r   r   r   r   ÚduckdbÚpandasrÊ   ÚpolarsrÄ   ÚpyarrowrÈ   Úsqlframe.base.dataframer   Ú_BaseDataFrameÚtyping_extensionsr   r   r   ÚSQLFrameDataFramer   r   rŒ   r   Ú__all__r.   r,   r1   r;   ro   rŽ   r�   r+   r¡   r¨   r/   r´   r·   r¹   r7   r)   r-   r3   r2   r*   r6   r5   r4   r:   r8   r9   r<   r0   r=   r(   r    r$   r"   r&   r#   r!   r%   r'   rF   r>   r@   rA   rJ   rG   rH   rB   rD   rC   r?   rE   rI   rN   rS   rQ   ú<module>rà      s   ðòfõP #ç AÓ Aß E× E÷÷ ÷ ñ ñ ÛÛÛÛÝGß9Ñ9à& s¨C°°c¸3Ð'>Ñ?ÐÙ�‹€AØ4€FˆIÓ4Ø€J�	Óò,€ôd:�(ô :ô:�e˜[¨(ô :ô=�k 8ô =ô<�5˜( 3™-¨ô <ô�e˜Xô ô( L˜?¨O¸XÔ KôN˜L¨/¸8ô Nô(� (ô (ô@Ð)¨8ô @ô@Ð'¨ô @ôE�˜hô Eô&Ð*¨Hô &ô(Ð(¨(ô (ôR˜¨ô Rð D€ˆiÓ CØ:€ˆYÓ :Ø3€ˆiÓ 3Ø9€ˆiÓ 9Ø9€ˆYÓ 9Ø6€
ˆIÓ 6Ø$QÐ ˜	Ó QØ'XÐ ˜9Ó XØRÐ �)Ó RØE€�	Ó EØ,€ˆyÓ ,Ø"3Ð �iÓ 3ØT€�Ó Tð D€ˆYó  DØM€ˆyÓ MØ4€	ˆ9Ó 4à*€ˆyÓ *ð
ð ! °*Ð!<Ñ=€ˆyÓ =Ø˜]¨MÐ9Ñ:€	ˆ9Ó :ðð %€
ˆIÓ $ðñ �\¨Ô3€
ðñ Ð)°Ô?€ðñ Ð)°Ô?€Ù�m¨:Ô6€ðóóñ Ð*Ð,=Ó>€Ø);Ð Ð&Ó ;Ø-BÐ Ð*Ó BÙÐ0Ð2FÓGÐ Ù Ø"Ð$@óÐ ñ Ð*¨MÓ:€ó[óóóPôrS   