Skip to content

Commit

Permalink
fix: type annotation of unwrap() and datetime parsing (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol authored Aug 13, 2022
1 parent 6720e24 commit 80f958c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, parsed: bool = False) -> None:
def body(self) -> List[Tuple[Optional[Key], Item]]:
return self._body

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

@property
def value(self) -> Dict[Any, Any]:
def value(self) -> Dict[str, Any]:
d = {}
for k, v in self._body:
if k is None:
Expand Down
16 changes: 10 additions & 6 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,11 @@ def as_string(self) -> str:
"""The TOML representation"""
raise NotImplementedError()

def unwrap(self):
@property
def value(self) -> Any:
return self

def unwrap(self) -> Any:
"""Returns as pure python object (ppo)"""
raise NotImplementedError()

Expand Down Expand Up @@ -1036,7 +1040,7 @@ def __init__(

self._raw = raw

def unwrap(self) -> datetime:
def unwrap(self) -> time:
(hour, minute, second, microsecond, tzinfo, _, _) = self._getstate()
return time(hour, minute, second, microsecond, tzinfo)

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

def unwrap(self) -> str:
def unwrap(self) -> List[Any]:
unwrapped = []
for v in self:
if isinstance(v, Item):
Expand Down Expand Up @@ -1427,7 +1431,7 @@ def __init__(self, value: "container.Container", trivia: Trivia):
if k is not None:
dict.__setitem__(self, k.key, v)

def unwrap(self):
def unwrap(self) -> Dict[str, Any]:
unwrapped = {}
for k, v in self.items():
if isinstance(k, Key):
Expand Down Expand Up @@ -1822,7 +1826,7 @@ def __init__(
for table in body:
self.append(table)

def unwrap(self) -> str:
def unwrap(self) -> List[Dict[str, Any]]:
unwrapped = []
for t in self._body:
if isinstance(t, Item):
Expand Down Expand Up @@ -1922,7 +1926,7 @@ class Null(Item):
def __init__(self) -> None:
pass

def unwrap(self) -> str:
def unwrap(self) -> None:
return None

@property
Expand Down
15 changes: 10 additions & 5 deletions tomlkit/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import re
import string

Expand Down Expand Up @@ -470,6 +471,7 @@ def _parse_value(self) -> Item:
# datetime
try:
dt = parse_rfc3339(raw)
assert isinstance(dt, datetime.datetime)
return DateTime(
dt.year,
dt.month,
Expand All @@ -488,6 +490,7 @@ def _parse_value(self) -> Item:
if m.group(1):
try:
dt = parse_rfc3339(raw)
assert isinstance(dt, datetime.date)
date = Date(dt.year, dt.month, dt.day, trivia, raw)
self.mark()
while self._current not in "\t\n\r#,]}" and self.inc():
Expand All @@ -499,6 +502,7 @@ def _parse_value(self) -> Item:
return date

dt = parse_rfc3339(raw + time_raw)
assert isinstance(dt, datetime.datetime)
return DateTime(
dt.year,
dt.month,
Expand All @@ -517,6 +521,7 @@ def _parse_value(self) -> Item:
if m.group(5):
try:
t = parse_rfc3339(raw)
assert isinstance(t, datetime.time)
return Time(
t.hour,
t.minute,
Expand Down Expand Up @@ -678,10 +683,10 @@ def _parse_number(self, raw: str, trivia: Trivia) -> Optional[Item]:
or sign
and raw.startswith(".")
):
return
return None

if raw.startswith(("0o", "0x", "0b")) and sign:
return
return None

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

if "_" in clean:
return
return None

if (
clean.endswith(".")
or not clean.startswith("0x")
and clean.split("e", 1)[0].endswith(".")
):
return
return None

try:
return Integer(int(sign + clean, base), trivia, sign + raw)
except ValueError:
try:
return Float(float(sign + clean), trivia, sign + raw)
except ValueError:
return
return None

def _parse_literal_string(self) -> String:
with self._state:
Expand Down

0 comments on commit 80f958c

Please sign in to comment.