|
3 | 3 | import datetime as dt |
4 | 4 | from decimal import Decimal |
5 | 5 | from functools import wraps |
6 | | -from typing import TYPE_CHECKING, Any, Callable, Literal, TypeVar, overload |
| 6 | +from typing import TYPE_CHECKING, Any, Callable, Literal, TypedDict, TypeVar, overload |
7 | 7 |
|
8 | 8 | from narwhals._constants import EPOCH, MS_PER_SECOND |
9 | 9 | from narwhals._namespace import ( |
|
30 | 30 | ) |
31 | 31 |
|
32 | 32 | if TYPE_CHECKING: |
| 33 | + from typing_extensions import NotRequired, Unpack |
| 34 | + |
33 | 35 | from narwhals.dataframe import DataFrame, LazyFrame |
34 | 36 | from narwhals.series import Series |
35 | 37 | from narwhals.typing import ( |
@@ -99,148 +101,144 @@ def to_native( |
99 | 101 | return narwhals_object |
100 | 102 |
|
101 | 103 |
|
102 | | -@overload |
103 | | -def from_native(native_object: SeriesT, **kwds: Any) -> SeriesT: ... |
| 104 | +# Upper bound |
| 105 | +class FromNativeFlags(TypedDict, total=False): |
| 106 | + pass_through: bool |
| 107 | + eager_only: bool |
| 108 | + series_only: bool |
| 109 | + allow_series: bool | None |
104 | 110 |
|
105 | 111 |
|
106 | | -@overload |
107 | | -def from_native(native_object: DataFrameT, **kwds: Any) -> DataFrameT: ... |
| 112 | +class DefaultFlags(TypedDict, total=False): |
| 113 | + pass_through: Literal[False] |
| 114 | + eager_only: Literal[False] |
| 115 | + series_only: Literal[False] |
| 116 | + allow_series: None |
108 | 117 |
|
109 | 118 |
|
110 | | -@overload |
111 | | -def from_native(native_object: LazyFrameT, **kwds: Any) -> LazyFrameT: ... |
| 119 | +class SeriesNever(TypedDict, total=False): |
| 120 | + pass_through: bool |
| 121 | + eager_only: bool |
| 122 | + series_only: Literal[False] |
| 123 | + allow_series: Literal[False] | None |
| 124 | + |
| 125 | + |
| 126 | +class PassThrough(TypedDict): |
| 127 | + pass_through: Literal[True] |
| 128 | + |
| 129 | + |
| 130 | +class SeriesAllow(TypedDict): |
| 131 | + pass_through: NotRequired[bool] |
| 132 | + eager_only: NotRequired[bool] |
| 133 | + series_only: NotRequired[Literal[False]] |
| 134 | + allow_series: Literal[True] |
| 135 | + |
| 136 | + |
| 137 | +class SeriesOnly(TypedDict): |
| 138 | + pass_through: NotRequired[bool] |
| 139 | + eager_only: NotRequired[bool] |
| 140 | + series_only: Literal[True] |
| 141 | + allow_series: NotRequired[bool | None] |
| 142 | + |
| 143 | + |
| 144 | +class EagerOnly(TypedDict): |
| 145 | + pass_through: NotRequired[bool] |
| 146 | + eager_only: Literal[True] |
| 147 | + series_only: NotRequired[bool] |
| 148 | + allow_series: NotRequired[bool | None] |
| 149 | + |
| 150 | + |
| 151 | +class LazyAllow(TypedDict): |
| 152 | + pass_through: NotRequired[bool] |
| 153 | + eager_only: NotRequired[Literal[False]] |
| 154 | + series_only: NotRequired[Literal[False]] |
| 155 | + allow_series: NotRequired[bool | None] |
112 | 156 |
|
113 | 157 |
|
114 | 158 | @overload |
115 | | -def from_native( |
116 | | - native_object: IntoDataFrameT | IntoSeriesT, |
117 | | - *, |
118 | | - pass_through: Literal[True], |
119 | | - eager_only: Literal[True], |
120 | | - series_only: Literal[False] = ..., |
121 | | - allow_series: Literal[True], |
122 | | -) -> DataFrame[IntoDataFrameT] | Series[IntoSeriesT]: ... |
| 159 | +def from_native(native_object: SeriesT, **kwds: Any) -> SeriesT: ... |
123 | 160 |
|
124 | 161 |
|
125 | 162 | @overload |
126 | | -def from_native( |
127 | | - native_object: IntoDataFrameT, |
128 | | - *, |
129 | | - pass_through: Literal[True], |
130 | | - eager_only: Literal[False] = ..., |
131 | | - series_only: Literal[False] = ..., |
132 | | - allow_series: None = ..., |
133 | | -) -> DataFrame[IntoDataFrameT]: ... |
| 163 | +def from_native(native_object: DataFrameT, **kwds: Any) -> DataFrameT: ... |
134 | 164 |
|
135 | 165 |
|
136 | 166 | @overload |
137 | | -def from_native( |
138 | | - native_object: T, |
139 | | - *, |
140 | | - pass_through: Literal[True], |
141 | | - eager_only: Literal[False] = ..., |
142 | | - series_only: Literal[False] = ..., |
143 | | - allow_series: None = ..., |
144 | | -) -> T: ... |
| 167 | +def from_native(native_object: LazyFrameT, **kwds: Any) -> LazyFrameT: ... |
145 | 168 |
|
146 | 169 |
|
147 | 170 | @overload |
148 | 171 | def from_native( |
149 | | - native_object: IntoDataFrameT, |
150 | | - *, |
151 | | - pass_through: Literal[True], |
152 | | - eager_only: Literal[True], |
153 | | - series_only: Literal[False] = ..., |
154 | | - allow_series: None = ..., |
| 172 | + native_object: IntoDataFrameT, **kwds: Unpack[SeriesNever] |
155 | 173 | ) -> DataFrame[IntoDataFrameT]: ... |
156 | 174 |
|
157 | 175 |
|
158 | 176 | @overload |
159 | 177 | def from_native( |
160 | | - native_object: T, |
161 | | - *, |
162 | | - pass_through: Literal[True], |
163 | | - eager_only: Literal[True], |
164 | | - series_only: Literal[False] = ..., |
165 | | - allow_series: None = ..., |
166 | | -) -> T: ... |
| 178 | + native_object: IntoSeriesT, **kwds: Unpack[SeriesOnly] |
| 179 | +) -> Series[IntoSeriesT]: ... |
167 | 180 |
|
168 | 181 |
|
169 | 182 | @overload |
170 | 183 | def from_native( |
171 | | - native_object: IntoDataFrameT | IntoLazyFrameT | IntoSeriesT, |
172 | | - *, |
173 | | - pass_through: Literal[True], |
174 | | - eager_only: Literal[False] = ..., |
175 | | - series_only: Literal[False] = ..., |
176 | | - allow_series: Literal[True], |
177 | | -) -> DataFrame[IntoDataFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT]: ... |
| 184 | + native_object: IntoSeriesT, **kwds: Unpack[SeriesAllow] |
| 185 | +) -> Series[IntoSeriesT]: ... |
178 | 186 |
|
179 | 187 |
|
180 | 188 | @overload |
181 | 189 | def from_native( |
182 | | - native_object: IntoSeriesT, |
183 | | - *, |
184 | | - pass_through: Literal[True], |
185 | | - eager_only: Literal[False] = ..., |
186 | | - series_only: Literal[True], |
187 | | - allow_series: None = ..., |
188 | | -) -> Series[IntoSeriesT]: ... |
| 190 | + native_object: IntoLazyFrameT, **kwds: Unpack[LazyAllow] |
| 191 | +) -> LazyFrame[IntoLazyFrameT]: ... |
189 | 192 |
|
190 | 193 |
|
191 | 194 | @overload |
192 | 195 | def from_native( |
193 | | - native_object: IntoLazyFrameT, |
194 | | - *, |
195 | | - pass_through: Literal[False] = ..., |
196 | | - eager_only: Literal[False] = ..., |
197 | | - series_only: Literal[False] = ..., |
198 | | - allow_series: None = ..., |
199 | | -) -> LazyFrame[IntoLazyFrameT]: ... |
| 196 | + native_object: IntoDataFrameT | IntoSeriesT, **kwds: Unpack[SeriesAllow] |
| 197 | +) -> DataFrame[IntoDataFrameT] | Series[IntoSeriesT]: ... |
200 | 198 |
|
201 | 199 |
|
202 | 200 | @overload |
203 | 201 | def from_native( |
204 | | - native_object: IntoDataFrameT, |
| 202 | + native_object: T, |
205 | 203 | *, |
206 | | - pass_through: Literal[False] = ..., |
| 204 | + pass_through: Literal[True], |
207 | 205 | eager_only: Literal[False] = ..., |
208 | 206 | series_only: Literal[False] = ..., |
209 | 207 | allow_series: None = ..., |
210 | | -) -> DataFrame[IntoDataFrameT]: ... |
| 208 | +) -> T: ... |
211 | 209 |
|
212 | 210 |
|
213 | 211 | @overload |
214 | 212 | def from_native( |
215 | | - native_object: IntoDataFrameT, |
| 213 | + native_object: T, |
216 | 214 | *, |
217 | | - pass_through: Literal[False] = ..., |
| 215 | + pass_through: Literal[True], |
218 | 216 | eager_only: Literal[True], |
219 | 217 | series_only: Literal[False] = ..., |
220 | 218 | allow_series: None = ..., |
221 | | -) -> DataFrame[IntoDataFrameT]: ... |
| 219 | +) -> T: ... |
222 | 220 |
|
223 | 221 |
|
224 | 222 | @overload |
225 | 223 | def from_native( |
226 | | - native_object: IntoFrame | IntoSeries, |
| 224 | + native_object: IntoDataFrameT | IntoLazyFrameT | IntoSeriesT, |
227 | 225 | *, |
228 | | - pass_through: Literal[False] = ..., |
| 226 | + pass_through: Literal[True], |
229 | 227 | eager_only: Literal[False] = ..., |
230 | 228 | series_only: Literal[False] = ..., |
231 | 229 | allow_series: Literal[True], |
232 | | -) -> DataFrame[Any] | LazyFrame[Any] | Series[Any]: ... |
| 230 | +) -> DataFrame[IntoDataFrameT] | LazyFrame[IntoLazyFrameT] | Series[IntoSeriesT]: ... |
233 | 231 |
|
234 | 232 |
|
235 | 233 | @overload |
236 | 234 | def from_native( |
237 | | - native_object: IntoSeriesT, |
| 235 | + native_object: IntoFrame | IntoSeries, |
238 | 236 | *, |
239 | 237 | pass_through: Literal[False] = ..., |
240 | 238 | eager_only: Literal[False] = ..., |
241 | | - series_only: Literal[True], |
242 | | - allow_series: None = ..., |
243 | | -) -> Series[IntoSeriesT]: ... |
| 239 | + series_only: Literal[False] = ..., |
| 240 | + allow_series: Literal[True], |
| 241 | +) -> DataFrame[Any] | LazyFrame[Any] | Series[Any]: ... |
244 | 242 |
|
245 | 243 |
|
246 | 244 | # All params passed in as variables |
|
0 commit comments