Skip to content

Commit b52788e

Browse files
waysHCookie
andauthored
fix: adjust due to deprecations and add typing (#271)
## Description - Add types where missing - Fix datetime.utcnow deprecation - Improve string formatting for readability - Remove useless int() Co-authored-by: Harrison Cook <[email protected]>
1 parent cbf3c8e commit b52788e

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/anemoi/utils/config.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,19 @@ def __init__(self, *args, **kwargs):
6565
super().__setitem__(k, self.convert_to_nested_dot_dict(v))
6666

6767
@staticmethod
68-
def convert_to_nested_dot_dict(value):
68+
def convert_to_nested_dot_dict(value: Any) -> Any:
69+
"""Convert nested dicts to DotDict recursively.
70+
71+
Parameters
72+
----------
73+
value : Any
74+
The value to convert
75+
76+
Returns
77+
-------
78+
Any
79+
Converted value with nested dicts as DotDict
80+
"""
6981
if isinstance(value, dict) or is_omegaconf_dict(value):
7082
return DotDict(value)
7183

src/anemoi/utils/humanize.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def when(
337337

338338
if now is None:
339339
if use_utc:
340-
now = datetime.datetime.utcnow()
340+
now = datetime.datetime.now(datetime.UTC)
341341
else:
342342
now = datetime.datetime.now()
343343

@@ -352,7 +352,7 @@ def when(
352352
if diff == 0:
353353
return "right now"
354354

355-
def _(x):
355+
def _(x: str) -> str:
356356
if last == "last":
357357
return f"{x} ago"
358358
else:
@@ -376,46 +376,38 @@ def _(x):
376376
jthen = then.toordinal()
377377

378378
if jnow == jthen:
379-
return "today at %02d:%02d" % (then.hour, then.minute)
379+
return f"today at {then.hour:02d}:{then.minute:02d}"
380380

381381
if jnow == jthen + 1:
382-
return "yesterday at %02d:%02d" % (then.hour, then.minute)
382+
return f"yesterday at {then.hour:02d}:{then.minute:02d}"
383383

384384
if jnow == jthen - 1:
385-
return "tomorrow at %02d:%02d" % (then.hour, then.minute)
385+
return f"tomorrow at {then.hour:02d}:{then.minute:02d}"
386386

387387
if abs(jnow - jthen) <= 7:
388388
if last == "next":
389389
last = "this"
390-
return "{} {}".format(
391-
last,
392-
DOW[then.weekday()],
393-
)
390+
return f"{last} {DOW[then.weekday()]}"
394391

395392
if abs(jnow - jthen) < 32 and now.month == then.month:
396-
return "the %d%s of this month" % (then.day, __(then.day))
393+
return f"the {then.day}{__(then.day)} of this month"
397394

398395
if abs(jnow - jthen) < 64 and now.month == then.month + 1:
399-
return "the %d%s of %s month" % (then.day, __(then.day), last)
396+
return f"the {then.day}{__(then.day)} of {last} month"
400397

401398
if short:
402399
years = int(abs(jnow - jthen) / 365.25 + 0.5)
403400
if years == 1:
404-
return "%s year" % last
401+
return f"{last} year"
405402

406403
if years > 1:
407-
return _("%d years" % (years,))
404+
return _(f"{years} years")
408405

409406
delta = abs(now - then)
410407
if delta.days > 1 and delta.days < 30:
411-
return _("%d days" % (delta.days,))
408+
return _(f"{delta.days} days")
412409

413-
return "on %s %d %s %d" % (
414-
DOW[then.weekday()],
415-
then.day,
416-
MONTH[then.month - 1],
417-
then.year,
418-
)
410+
return f"on {DOW[then.weekday()]} {then.day} {MONTH[then.month - 1]} {then.year}"
419411

420412

421413
def string_distance(s: str, t: str) -> int:
@@ -439,8 +431,8 @@ def string_distance(s: str, t: str) -> int:
439431
n = len(t)
440432
d = np.zeros((m + 1, n + 1), dtype=int)
441433

442-
one = int(1)
443-
zero = int(0)
434+
one = 1
435+
zero = 0
444436

445437
d[:, 0] = np.arange(m + 1)
446438
d[0, :] = np.arange(n + 1)

src/anemoi/utils/text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# https://en.wikipedia.org/wiki/Box-drawing_character
1919

2020

21-
def dotted_line(width=84) -> str:
21+
def dotted_line(width: int = 84) -> str:
2222
"""Return a dotted line using '┈'.
2323
2424
>>> dotted_line(40)
@@ -165,7 +165,7 @@ def boxed(text: str, min_width: int = 80, max_width: int = 80) -> str:
165165

166166
if max_width is not None:
167167

168-
def shorten_line(line, max_width):
168+
def shorten_line(line: list, max_width: int) -> list:
169169
if visual_len(line) > max_width:
170170
while visual_len(line) >= max_width:
171171
line = line[:-1]
@@ -175,7 +175,7 @@ def shorten_line(line, max_width):
175175
width = min(width, max_width)
176176
lines = [shorten_line(line, max_width) for line in lines]
177177

178-
def pad_line(line, width):
178+
def pad_line(line: list, width: int) -> list:
179179
line = line + [" "] * (width - visual_len(line))
180180
return line
181181

0 commit comments

Comments
 (0)