Skip to content

Commit 7f20d81

Browse files
author
Adrian Immer
committed
fix: write_api TypeError in kwargs
PR #158 introduced a bug writing dataframes. Filtering now the kwargs before forwarding them to lower levels
1 parent 4cbf156 commit 7f20d81

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.testing.pytestEnabled": true,
3+
"python.testing.unittestEnabled": false,
4+
"python.testing.pytestArgs": [
5+
"tests"
6+
]
7+
}

influxdb_client_3/write_client/client/write_api.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
DEFAULT_WRITE_NO_SYNC = False
2828
DEFAULT_WRITE_TIMEOUT = 10_000
2929

30+
# Kwargs consumed during serialization that should not be passed to _post_write
31+
SERIALIZER_KWARGS = {
32+
# DataFrame-specific kwargs
33+
'data_frame_measurement_name',
34+
'data_frame_tag_columns',
35+
'data_frame_timestamp_column',
36+
'data_frame_timestamp_timezone',
37+
# Record-specific kwargs (dict, NamedTuple, dataclass)
38+
'record_measurement_key',
39+
'record_measurement_name',
40+
'record_time_key',
41+
'record_tag_keys',
42+
'record_field_keys',
43+
}
44+
3045
logger = logging.getLogger('influxdb_client_3.write_client.client.write_api')
3146

3247
if _HAS_DATACLASS:
@@ -397,9 +412,12 @@ def write(self, bucket: str, org: str = None,
397412

398413
_async_req = True if self._write_options.write_type == WriteType.asynchronous else False
399414

415+
# Filter out serializer-specific kwargs before passing to _post_write
416+
http_kwargs = {k: v for k, v in kwargs.items() if k not in SERIALIZER_KWARGS}
417+
400418
def write_payload(payload):
401419
final_string = b'\n'.join(payload[1])
402-
return self._post_write(_async_req, bucket, org, final_string, payload[0], no_sync, **kwargs)
420+
return self._post_write(_async_req, bucket, org, final_string, payload[0], no_sync, **http_kwargs)
403421

404422
results = list(map(write_payload, payloads.items()))
405423
if not _async_req:

tests/test_write_local_server.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
from http import HTTPStatus
44

5+
import pandas as pd
56
import pytest
67
from pytest_httpserver import HTTPServer, RequestMatcher
78
from urllib3.exceptions import TimeoutError as urllib3_TimeoutError
@@ -183,3 +184,32 @@ def test_write_with_timeout_arg(self, httpserver: HTTPServer):
183184
),
184185
enable_gzip=True
185186
).write(self.SAMPLE_RECORD, _request_timeout=1)
187+
188+
def test_write_dataframe_does_not_raise_type_error(self, httpserver: HTTPServer):
189+
"""
190+
Regression test: writing a DataFrame should not raise TypeError.
191+
192+
Before the fix, serializer kwargs were passed to post_write(), causing a TypeError.
193+
"""
194+
self.set_response_status(httpserver, 200)
195+
196+
df = pd.DataFrame({
197+
'time': pd.to_datetime(['2024-01-01', '2024-01-02']),
198+
'city': ['London', 'Paris'],
199+
'temperature': [15.0, 18.0]
200+
})
201+
202+
try:
203+
InfluxDBClient3(
204+
host=(httpserver.url_for("/")), org="ORG", database="DB", token="TOKEN",
205+
write_client_options=write_client_options(
206+
write_options=WriteOptions(write_type=WriteType.synchronous)
207+
)
208+
).write_dataframe(
209+
df,
210+
measurement='weather',
211+
timestamp_column='time',
212+
tags=['city']
213+
)
214+
except TypeError as e:
215+
pytest.fail(f"write_dataframe raised TypeError: {e}")

0 commit comments

Comments
 (0)