-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathconftest.py
299 lines (241 loc) · 8.63 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from __future__ import annotations
import gc
import os
import random
import string
import sys
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, cast
import numpy as np
import pytest
import polars as pl
from polars.testing.parametric import load_profile
if TYPE_CHECKING:
from collections.abc import Generator
from types import ModuleType
from typing import Any
FixtureRequest = Any
load_profile(
profile=os.environ.get("POLARS_HYPOTHESIS_PROFILE", "fast"), # type: ignore[arg-type]
)
# Data type groups
SIGNED_INTEGER_DTYPES = [pl.Int8(), pl.Int16(), pl.Int32(), pl.Int64(), pl.Int128()]
UNSIGNED_INTEGER_DTYPES = [pl.UInt8(), pl.UInt16(), pl.UInt32(), pl.UInt64()]
INTEGER_DTYPES = SIGNED_INTEGER_DTYPES + UNSIGNED_INTEGER_DTYPES
FLOAT_DTYPES = [pl.Float32(), pl.Float64()]
NUMERIC_DTYPES = INTEGER_DTYPES + FLOAT_DTYPES
DATETIME_DTYPES = [pl.Datetime("ms"), pl.Datetime("us"), pl.Datetime("ns")]
DURATION_DTYPES = [pl.Duration("ms"), pl.Duration("us"), pl.Duration("ns")]
TEMPORAL_DTYPES = [*DATETIME_DTYPES, *DURATION_DTYPES, pl.Date(), pl.Time()]
NESTED_DTYPES = [pl.List, pl.Struct, pl.Array]
@pytest.fixture
def partition_limit() -> int:
"""The limit at which Polars will start partitioning in debug builds."""
return 15
@pytest.fixture
def df() -> pl.DataFrame:
df = pl.DataFrame(
{
"bools": [False, True, False],
"bools_nulls": [None, True, False],
"int": [1, 2, 3],
"int_nulls": [1, None, 3],
"floats": [1.0, 2.0, 3.0],
"floats_nulls": [1.0, None, 3.0],
"strings": ["foo", "bar", "ham"],
"strings_nulls": ["foo", None, "ham"],
"date": [1324, 123, 1234],
"datetime": [13241324, 12341256, 12341234],
"time": [13241324, 12341256, 12341234],
"list_str": [["a", "b", None], ["a"], []],
"list_bool": [[True, False, None], [None], []],
"list_int": [[1, None, 3], [None], []],
"list_flt": [[1.0, None, 3.0], [None], []],
}
)
return df.with_columns(
pl.col("date").cast(pl.Date),
pl.col("datetime").cast(pl.Datetime),
pl.col("strings").cast(pl.Categorical).alias("cat"),
pl.col("strings").cast(pl.Enum(["foo", "ham", "bar"])).alias("enum"),
pl.col("time").cast(pl.Time),
)
@pytest.fixture
def df_no_lists(df: pl.DataFrame) -> pl.DataFrame:
return df.select(
pl.all().exclude(["list_str", "list_int", "list_bool", "list_int", "list_flt"])
)
@pytest.fixture
def fruits_cars() -> pl.DataFrame:
return pl.DataFrame(
{
"A": [1, 2, 3, 4, 5],
"fruits": ["banana", "banana", "apple", "apple", "banana"],
"B": [5, 4, 3, 2, 1],
"cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
},
schema_overrides={"A": pl.Int64, "B": pl.Int64},
)
@pytest.fixture
def str_ints_df() -> pl.DataFrame:
n = 1000
strs = pl.Series("strs", random.choices(string.ascii_lowercase, k=n))
strs = pl.select(
pl.when(strs == "a")
.then(pl.lit(""))
.when(strs == "b")
.then(None)
.otherwise(strs)
.alias("strs")
).to_series()
vals = pl.Series("vals", np.random.rand(n))
return pl.DataFrame([vals, strs])
ISO8601_FORMATS_DATETIME = []
for T in ["T", " "]:
for hms in (
[
f"{T}%H:%M:%S",
f"{T}%H%M%S",
f"{T}%H:%M",
f"{T}%H%M",
]
+ [f"{T}%H:%M:%S.{fraction}" for fraction in ["%9f", "%6f", "%3f"]]
+ [f"{T}%H%M%S.{fraction}" for fraction in ["%9f", "%6f", "%3f"]]
+ [""]
):
for date_sep in ("/", "-"):
fmt = f"%Y{date_sep}%m{date_sep}%d{hms}"
ISO8601_FORMATS_DATETIME.append(fmt)
@pytest.fixture(params=ISO8601_FORMATS_DATETIME)
def iso8601_format_datetime(request: pytest.FixtureRequest) -> list[str]:
return cast(list[str], request.param)
ISO8601_TZ_AWARE_FORMATS_DATETIME = []
for T in ["T", " "]:
for hms in (
[
f"{T}%H:%M:%S",
f"{T}%H%M%S",
f"{T}%H:%M",
f"{T}%H%M",
]
+ [f"{T}%H:%M:%S.{fraction}" for fraction in ["%9f", "%6f", "%3f"]]
+ [f"{T}%H%M%S.{fraction}" for fraction in ["%9f", "%6f", "%3f"]]
):
for date_sep in ("/", "-"):
fmt = f"%Y{date_sep}%m{date_sep}%d{hms}%#z"
ISO8601_TZ_AWARE_FORMATS_DATETIME.append(fmt)
@pytest.fixture(params=ISO8601_TZ_AWARE_FORMATS_DATETIME)
def iso8601_tz_aware_format_datetime(request: pytest.FixtureRequest) -> list[str]:
return cast(list[str], request.param)
ISO8601_FORMATS_DATE = []
for date_sep in ("/", "-"):
fmt = f"%Y{date_sep}%m{date_sep}%d"
ISO8601_FORMATS_DATE.append(fmt)
@pytest.fixture(params=ISO8601_FORMATS_DATE)
def iso8601_format_date(request: pytest.FixtureRequest) -> list[str]:
return cast(list[str], request.param)
class MemoryUsage:
"""
Provide an API for measuring peak memory usage.
Memory from PyArrow is not tracked at the moment.
"""
def reset_tracking(self) -> None:
"""Reset tracking to zero."""
# gc.collect()
# tracemalloc.stop()
# tracemalloc.start()
# assert self.get_peak() < 100_000
def get_current(self) -> int:
"""
Return currently allocated memory, in bytes.
This only tracks allocations since this object was created or
``reset_tracking()`` was called, whichever is later.
"""
return 0
# tracemalloc.get_traced_memory()[0]
def get_peak(self) -> int:
"""
Return peak allocated memory, in bytes.
This returns peak allocations since this object was created or
``reset_tracking()`` was called, whichever is later.
"""
return 0
# tracemalloc.get_traced_memory()[1]
# The bizarre syntax is from
# https://github.com/pytest-dev/pytest/issues/1368#issuecomment-2344450259 - we
# need to mark any test using this fixture as slow because we have a sleep
# added to work around a CPython bug, see the end of the function.
@pytest.fixture(params=[pytest.param(0, marks=pytest.mark.slow)])
def memory_usage_without_pyarrow() -> Generator[MemoryUsage, Any, Any]:
"""
Provide an API for measuring peak memory usage.
Not thread-safe: there should only be one instance of MemoryUsage at any
given time.
Memory usage from PyArrow is not tracked.
"""
if not pl.polars._debug: # type: ignore[attr-defined]
pytest.skip("Memory usage only available in debug/dev builds.")
if os.getenv("POLARS_FORCE_ASYNC", "0") == "1":
pytest.skip("Hangs when combined with async glob")
if sys.platform == "win32":
# abi3 wheels don't have the tracemalloc C APIs, which breaks linking
# on Windows.
pytest.skip("Windows not supported at the moment.")
gc.collect()
try:
yield MemoryUsage()
finally:
gc.collect()
# gc.collect()
# tracemalloc.start()
# try:
# yield MemoryUsage()
# finally:
# # Workaround for https://github.com/python/cpython/issues/128679
# time.sleep(1)
# gc.collect()
#
# tracemalloc.stop()
@pytest.fixture(params=[True, False])
def test_global_and_local(
request: FixtureRequest,
) -> Generator[Any, Any, Any]:
"""
Setup fixture which runs each test with and without global string cache.
Usage: @pytest.mark.usefixtures("test_global_and_local")
"""
use_global = request.param
if use_global:
with pl.StringCache():
# Pre-fill some global items to ensure physical repr isn't 0..n.
pl.Series(["eapioejf", "2m4lmv", "3v3v9dlf"], dtype=pl.Categorical)
yield
else:
yield
@contextmanager
def mock_module_import(
name: str, module: ModuleType, *, replace_if_exists: bool = False
) -> Generator[None, None, None]:
"""
Mock an optional module import for the duration of a context.
Parameters
----------
name
The name of the module to mock.
module
A ModuleType instance representing the mocked module.
replace_if_exists
Whether to replace the module if it already exists in `sys.modules` (defaults to
False, meaning that if the module is already imported, it will not be replaced).
"""
if (original := sys.modules.get(name, None)) is not None and not replace_if_exists:
yield
else:
sys.modules[name] = module
try:
yield
finally:
if original is not None:
sys.modules[name] = original
else:
del sys.modules[name]