Skip to content

Commit 0acc69a

Browse files
Unify _to_date converter across arrow and polars modules
Moved the flexible _to_date function (handling str, datetime, and date) to the base converter module and updated arrow/polars converters to import and reuse it, eliminating duplicate implementations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a2d9c20 commit 0acc69a

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

pyathena/arrow/converter.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
from __future__ import annotations
33

44
import logging
5-
from builtins import isinstance
65
from copy import deepcopy
7-
from datetime import date, datetime
8-
from typing import Any, Callable, Dict, Optional, Type, Union
6+
from typing import Any, Callable, Dict, Optional, Type
97

108
from pyathena.converter import (
119
Converter,
1210
_to_binary,
11+
_to_date,
1312
_to_decimal,
1413
_to_default,
1514
_to_json,
@@ -19,14 +18,6 @@
1918
_logger = logging.getLogger(__name__) # type: ignore
2019

2120

22-
def _to_date(value: Optional[Union[str, datetime]]) -> Optional[date]:
23-
if value is None:
24-
return None
25-
if isinstance(value, datetime):
26-
return value.date()
27-
return datetime.strptime(value, "%Y-%m-%d").date()
28-
29-
3021
_DEFAULT_ARROW_CONVERTERS: Dict[str, Callable[[Optional[str]], Optional[Any]]] = {
3122
"date": _to_date,
3223
"time": _to_time,

pyathena/converter.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from copy import deepcopy
99
from datetime import date, datetime, time
1010
from decimal import Decimal
11-
from typing import Any, Callable, Dict, List, Optional, Type
11+
from typing import Any, Callable, Dict, List, Optional, Type, Union
1212

1313
from dateutil.tz import gettz
1414

@@ -17,10 +17,14 @@
1717
_logger = logging.getLogger(__name__) # type: ignore
1818

1919

20-
def _to_date(varchar_value: Optional[str]) -> Optional[date]:
21-
if varchar_value is None:
20+
def _to_date(value: Optional[Union[str, datetime, date]]) -> Optional[date]:
21+
if value is None:
2222
return None
23-
return datetime.strptime(varchar_value, "%Y-%m-%d").date()
23+
if isinstance(value, datetime):
24+
return value.date()
25+
if isinstance(value, date):
26+
return value
27+
return datetime.strptime(value, "%Y-%m-%d").date()
2428

2529

2630
def _to_datetime(varchar_value: Optional[str]) -> Optional[datetime]:

pyathena/polars/converter.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
import logging
55
from copy import deepcopy
6-
from datetime import date, datetime
7-
from typing import Any, Callable, Dict, Optional, Union
6+
from typing import Any, Callable, Dict, Optional
87

98
from pyathena.converter import (
109
Converter,
1110
_to_binary,
11+
_to_date,
1212
_to_default,
1313
_to_json,
1414
_to_time,
@@ -17,16 +17,6 @@
1717
_logger = logging.getLogger(__name__)
1818

1919

20-
def _to_date(value: Optional[Union[str, datetime, date]]) -> Optional[date]:
21-
if value is None:
22-
return None
23-
if isinstance(value, datetime):
24-
return value.date()
25-
if isinstance(value, date):
26-
return value
27-
return datetime.strptime(value, "%Y-%m-%d").date()
28-
29-
3020
_DEFAULT_POLARS_CONVERTERS: Dict[str, Callable[[Optional[str]], Optional[Any]]] = {
3121
"date": _to_date,
3222
"time": _to_time,

0 commit comments

Comments
 (0)