Skip to content

Commit 80f958c

Browse files
authored
fix: type annotation of unwrap() and datetime parsing (#229)
1 parent 6720e24 commit 80f958c

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

tomlkit/container.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, parsed: bool = False) -> None:
4646
def body(self) -> List[Tuple[Optional[Key], Item]]:
4747
return self._body
4848

49-
def unwrap(self) -> str:
49+
def unwrap(self) -> Dict[str, Any]:
5050
unwrapped = {}
5151
for k, v in self.items():
5252
if k is None:
@@ -66,7 +66,7 @@ def unwrap(self) -> str:
6666
return unwrapped
6767

6868
@property
69-
def value(self) -> Dict[Any, Any]:
69+
def value(self) -> Dict[str, Any]:
7070
d = {}
7171
for k, v in self._body:
7272
if k is None:

tomlkit/items.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,11 @@ def as_string(self) -> str:
503503
"""The TOML representation"""
504504
raise NotImplementedError()
505505

506-
def unwrap(self):
506+
@property
507+
def value(self) -> Any:
508+
return self
509+
510+
def unwrap(self) -> Any:
507511
"""Returns as pure python object (ppo)"""
508512
raise NotImplementedError()
509513

@@ -1036,7 +1040,7 @@ def __init__(
10361040

10371041
self._raw = raw
10381042

1039-
def unwrap(self) -> datetime:
1043+
def unwrap(self) -> time:
10401044
(hour, minute, second, microsecond, tzinfo, _, _) = self._getstate()
10411045
return time(hour, minute, second, microsecond, tzinfo)
10421046

@@ -1157,7 +1161,7 @@ def _group_values(self, value: List[Item]) -> List[_ArrayItemGroup]:
11571161
groups.append(this_group)
11581162
return [group for group in groups if group]
11591163

1160-
def unwrap(self) -> str:
1164+
def unwrap(self) -> List[Any]:
11611165
unwrapped = []
11621166
for v in self:
11631167
if isinstance(v, Item):
@@ -1427,7 +1431,7 @@ def __init__(self, value: "container.Container", trivia: Trivia):
14271431
if k is not None:
14281432
dict.__setitem__(self, k.key, v)
14291433

1430-
def unwrap(self):
1434+
def unwrap(self) -> Dict[str, Any]:
14311435
unwrapped = {}
14321436
for k, v in self.items():
14331437
if isinstance(k, Key):
@@ -1822,7 +1826,7 @@ def __init__(
18221826
for table in body:
18231827
self.append(table)
18241828

1825-
def unwrap(self) -> str:
1829+
def unwrap(self) -> List[Dict[str, Any]]:
18261830
unwrapped = []
18271831
for t in self._body:
18281832
if isinstance(t, Item):
@@ -1922,7 +1926,7 @@ class Null(Item):
19221926
def __init__(self) -> None:
19231927
pass
19241928

1925-
def unwrap(self) -> str:
1929+
def unwrap(self) -> None:
19261930
return None
19271931

19281932
@property

tomlkit/parser.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import re
23
import string
34

@@ -470,6 +471,7 @@ def _parse_value(self) -> Item:
470471
# datetime
471472
try:
472473
dt = parse_rfc3339(raw)
474+
assert isinstance(dt, datetime.datetime)
473475
return DateTime(
474476
dt.year,
475477
dt.month,
@@ -488,6 +490,7 @@ def _parse_value(self) -> Item:
488490
if m.group(1):
489491
try:
490492
dt = parse_rfc3339(raw)
493+
assert isinstance(dt, datetime.date)
491494
date = Date(dt.year, dt.month, dt.day, trivia, raw)
492495
self.mark()
493496
while self._current not in "\t\n\r#,]}" and self.inc():
@@ -499,6 +502,7 @@ def _parse_value(self) -> Item:
499502
return date
500503

501504
dt = parse_rfc3339(raw + time_raw)
505+
assert isinstance(dt, datetime.datetime)
502506
return DateTime(
503507
dt.year,
504508
dt.month,
@@ -517,6 +521,7 @@ def _parse_value(self) -> Item:
517521
if m.group(5):
518522
try:
519523
t = parse_rfc3339(raw)
524+
assert isinstance(t, datetime.time)
520525
return Time(
521526
t.hour,
522527
t.minute,
@@ -678,10 +683,10 @@ def _parse_number(self, raw: str, trivia: Trivia) -> Optional[Item]:
678683
or sign
679684
and raw.startswith(".")
680685
):
681-
return
686+
return None
682687

683688
if raw.startswith(("0o", "0x", "0b")) and sign:
684-
return
689+
return None
685690

686691
digits = "[0-9]"
687692
base = 10
@@ -699,22 +704,22 @@ def _parse_number(self, raw: str, trivia: Trivia) -> Optional[Item]:
699704
clean = re.sub(f"(?i)(?<={digits})_(?={digits})", "", raw).lower()
700705

701706
if "_" in clean:
702-
return
707+
return None
703708

704709
if (
705710
clean.endswith(".")
706711
or not clean.startswith("0x")
707712
and clean.split("e", 1)[0].endswith(".")
708713
):
709-
return
714+
return None
710715

711716
try:
712717
return Integer(int(sign + clean, base), trivia, sign + raw)
713718
except ValueError:
714719
try:
715720
return Float(float(sign + clean), trivia, sign + raw)
716721
except ValueError:
717-
return
722+
return None
718723

719724
def _parse_literal_string(self) -> String:
720725
with self._state:

0 commit comments

Comments
 (0)