Skip to content

Commit 68f6b42

Browse files
authored
Fix datetime deprecation warnings (#815)
* Fix datetime deprecation warnings * Remove ndjson to appease mypy * Add more typing to kafka generator and time * Appease black * Update changelog * Silence mypy for confluent_kafka import * Add more typing for confluent_kafka
1 parent 762d7be commit 68f6b42

File tree

7 files changed

+40
-44
lines changed

7 files changed

+40
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Fixed assign_callback error in confluentkafka input
2222
- Fixed error logging in ` _get_configuration`, which caused the github checks to fail
2323
- Fix domain resolver errors for invalid domains
24+
- Fixed deprecation warnings caused by datetime when using Python >= 3.12
2425

2526
## 16.1.0
2627
### Deprecations

logprep/generator/kafka/document_loader.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""For loading documents from Kafka or from file and preparing them for sending"""
22

33
import json
4-
from datetime import datetime
4+
from datetime import datetime, UTC
55
from logging import Logger
6-
7-
import ndjson
6+
from typing import Any
87

98
from logprep.generator.kafka.configuration import Configuration
109
from logprep.generator.kafka.kafka_connector import KafkaConsumer
@@ -20,13 +19,13 @@ def __init__(self, config: Configuration, logger: Logger):
2019
self._timeout = config.kafka.consumer.timeout
2120
self._kafka_consumer = KafkaConsumer(config.kafka)
2221

23-
def _get_from_file(self) -> list:
24-
with self._source_file.open("r", encoding="utf-8") as input_docs:
25-
input_docs = ndjson.load(input_docs)
22+
def _get_from_file(self) -> list[dict[Any, Any]]:
23+
with self._source_file.open("r", encoding="utf-8") as docs_file:
24+
input_docs: list[dict[Any, Any]] = [json.loads(doc) for doc in docs_file.readlines()]
2625
self._logger.info(f"Loaded {len(input_docs)} documents")
2726
return input_docs
2827

29-
def _get_from_kafka(self) -> list:
28+
def _get_from_kafka(self) -> list[dict[Any, Any]]:
3029
documents = []
3130
cnt_invalid = 0
3231
for _ in range(self._source_count):
@@ -47,32 +46,32 @@ def _get_from_kafka(self) -> list:
4746
self._logger.info(f"Fetched {len(documents)} documents")
4847
return documents
4948

50-
def get_documents(self) -> list:
49+
def get_documents(self) -> list[str]:
5150
"""Get documents from Kafka format them so a unique value can be added easily"""
52-
docs = self._get_raw_documents()
51+
docs_json = self._get_raw_documents()
5352

54-
self._prepare_json_docs(docs)
55-
docs = ndjson.dumps(docs).splitlines()
53+
self._prepare_json_docs(docs_json)
54+
docs = [json.dumps(doc) for doc in docs_json]
5655
self._prepare_string_addition_of_additional_fields(docs)
5756
return docs
5857

59-
def _get_raw_documents(self):
58+
def _get_raw_documents(self) -> list[dict[Any, Any]]:
6059
return self._get_from_file() if self._source_file else self._get_from_kafka()
6160

6261
@staticmethod
63-
def _prepare_json_docs(docs: list, index_name="load-tester"):
62+
def _prepare_json_docs(docs: list[dict[Any, Any]], index_name: str = "load-tester") -> None:
6463
for doc in docs:
6564
doc["_index"] = index_name
6665
doc["tags"] = ["load-tester"]
67-
doc["@timestamp"] = f"{datetime.utcnow().isoformat()}Z"
66+
doc["@timestamp"] = datetime.now(UTC).isoformat()
6867

6968
@staticmethod
70-
def _prepare_string_addition_of_additional_fields(docs: list):
69+
def _prepare_string_addition_of_additional_fields(docs: list[str]) -> None:
7170
"""Prepare document for unique value that will be added on sending"""
7271
for idx, doc in enumerate(docs):
7372
if '"' in doc and doc.endswith("}"):
7473
docs[idx] = f'{doc[:-1]}, "load-tester-unique": "'
7574

76-
def shut_down(self):
75+
def shut_down(self) -> None:
7776
"""Shut down the Kafka consumer gracefully"""
7877
self._kafka_consumer.shut_down()

logprep/generator/kafka/kafka_connector.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""For retrieval and insertion of data from and into Kafka."""
22

33
import json
4-
from typing import Optional
4+
from typing import Any
55

6-
from confluent_kafka import Consumer, Producer
6+
from confluent_kafka import Consumer, Producer # type: ignore[import-untyped]
77

88
from logprep.generator.kafka.configuration import Kafka
99

@@ -35,15 +35,15 @@ def __init__(self, config: Kafka):
3535

3636
self._producer = Producer(self._config)
3737

38-
def store(self, document: str):
38+
def store(self, document: str) -> None:
3939
"""Write document into Kafka"""
4040
try:
4141
self._producer.produce(self._producer_topic, value=document)
4242
self._producer.poll(0)
4343
except BufferError:
4444
self._producer.flush(timeout=self._flush_timeout)
4545

46-
def shut_down(self):
46+
def shut_down(self) -> None:
4747
"""Gracefully close Kafka producer"""
4848
if self._producer is not None:
4949
self._producer.flush(self._flush_timeout)
@@ -76,7 +76,7 @@ def __init__(self, config: Kafka):
7676
self._consumer = Consumer(self._config)
7777
self._consumer.subscribe([config.consumer.topic])
7878

79-
def get(self, timeout: float) -> Optional[str]:
79+
def get(self, timeout: float) -> Any:
8080
"""Get document from Kafka"""
8181
record = self._consumer.poll(timeout=timeout)
8282
if not record:
@@ -86,7 +86,7 @@ def get(self, timeout: float) -> Optional[str]:
8686
raise record.error()
8787
return json.loads(record.value().decode("utf-8"))
8888

89-
def shut_down(self):
89+
def shut_down(self) -> None:
9090
"""Gracefully close Kafka consumer"""
9191
if self._consumer is not None:
9292
self._consumer.close()

logprep/util/time.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
"""logprep time helpers module"""
22

3-
from datetime import datetime, tzinfo
3+
from datetime import datetime, tzinfo, UTC
44
from typing import Union
5-
from zoneinfo import ZoneInfo
65

76
from logprep.abc.exceptions import LogprepException
87

9-
UTC = ZoneInfo("UTC")
10-
118

129
class TimeParserException(LogprepException):
1310
"""exception class for time parsing"""
@@ -54,7 +51,7 @@ def from_timestamp(cls, timestamp: Union[int, float]) -> datetime:
5451
datetime
5552
datetime object
5653
"""
57-
time_object = datetime.utcfromtimestamp(timestamp)
54+
time_object = datetime.fromtimestamp(timestamp, tz=UTC)
5855
time_object = cls._set_utc_if_timezone_is_missing(time_object)
5956
return time_object
6057

@@ -108,7 +105,7 @@ def from_format(cls, source: str, format_str: str, set_missing_utc: bool = True)
108105
raise TimeParserException(str(error)) from error
109106

110107
@classmethod
111-
def _set_utc_if_timezone_is_missing(cls, time_object):
108+
def _set_utc_if_timezone_is_missing(cls, time_object: datetime) -> datetime:
112109
if time_object.tzinfo is None:
113110
time_object = time_object.replace(tzinfo=UTC)
114111
return time_object

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ dependencies = [
8484
"msgspec",
8585
"boto3",
8686
"pydantic",
87-
"ndjson",
8887
"click",
8988
"pandas",
9089
"tabulate",

tests/unit/generator/kafka/test_document_loader.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_get_documents_source_empty_dict(self):
119119
for document in documents:
120120
assert re.match(
121121
r'{"_index": "load-tester", "tags": \["load-tester"\], '
122-
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z", '
122+
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+00:00", '
123123
r'"load-tester-unique": "',
124124
document,
125125
)
@@ -133,7 +133,7 @@ def test_get_documents_source_multiple_times(self):
133133
for document in documents:
134134
assert re.match(
135135
r'{"bar": "2", "_index": "load-tester", "tags": \["load-tester"\], '
136-
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z", '
136+
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+00:00", '
137137
r'"load-tester-unique": "',
138138
document,
139139
)
@@ -143,7 +143,7 @@ def test_get_documents_source_multiple_times(self):
143143
for document in documents:
144144
assert re.match(
145145
r'{"foo": "1", "_index": "load-tester", "tags": \["load-tester"\], '
146-
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z", '
146+
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+00:00", '
147147
r'"load-tester-unique": "',
148148
document,
149149
)
@@ -159,7 +159,7 @@ def test_get_documents_multiple_source(self):
159159
for document in documents:
160160
assert re.match(
161161
r'{"foo": "bar", "_index": "load-tester", "tags": \["load-tester"\], '
162-
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z", '
162+
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+00:00", '
163163
r'"load-tester-unique": "',
164164
document,
165165
)
@@ -175,7 +175,7 @@ def test_get_documents_more_source_than_available(self):
175175
for document in documents:
176176
assert re.match(
177177
r'{"foo": "bar", "_index": "load-tester", "tags": \["load-tester"\], '
178-
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z", '
178+
r'"@timestamp": "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+\+00:00", '
179179
r'"load-tester-unique": "',
180180
document,
181181
)

tests/unit/util/test_time.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pylint: disable=missing-docstring
22
# pylint: disable=protected-access
33
# pylint: disable=too-many-arguments
4-
from datetime import datetime
4+
from datetime import datetime, UTC
55
from zoneinfo import ZoneInfo
66

77
import pytest
@@ -60,21 +60,21 @@ def test_from_format_returns(self, source, format_str, expected):
6060
for attribute, value in expected.items():
6161
assert getattr(timestamp, attribute) == value
6262

63-
@pytest.mark.parametrize("timezone", [None, ZoneInfo("UTC"), ZoneInfo("Europe/Berlin")])
63+
@pytest.mark.parametrize("timezone", [None, UTC, ZoneInfo("Europe/Berlin")])
6464
def test_has_utc_if_timezone_was_set(self, timezone):
6565
datetime_time = datetime.now(timezone)
6666
time_parser_time = TimeParser.now(timezone)
6767
assert time_parser_time.second == pytest.approx(datetime_time.second, abs=1)
6868
if timezone is None:
69-
assert time_parser_time.tzinfo == ZoneInfo("UTC")
69+
assert time_parser_time.tzinfo == UTC
7070
else:
7171
assert time_parser_time.tzinfo == timezone
7272

7373
def test_set_utc_if_timezone_is_missing_sets_timezone(self):
7474
datetime_time = datetime.now()
7575
assert datetime_time.tzinfo is None
7676
time_parser_time = TimeParser._set_utc_if_timezone_is_missing(datetime_time)
77-
assert time_parser_time.tzinfo is ZoneInfo("UTC")
77+
assert time_parser_time.tzinfo is UTC
7878
assert time_parser_time.second == pytest.approx(datetime_time.second, abs=1)
7979

8080
@pytest.mark.parametrize(
@@ -90,28 +90,28 @@ def test_set_utc_if_timezone_is_missing_sets_timezone(self):
9090
(
9191
"2021-03-13T11:23:13Z",
9292
"ISO8601",
93-
ZoneInfo("UTC"),
93+
UTC,
9494
"UTC",
9595
{"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13},
9696
),
9797
(
9898
"2021-03-13T11:23:13+01",
9999
"ISO8601",
100-
ZoneInfo("UTC"),
100+
UTC,
101101
"UTC+01:00",
102102
{"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13},
103103
),
104104
(
105105
"2021-03-13T11:23:13",
106106
"ISO8601",
107-
ZoneInfo("UTC"),
107+
UTC,
108108
"UTC",
109109
{"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13},
110110
),
111111
(
112112
"2021 03 13 - 11:23:13",
113113
"%Y %m %d - %H:%M:%S",
114-
ZoneInfo("UTC"),
114+
UTC,
115115
"UTC",
116116
{"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13},
117117
),
@@ -132,7 +132,7 @@ def test_set_utc_if_timezone_is_missing_sets_timezone(self):
132132
(
133133
"03 13 - 11:23:13",
134134
"%m %d - %H:%M:%S",
135-
ZoneInfo("UTC"),
135+
UTC,
136136
"UTC",
137137
{
138138
"year": datetime.now().year,
@@ -159,7 +159,7 @@ def test_parse_datetime_replaces_timezone_if_it_does_not_exist_in_string(
159159
(
160160
"1615634593",
161161
"UNIX",
162-
ZoneInfo("UTC"),
162+
UTC,
163163
"UTC",
164164
{"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13},
165165
),

0 commit comments

Comments
 (0)