polars-runtime-compat 1.34.0b2__cp39-abi3-macosx_10_12_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of polars-runtime-compat might be problematic. Click here for more details.

Files changed (203) hide show
  1. _polars_runtime_compat/.gitkeep +0 -0
  2. _polars_runtime_compat/_polars_runtime_compat.abi3.so +0 -0
  3. polars/__init__.py +528 -0
  4. polars/_cpu_check.py +265 -0
  5. polars/_dependencies.py +355 -0
  6. polars/_plr.py +99 -0
  7. polars/_plr.pyi +2496 -0
  8. polars/_reexport.py +23 -0
  9. polars/_typing.py +478 -0
  10. polars/_utils/__init__.py +37 -0
  11. polars/_utils/async_.py +102 -0
  12. polars/_utils/cache.py +176 -0
  13. polars/_utils/cloud.py +40 -0
  14. polars/_utils/constants.py +29 -0
  15. polars/_utils/construction/__init__.py +46 -0
  16. polars/_utils/construction/dataframe.py +1397 -0
  17. polars/_utils/construction/other.py +72 -0
  18. polars/_utils/construction/series.py +560 -0
  19. polars/_utils/construction/utils.py +118 -0
  20. polars/_utils/convert.py +224 -0
  21. polars/_utils/deprecation.py +406 -0
  22. polars/_utils/getitem.py +457 -0
  23. polars/_utils/logging.py +11 -0
  24. polars/_utils/nest_asyncio.py +264 -0
  25. polars/_utils/parquet.py +15 -0
  26. polars/_utils/parse/__init__.py +12 -0
  27. polars/_utils/parse/expr.py +242 -0
  28. polars/_utils/polars_version.py +19 -0
  29. polars/_utils/pycapsule.py +53 -0
  30. polars/_utils/scan.py +27 -0
  31. polars/_utils/serde.py +63 -0
  32. polars/_utils/slice.py +215 -0
  33. polars/_utils/udfs.py +1251 -0
  34. polars/_utils/unstable.py +63 -0
  35. polars/_utils/various.py +782 -0
  36. polars/_utils/wrap.py +25 -0
  37. polars/api.py +370 -0
  38. polars/catalog/__init__.py +0 -0
  39. polars/catalog/unity/__init__.py +19 -0
  40. polars/catalog/unity/client.py +733 -0
  41. polars/catalog/unity/models.py +152 -0
  42. polars/config.py +1571 -0
  43. polars/convert/__init__.py +25 -0
  44. polars/convert/general.py +1046 -0
  45. polars/convert/normalize.py +261 -0
  46. polars/dataframe/__init__.py +5 -0
  47. polars/dataframe/_html.py +186 -0
  48. polars/dataframe/frame.py +12582 -0
  49. polars/dataframe/group_by.py +1067 -0
  50. polars/dataframe/plotting.py +257 -0
  51. polars/datatype_expr/__init__.py +5 -0
  52. polars/datatype_expr/array.py +56 -0
  53. polars/datatype_expr/datatype_expr.py +304 -0
  54. polars/datatype_expr/list.py +18 -0
  55. polars/datatype_expr/struct.py +69 -0
  56. polars/datatypes/__init__.py +122 -0
  57. polars/datatypes/_parse.py +195 -0
  58. polars/datatypes/_utils.py +48 -0
  59. polars/datatypes/classes.py +1213 -0
  60. polars/datatypes/constants.py +11 -0
  61. polars/datatypes/constructor.py +172 -0
  62. polars/datatypes/convert.py +366 -0
  63. polars/datatypes/group.py +130 -0
  64. polars/exceptions.py +230 -0
  65. polars/expr/__init__.py +7 -0
  66. polars/expr/array.py +964 -0
  67. polars/expr/binary.py +346 -0
  68. polars/expr/categorical.py +306 -0
  69. polars/expr/datetime.py +2620 -0
  70. polars/expr/expr.py +11272 -0
  71. polars/expr/list.py +1408 -0
  72. polars/expr/meta.py +444 -0
  73. polars/expr/name.py +321 -0
  74. polars/expr/string.py +3045 -0
  75. polars/expr/struct.py +357 -0
  76. polars/expr/whenthen.py +185 -0
  77. polars/functions/__init__.py +193 -0
  78. polars/functions/aggregation/__init__.py +33 -0
  79. polars/functions/aggregation/horizontal.py +298 -0
  80. polars/functions/aggregation/vertical.py +341 -0
  81. polars/functions/as_datatype.py +848 -0
  82. polars/functions/business.py +138 -0
  83. polars/functions/col.py +384 -0
  84. polars/functions/datatype.py +121 -0
  85. polars/functions/eager.py +524 -0
  86. polars/functions/escape_regex.py +29 -0
  87. polars/functions/lazy.py +2751 -0
  88. polars/functions/len.py +68 -0
  89. polars/functions/lit.py +210 -0
  90. polars/functions/random.py +22 -0
  91. polars/functions/range/__init__.py +19 -0
  92. polars/functions/range/_utils.py +15 -0
  93. polars/functions/range/date_range.py +303 -0
  94. polars/functions/range/datetime_range.py +370 -0
  95. polars/functions/range/int_range.py +348 -0
  96. polars/functions/range/linear_space.py +311 -0
  97. polars/functions/range/time_range.py +287 -0
  98. polars/functions/repeat.py +301 -0
  99. polars/functions/whenthen.py +353 -0
  100. polars/interchange/__init__.py +10 -0
  101. polars/interchange/buffer.py +77 -0
  102. polars/interchange/column.py +190 -0
  103. polars/interchange/dataframe.py +230 -0
  104. polars/interchange/from_dataframe.py +328 -0
  105. polars/interchange/protocol.py +303 -0
  106. polars/interchange/utils.py +170 -0
  107. polars/io/__init__.py +64 -0
  108. polars/io/_utils.py +317 -0
  109. polars/io/avro.py +49 -0
  110. polars/io/clipboard.py +36 -0
  111. polars/io/cloud/__init__.py +17 -0
  112. polars/io/cloud/_utils.py +80 -0
  113. polars/io/cloud/credential_provider/__init__.py +17 -0
  114. polars/io/cloud/credential_provider/_builder.py +520 -0
  115. polars/io/cloud/credential_provider/_providers.py +618 -0
  116. polars/io/csv/__init__.py +9 -0
  117. polars/io/csv/_utils.py +38 -0
  118. polars/io/csv/batched_reader.py +142 -0
  119. polars/io/csv/functions.py +1495 -0
  120. polars/io/database/__init__.py +6 -0
  121. polars/io/database/_arrow_registry.py +70 -0
  122. polars/io/database/_cursor_proxies.py +147 -0
  123. polars/io/database/_executor.py +578 -0
  124. polars/io/database/_inference.py +314 -0
  125. polars/io/database/_utils.py +144 -0
  126. polars/io/database/functions.py +516 -0
  127. polars/io/delta.py +499 -0
  128. polars/io/iceberg/__init__.py +3 -0
  129. polars/io/iceberg/_utils.py +697 -0
  130. polars/io/iceberg/dataset.py +556 -0
  131. polars/io/iceberg/functions.py +151 -0
  132. polars/io/ipc/__init__.py +8 -0
  133. polars/io/ipc/functions.py +514 -0
  134. polars/io/json/__init__.py +3 -0
  135. polars/io/json/read.py +101 -0
  136. polars/io/ndjson.py +332 -0
  137. polars/io/parquet/__init__.py +17 -0
  138. polars/io/parquet/field_overwrites.py +140 -0
  139. polars/io/parquet/functions.py +722 -0
  140. polars/io/partition.py +491 -0
  141. polars/io/plugins.py +187 -0
  142. polars/io/pyarrow_dataset/__init__.py +5 -0
  143. polars/io/pyarrow_dataset/anonymous_scan.py +109 -0
  144. polars/io/pyarrow_dataset/functions.py +79 -0
  145. polars/io/scan_options/__init__.py +5 -0
  146. polars/io/scan_options/_options.py +59 -0
  147. polars/io/scan_options/cast_options.py +126 -0
  148. polars/io/spreadsheet/__init__.py +6 -0
  149. polars/io/spreadsheet/_utils.py +52 -0
  150. polars/io/spreadsheet/_write_utils.py +647 -0
  151. polars/io/spreadsheet/functions.py +1323 -0
  152. polars/lazyframe/__init__.py +9 -0
  153. polars/lazyframe/engine_config.py +61 -0
  154. polars/lazyframe/frame.py +8564 -0
  155. polars/lazyframe/group_by.py +669 -0
  156. polars/lazyframe/in_process.py +42 -0
  157. polars/lazyframe/opt_flags.py +333 -0
  158. polars/meta/__init__.py +14 -0
  159. polars/meta/build.py +33 -0
  160. polars/meta/index_type.py +27 -0
  161. polars/meta/thread_pool.py +50 -0
  162. polars/meta/versions.py +120 -0
  163. polars/ml/__init__.py +0 -0
  164. polars/ml/torch.py +213 -0
  165. polars/ml/utilities.py +30 -0
  166. polars/plugins.py +155 -0
  167. polars/py.typed +0 -0
  168. polars/pyproject.toml +96 -0
  169. polars/schema.py +265 -0
  170. polars/selectors.py +3117 -0
  171. polars/series/__init__.py +5 -0
  172. polars/series/array.py +776 -0
  173. polars/series/binary.py +254 -0
  174. polars/series/categorical.py +246 -0
  175. polars/series/datetime.py +2275 -0
  176. polars/series/list.py +1087 -0
  177. polars/series/plotting.py +191 -0
  178. polars/series/series.py +9197 -0
  179. polars/series/string.py +2367 -0
  180. polars/series/struct.py +154 -0
  181. polars/series/utils.py +191 -0
  182. polars/sql/__init__.py +7 -0
  183. polars/sql/context.py +677 -0
  184. polars/sql/functions.py +139 -0
  185. polars/string_cache.py +185 -0
  186. polars/testing/__init__.py +13 -0
  187. polars/testing/asserts/__init__.py +9 -0
  188. polars/testing/asserts/frame.py +231 -0
  189. polars/testing/asserts/series.py +219 -0
  190. polars/testing/asserts/utils.py +12 -0
  191. polars/testing/parametric/__init__.py +33 -0
  192. polars/testing/parametric/profiles.py +107 -0
  193. polars/testing/parametric/strategies/__init__.py +22 -0
  194. polars/testing/parametric/strategies/_utils.py +14 -0
  195. polars/testing/parametric/strategies/core.py +615 -0
  196. polars/testing/parametric/strategies/data.py +452 -0
  197. polars/testing/parametric/strategies/dtype.py +436 -0
  198. polars/testing/parametric/strategies/legacy.py +169 -0
  199. polars/type_aliases.py +24 -0
  200. polars_runtime_compat-1.34.0b2.dist-info/METADATA +190 -0
  201. polars_runtime_compat-1.34.0b2.dist-info/RECORD +203 -0
  202. polars_runtime_compat-1.34.0b2.dist-info/WHEEL +4 -0
  203. polars_runtime_compat-1.34.0b2.dist-info/licenses/LICENSE +20 -0
@@ -0,0 +1,436 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ import hypothesis.strategies as st
6
+ from hypothesis.errors import InvalidArgument
7
+
8
+ from polars.datatypes import (
9
+ Array,
10
+ Binary,
11
+ Boolean,
12
+ Categorical,
13
+ DataType,
14
+ Date,
15
+ Datetime,
16
+ Decimal,
17
+ Duration,
18
+ Enum,
19
+ Field,
20
+ Float32,
21
+ Float64,
22
+ Int8,
23
+ Int16,
24
+ Int32,
25
+ Int64,
26
+ Int128,
27
+ List,
28
+ Null,
29
+ String,
30
+ Struct,
31
+ Time,
32
+ UInt8,
33
+ UInt16,
34
+ UInt32,
35
+ UInt64,
36
+ UInt128,
37
+ )
38
+
39
+ if TYPE_CHECKING:
40
+ from collections.abc import Collection, Sequence
41
+
42
+ from hypothesis.strategies import DrawFn, SearchStrategy
43
+
44
+ from polars._typing import PolarsDataType, TimeUnit
45
+ from polars.datatypes import DataTypeClass
46
+
47
+
48
+ # Supported data type classes which do not take any arguments
49
+ _SIMPLE_DTYPES: list[DataTypeClass] = [
50
+ Int8,
51
+ Int16,
52
+ Int32,
53
+ Int64,
54
+ Int128,
55
+ Float64,
56
+ Float32,
57
+ Boolean,
58
+ UInt8,
59
+ UInt16,
60
+ UInt32,
61
+ UInt64,
62
+ UInt128,
63
+ String,
64
+ Binary,
65
+ Date,
66
+ Time,
67
+ Null,
68
+ # TODO: Enable Object types by default when various issues are solved.
69
+ # Object,
70
+ ]
71
+ # Supported data type classes with arguments
72
+ _COMPLEX_DTYPES: list[DataTypeClass] = [
73
+ Datetime,
74
+ Duration,
75
+ Categorical,
76
+ Decimal,
77
+ Enum,
78
+ ]
79
+ # Supported data type classes that contain other data types
80
+ _NESTED_DTYPES: list[DataTypeClass] = [
81
+ # TODO: Enable nested types by default when various issues are solved.
82
+ # List,
83
+ # Array,
84
+ Struct,
85
+ ]
86
+ # Supported data type classes that do not contain other data types
87
+ _FLAT_DTYPES = _SIMPLE_DTYPES + _COMPLEX_DTYPES
88
+
89
+ _DEFAULT_ARRAY_WIDTH_LIMIT = 3
90
+ _DEFAULT_STRUCT_FIELDS_LIMIT = 3
91
+ _DEFAULT_ENUM_CATEGORIES_LIMIT = 3
92
+
93
+
94
+ def dtypes(
95
+ *,
96
+ allowed_dtypes: Collection[PolarsDataType] | None = None,
97
+ excluded_dtypes: Sequence[PolarsDataType] | None = None,
98
+ allow_time_zones: bool = True,
99
+ nesting_level: int = 3,
100
+ ) -> SearchStrategy[DataType]:
101
+ """
102
+ Create a strategy for generating Polars :class:`DataType` objects.
103
+
104
+ .. warning::
105
+ This functionality is currently considered **unstable**. It may be
106
+ changed at any point without it being considered a breaking change.
107
+
108
+ Parameters
109
+ ----------
110
+ allowed_dtypes
111
+ Data types the strategy will pick from. If set to `None` (default),
112
+ all supported data types are included.
113
+ excluded_dtypes
114
+ Data types the strategy will *not* pick from. This takes priority over
115
+ data types specified in `allowed_dtypes`.
116
+ allow_time_zones
117
+ Allow generating `Datetime` data types with a time zone.
118
+ nesting_level
119
+ The complexity of nested data types. If set to 0, nested data types are
120
+ disabled.
121
+ """
122
+ flat_dtypes, nested_dtypes, excluded_dtypes = _parse_dtype_restrictions(
123
+ allowed_dtypes, excluded_dtypes
124
+ )
125
+
126
+ if nesting_level > 0 and nested_dtypes:
127
+ if not flat_dtypes:
128
+ return _nested_dtypes(
129
+ inner=st.just(Null()),
130
+ allowed_dtypes=nested_dtypes,
131
+ excluded_dtypes=excluded_dtypes,
132
+ allow_time_zones=allow_time_zones,
133
+ )
134
+ return st.recursive(
135
+ base=_flat_dtypes(
136
+ allowed_dtypes=flat_dtypes,
137
+ excluded_dtypes=excluded_dtypes,
138
+ allow_time_zones=allow_time_zones,
139
+ ),
140
+ extend=lambda s: _nested_dtypes(
141
+ s,
142
+ allowed_dtypes=nested_dtypes,
143
+ excluded_dtypes=excluded_dtypes,
144
+ allow_time_zones=allow_time_zones,
145
+ ),
146
+ max_leaves=nesting_level,
147
+ )
148
+ else:
149
+ return _flat_dtypes(
150
+ allowed_dtypes=flat_dtypes,
151
+ excluded_dtypes=excluded_dtypes,
152
+ allow_time_zones=allow_time_zones,
153
+ )
154
+
155
+
156
+ def _parse_dtype_restrictions(
157
+ allowed_dtypes: Collection[PolarsDataType] | None = None,
158
+ excluded_dtypes: Sequence[PolarsDataType] | None = None,
159
+ ) -> tuple[list[PolarsDataType], list[PolarsDataType], list[DataType]]:
160
+ """
161
+ Parse data type restrictions.
162
+
163
+ Splits allowed data types into flat and nested data types.
164
+ Filters the allowed data types by excluded data type classes.
165
+ Excluded instantiated data types are returned to be filtered later.
166
+ """
167
+ # Split excluded dtypes into instances and classes
168
+ excluded_dtypes_instance = []
169
+ excluded_dtypes_class = []
170
+ if excluded_dtypes:
171
+ for dt in excluded_dtypes:
172
+ if isinstance(dt, DataType):
173
+ excluded_dtypes_instance.append(dt)
174
+ else:
175
+ excluded_dtypes_class.append(dt)
176
+
177
+ # Split allowed dtypes into flat and nested, excluding certain dtype classes
178
+ allowed_dtypes_flat: list[PolarsDataType]
179
+ allowed_dtypes_nested: list[PolarsDataType]
180
+ if allowed_dtypes is None:
181
+ allowed_dtypes_flat = [
182
+ dt for dt in _FLAT_DTYPES if dt not in excluded_dtypes_class
183
+ ]
184
+ allowed_dtypes_nested = [
185
+ dt for dt in _NESTED_DTYPES if dt not in excluded_dtypes_class
186
+ ]
187
+ else:
188
+ allowed_dtypes_flat = []
189
+ allowed_dtypes_nested = []
190
+ for dt in allowed_dtypes:
191
+ if dt in excluded_dtypes_class:
192
+ continue
193
+ elif dt.is_nested():
194
+ allowed_dtypes_nested.append(dt)
195
+ else:
196
+ allowed_dtypes_flat.append(dt)
197
+
198
+ return allowed_dtypes_flat, allowed_dtypes_nested, excluded_dtypes_instance
199
+
200
+
201
+ @st.composite
202
+ def _flat_dtypes(
203
+ draw: DrawFn,
204
+ allowed_dtypes: Sequence[PolarsDataType] | None = None,
205
+ excluded_dtypes: Sequence[PolarsDataType] | None = None,
206
+ *,
207
+ allow_time_zones: bool = True,
208
+ ) -> DataType:
209
+ """Create a strategy for generating non-nested Polars :class:`DataType` objects."""
210
+ if allowed_dtypes is None:
211
+ allowed_dtypes = _FLAT_DTYPES
212
+ if excluded_dtypes is None:
213
+ excluded_dtypes = []
214
+
215
+ dtype = draw(st.sampled_from(allowed_dtypes))
216
+ return draw(
217
+ _instantiate_flat_dtype(dtype, allow_time_zones=allow_time_zones).filter(
218
+ lambda x: x not in excluded_dtypes
219
+ )
220
+ )
221
+
222
+
223
+ @st.composite
224
+ def _instantiate_flat_dtype(
225
+ draw: DrawFn, dtype: PolarsDataType, *, allow_time_zones: bool = True
226
+ ) -> DataType:
227
+ """Take a flat data type and instantiate it."""
228
+ if isinstance(dtype, DataType):
229
+ return dtype
230
+ elif dtype in _SIMPLE_DTYPES:
231
+ return dtype()
232
+ elif dtype == Datetime:
233
+ time_unit = draw(_time_units())
234
+ time_zone = draw(st.none() | _time_zones()) if allow_time_zones else None
235
+ return Datetime(time_unit, time_zone)
236
+ elif dtype == Duration:
237
+ time_unit = draw(_time_units())
238
+ return Duration(time_unit)
239
+ elif dtype == Categorical:
240
+ return Categorical()
241
+ elif dtype == Enum:
242
+ n_categories = draw(
243
+ st.integers(min_value=1, max_value=_DEFAULT_ENUM_CATEGORIES_LIMIT)
244
+ )
245
+ categories = [f"c{i}" for i in range(n_categories)]
246
+ return Enum(categories)
247
+ elif dtype == Decimal:
248
+ precision = draw(st.integers(min_value=1, max_value=38) | st.none())
249
+ scale = draw(st.integers(min_value=0, max_value=precision or 38))
250
+ return Decimal(precision, scale)
251
+ else:
252
+ msg = f"unsupported data type: {dtype}"
253
+ raise InvalidArgument(msg)
254
+
255
+
256
+ @st.composite
257
+ def _nested_dtypes(
258
+ draw: DrawFn,
259
+ inner: SearchStrategy[DataType],
260
+ allowed_dtypes: Sequence[PolarsDataType] | None = None,
261
+ excluded_dtypes: Sequence[PolarsDataType] | None = None,
262
+ *,
263
+ allow_time_zones: bool = True,
264
+ ) -> DataType:
265
+ """Create a strategy for generating nested Polars :class:`DataType` objects."""
266
+ if allowed_dtypes is None:
267
+ allowed_dtypes = _NESTED_DTYPES
268
+ if excluded_dtypes is None:
269
+ excluded_dtypes = []
270
+
271
+ dtype = draw(st.sampled_from(allowed_dtypes))
272
+ return draw(
273
+ _instantiate_nested_dtype(
274
+ dtype, inner, allow_time_zones=allow_time_zones
275
+ ).filter(lambda x: x not in excluded_dtypes)
276
+ )
277
+
278
+
279
+ @st.composite
280
+ def _instantiate_nested_dtype(
281
+ draw: DrawFn,
282
+ dtype: PolarsDataType,
283
+ inner: SearchStrategy[DataType],
284
+ *,
285
+ allow_time_zones: bool = True,
286
+ ) -> DataType:
287
+ """Take a nested data type and instantiate it."""
288
+
289
+ def instantiate_inner(inner_dtype: PolarsDataType | None) -> DataType:
290
+ if inner_dtype is None:
291
+ return draw(inner)
292
+ elif inner_dtype.is_nested():
293
+ return draw(
294
+ _instantiate_nested_dtype(
295
+ inner_dtype, inner, allow_time_zones=allow_time_zones
296
+ )
297
+ )
298
+ else:
299
+ return draw(
300
+ _instantiate_flat_dtype(inner_dtype, allow_time_zones=allow_time_zones)
301
+ )
302
+
303
+ if dtype == List:
304
+ inner_dtype = instantiate_inner(getattr(dtype, "inner", None))
305
+ return List(inner_dtype)
306
+ elif dtype == Array:
307
+ inner_dtype = instantiate_inner(getattr(dtype, "inner", None))
308
+ size = getattr(
309
+ dtype,
310
+ "size",
311
+ draw(st.integers(min_value=1, max_value=_DEFAULT_ARRAY_WIDTH_LIMIT)),
312
+ )
313
+ return Array(inner_dtype, size)
314
+ elif dtype == Struct:
315
+ if isinstance(dtype, Struct):
316
+ fields = [Field(f.name, instantiate_inner(f.dtype)) for f in dtype.fields]
317
+ else:
318
+ n_fields = draw(
319
+ st.integers(min_value=1, max_value=_DEFAULT_STRUCT_FIELDS_LIMIT)
320
+ )
321
+ fields = [Field(f"f{i}", draw(inner)) for i in range(n_fields)]
322
+ return Struct(fields)
323
+ else:
324
+ msg = f"unsupported data type: {dtype}"
325
+ raise InvalidArgument(msg)
326
+
327
+
328
+ def _time_units() -> SearchStrategy[TimeUnit]:
329
+ """Create a strategy for generating valid units of time."""
330
+ return st.sampled_from(["us", "ns", "ms"])
331
+
332
+
333
+ def _time_zones() -> SearchStrategy[str]:
334
+ """Create a strategy for generating valid time zones."""
335
+ # Not available when building docs, so just import here.
336
+ from polars._plr import _known_timezones
337
+
338
+ chrono_known_tz = set(_known_timezones())
339
+ return st.timezone_keys(allow_prefix=False).filter(
340
+ lambda tz: tz not in {"Factory", "localtime"} and tz in chrono_known_tz
341
+ )
342
+
343
+
344
+ @st.composite
345
+ def _instantiate_dtype(
346
+ draw: DrawFn,
347
+ dtype: PolarsDataType,
348
+ *,
349
+ allowed_dtypes: Collection[PolarsDataType] | None = None,
350
+ excluded_dtypes: Sequence[PolarsDataType] | None = None,
351
+ nesting_level: int = 3,
352
+ allow_time_zones: bool = True,
353
+ ) -> DataType:
354
+ """Take a data type and instantiate it."""
355
+ if not dtype.is_nested():
356
+ if isinstance(dtype, DataType):
357
+ return dtype
358
+
359
+ if allowed_dtypes is None:
360
+ allowed_dtypes = [dtype]
361
+ else:
362
+ same_dtypes = [dt for dt in allowed_dtypes if dt == dtype]
363
+ allowed_dtypes = same_dtypes if same_dtypes else [dtype]
364
+
365
+ return draw(
366
+ _flat_dtypes(
367
+ allowed_dtypes=allowed_dtypes,
368
+ excluded_dtypes=excluded_dtypes,
369
+ allow_time_zones=allow_time_zones,
370
+ )
371
+ )
372
+
373
+ def draw_inner(dtype: PolarsDataType | None) -> DataType:
374
+ if dtype is None:
375
+ return draw(
376
+ dtypes(
377
+ allowed_dtypes=allowed_dtypes,
378
+ excluded_dtypes=excluded_dtypes,
379
+ nesting_level=nesting_level - 1,
380
+ allow_time_zones=allow_time_zones,
381
+ )
382
+ )
383
+ else:
384
+ return draw(
385
+ _instantiate_dtype(
386
+ dtype,
387
+ allowed_dtypes=allowed_dtypes,
388
+ excluded_dtypes=excluded_dtypes,
389
+ nesting_level=nesting_level - 1,
390
+ allow_time_zones=allow_time_zones,
391
+ )
392
+ )
393
+
394
+ if dtype == List:
395
+ inner = draw_inner(getattr(dtype, "inner", None))
396
+ return List(inner)
397
+ elif dtype == Array:
398
+ inner = draw_inner(getattr(dtype, "inner", None))
399
+ size = getattr(
400
+ dtype,
401
+ "size",
402
+ draw(st.integers(min_value=1, max_value=_DEFAULT_ARRAY_WIDTH_LIMIT)),
403
+ )
404
+ return Array(inner, size)
405
+ elif dtype == Struct:
406
+ if isinstance(dtype, Struct):
407
+ fields = [
408
+ Field(
409
+ name=f.name,
410
+ dtype=draw(
411
+ _instantiate_dtype(
412
+ f.dtype,
413
+ allowed_dtypes=allowed_dtypes,
414
+ excluded_dtypes=excluded_dtypes,
415
+ nesting_level=nesting_level - 1,
416
+ allow_time_zones=allow_time_zones,
417
+ )
418
+ ),
419
+ )
420
+ for f in dtype.fields
421
+ ]
422
+ else:
423
+ n_fields = draw(
424
+ st.integers(min_value=1, max_value=_DEFAULT_STRUCT_FIELDS_LIMIT)
425
+ )
426
+ inner_strategy = dtypes(
427
+ allowed_dtypes=allowed_dtypes,
428
+ excluded_dtypes=excluded_dtypes,
429
+ nesting_level=nesting_level - 1,
430
+ allow_time_zones=allow_time_zones,
431
+ )
432
+ fields = [Field(f"f{i}", draw(inner_strategy)) for i in range(n_fields)]
433
+ return Struct(fields)
434
+ else:
435
+ msg = f"unsupported data type: {dtype}"
436
+ raise InvalidArgument(msg)
@@ -0,0 +1,169 @@
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Sequence
4
+ from typing import TYPE_CHECKING, Any
5
+
6
+ import hypothesis.strategies as st
7
+ from hypothesis.errors import InvalidArgument
8
+
9
+ from polars._utils.deprecation import deprecated
10
+ from polars.datatypes import is_polars_dtype
11
+ from polars.testing.parametric.strategies.core import _COL_LIMIT, column
12
+ from polars.testing.parametric.strategies.data import lists
13
+ from polars.testing.parametric.strategies.dtype import _instantiate_dtype, dtypes
14
+
15
+ if TYPE_CHECKING:
16
+ import sys
17
+
18
+ from hypothesis.strategies import SearchStrategy
19
+
20
+ from polars._typing import OneOrMoreDataTypes, PolarsDataType
21
+
22
+ if sys.version_info >= (3, 13):
23
+ from warnings import deprecated
24
+ else:
25
+ from typing_extensions import deprecated # noqa: TC004
26
+
27
+
28
+ @deprecated(
29
+ "`columns` is deprecated; use `column` instead, "
30
+ "in conjunction with a list comprehension."
31
+ )
32
+ def columns(
33
+ cols: int | Sequence[str] | None = None,
34
+ *,
35
+ dtype: OneOrMoreDataTypes | None = None,
36
+ min_cols: int = 0,
37
+ max_cols: int = _COL_LIMIT,
38
+ unique: bool = False,
39
+ ) -> list[column]:
40
+ """
41
+ Define multiple columns for use with the @dataframes strategy.
42
+
43
+ .. deprecated:: 0.20.26
44
+ Use :class:`column` instead, in conjunction with a list comprehension.
45
+
46
+ .. warning::
47
+ This functionality is currently considered **unstable**. It may be
48
+ changed at any point without it being considered a breaking change.
49
+
50
+ Generate a fixed sequence of `column` objects suitable for passing to the
51
+ @dataframes strategy, or using standalone (note that this function is not itself
52
+ a strategy).
53
+
54
+ Notes
55
+ -----
56
+ Additional control is available by creating a sequence of columns explicitly,
57
+ using the `column` class (an especially useful option is to override the default
58
+ data-generating strategy for a given col/dtype).
59
+
60
+ Parameters
61
+ ----------
62
+ cols : {int, [str]}, optional
63
+ integer number of cols to create, or explicit list of column names. if
64
+ omitted a random number of columns (between mincol and max_cols) are
65
+ created.
66
+ dtype : PolarsDataType, optional
67
+ a single dtype for all cols, or list of dtypes (the same length as `cols`).
68
+ if omitted, each generated column is assigned a random dtype.
69
+ min_cols : int, optional
70
+ if not passing an exact size, can set a minimum here (defaults to 0).
71
+ max_cols : int, optional
72
+ if not passing an exact size, can set a maximum value here (defaults to
73
+ MAX_COLS).
74
+ unique : bool, optional
75
+ indicate if the values generated for these columns should be unique
76
+ (per-column).
77
+
78
+ Examples
79
+ --------
80
+ >>> from polars.testing.parametric import columns, dataframes
81
+ >>> from hypothesis import given
82
+ >>> @given(dataframes(columns(["x", "y", "z"], unique=True))) # doctest: +SKIP
83
+ ... def test_unique_xyz(df: pl.DataFrame) -> None:
84
+ ... assert_something(df)
85
+ """
86
+ # create/assign named columns
87
+ if cols is None:
88
+ cols = st.integers(min_value=min_cols, max_value=max_cols).example()
89
+ if isinstance(cols, int):
90
+ names: Sequence[str] = [f"col{n}" for n in range(cols)]
91
+ else:
92
+ names = cols
93
+ n_cols = len(names)
94
+
95
+ if dtype is None:
96
+ dtypes: Sequence[PolarsDataType | None] = [None] * n_cols
97
+ elif is_polars_dtype(dtype):
98
+ dtypes = [dtype] * n_cols
99
+ elif isinstance(dtype, Sequence):
100
+ if (n_dtypes := len(dtype)) != n_cols:
101
+ msg = f"given {n_dtypes} dtypes for {n_cols} names"
102
+ raise InvalidArgument(msg)
103
+ dtypes = dtype
104
+ else:
105
+ msg = f"{dtype!r} is not a valid polars datatype"
106
+ raise InvalidArgument(msg)
107
+
108
+ # init list of named/typed columns
109
+ return [column(name=nm, dtype=tp, unique=unique) for nm, tp in zip(names, dtypes)]
110
+
111
+
112
+ @deprecated("`create_list_strategy` is deprecated; use `lists` instead.")
113
+ def create_list_strategy(
114
+ inner_dtype: PolarsDataType | None = None,
115
+ *,
116
+ select_from: Sequence[Any] | None = None,
117
+ size: int | None = None,
118
+ min_size: int = 0,
119
+ max_size: int | None = None,
120
+ unique: bool = False,
121
+ ) -> SearchStrategy[list[Any]]:
122
+ """
123
+ Create a strategy for generating Polars :class:`List` data.
124
+
125
+ .. deprecated:: 0.20.26
126
+ Use :func:`lists` instead.
127
+
128
+ Parameters
129
+ ----------
130
+ inner_dtype : PolarsDataType
131
+ type of the inner list elements (can also be another List).
132
+ select_from : list, optional
133
+ randomly select the innermost values from this list (otherwise
134
+ the default strategy associated with the innermost dtype is used).
135
+ size : int, optional
136
+ if set, generated lists will be of exactly this size (and
137
+ ignore the min_size/max_size params).
138
+ min_size : int, optional
139
+ set the minimum size of the generated lists (default: 0 if unset).
140
+ max_size : int, optional
141
+ set the maximum size of the generated lists (default: 3 if
142
+ min_size is unset or zero, otherwise 2x min_size).
143
+ unique : bool, optional
144
+ ensure that the generated lists contain unique values.
145
+
146
+ Examples
147
+ --------
148
+ Create a strategy that generates a list of i32 values:
149
+
150
+ >>> from polars.testing.parametric import create_list_strategy
151
+ >>> lst = create_list_strategy(inner_dtype=pl.Int32) # doctest: +SKIP
152
+ >>> lst.example() # doctest: +SKIP
153
+ [-11330, 24030, 116]
154
+ """
155
+ if size is not None:
156
+ min_size = max_size = size
157
+
158
+ if inner_dtype is None:
159
+ inner_dtype = dtypes().example()
160
+ else:
161
+ inner_dtype = _instantiate_dtype(inner_dtype).example()
162
+
163
+ return lists(
164
+ inner_dtype,
165
+ select_from=select_from,
166
+ min_size=min_size,
167
+ max_size=max_size,
168
+ unique=unique,
169
+ )
polars/type_aliases.py ADDED
@@ -0,0 +1,24 @@
1
+ """
2
+ Deprecated module - do not use.
3
+
4
+ Used to contain private type aliases. These are now in the `polars._typing` module.
5
+ """
6
+
7
+ from typing import Any
8
+
9
+ import polars._typing as plt
10
+ from polars._utils.deprecation import issue_deprecation_warning
11
+
12
+
13
+ def __getattr__(name: str) -> Any:
14
+ if name in dir(plt):
15
+ issue_deprecation_warning(
16
+ "the `polars.type_aliases` module was deprecated in version 1.0.0."
17
+ " The type aliases have moved to the `polars._typing` module to explicitly mark them as private."
18
+ " Please define your own type aliases, or temporarily import from the `polars._typing` module."
19
+ " A public `polars.typing` module will be added in the future.",
20
+ )
21
+ return getattr(plt, name)
22
+
23
+ msg = f"module {__name__!r} has no attribute {name!r}"
24
+ raise AttributeError(msg)