Skip to content
This repository was archived by the owner on Feb 13, 2021. It is now read-only.

Commit 30d5fcd

Browse files
author
Thiago C. D'Ávila
authored
Merge pull request #55 from staticdev/Typeguard
Typeguard fixes
2 parents 8bb63d1 + e90fdc7 commit 30d5fcd

2 files changed

Lines changed: 31 additions & 42 deletions

File tree

src/humanizer_portugues/number.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"""Humanizing functions for numbers."""
44
import fractions
55
import re
6-
from typing import Union
6+
from typing import Any
77

88

9-
def ordinal(value: int) -> Union[str, int]:
9+
def ordinal(value: Any) -> Any:
1010
"""Converts an integer to its ordinal as a string.
1111
1212
1 is '1º', 2 is '2º', 3 is '3º', etc.
@@ -17,7 +17,7 @@ def ordinal(value: int) -> Union[str, int]:
1717
value: integer.
1818
1919
Returns:
20-
str: ordinal string.
20+
Any: ordinal string.
2121
"""
2222
try:
2323
value = int(value)
@@ -26,18 +26,18 @@ def ordinal(value: int) -> Union[str, int]:
2626
return "{}{}".format(value, "º")
2727

2828

29-
def int_comma(value: Union[str, int]) -> Union[str, int]:
29+
def int_comma(value: Any) -> Any:
3030
"""Converts an integer to a string containing commas every three digits.
3131
3232
For example, 3000 becomes '3,000' and 45000 becomes '45,000'. To maintain
3333
some compatability with Django's int_comma, this function also accepts
3434
floats.
3535
3636
Args:
37-
value (int): any number.
37+
value: any number.
3838
3939
Returns:
40-
str: formatted number with commas.
40+
Any: formatted number with commas.
4141
"""
4242
try:
4343
if isinstance(value, str):
@@ -69,7 +69,7 @@ def int_comma(value: Union[str, int]) -> Union[str, int]:
6969
)
7070

7171

72-
def int_word(value: int, formatting: str = "%.1f") -> Union[str, int]:
72+
def int_word(value: Any, formatting: str = "%.1f") -> Any:
7373
"""Converts a large integer to a friendly text representation.
7474
7575
Works best for numbers over 1 million.
@@ -82,11 +82,11 @@ def int_word(value: int, formatting: str = "%.1f") -> Union[str, int]:
8282
coaxed into an int.
8383
8484
Args:
85-
value (int): any number.
85+
value: any number.
8686
formatting (str): string formatting pattern. Defaults to "%.1f":str.
8787
8888
Returns:
89-
str: number formatted with scale words.
89+
Any: number formatted with scale words.
9090
"""
9191
try:
9292
value = int(value)
@@ -102,17 +102,17 @@ def int_word(value: int, formatting: str = "%.1f") -> Union[str, int]:
102102
return str(value)
103103

104104

105-
def ap_number(value: int) -> Union[str, int]:
105+
def ap_number(value: Any) -> Any:
106106
"""For numbers 1-9, returns the number spelled out. Otherwise, returns the number.
107107
108108
This follows Associated Press style. This always returns a string
109109
unless the value was not int-able, unlike the Django filter.
110110
111111
Args:
112-
value (int): any number.
112+
value: any number.
113113
114114
Returns:
115-
str: spelled 1-9 numbers or original number.
115+
Any: spelled 1-9 numbers or original number.
116116
"""
117117
try:
118118
value = int(value)
@@ -125,7 +125,7 @@ def ap_number(value: int) -> Union[str, int]:
125125
]
126126

127127

128-
def fractional(value: float) -> Union[str, float]:
128+
def fractional(value: Any) -> Any:
129129
"""Returns a human readable fractional number.
130130
131131
The return can be in the form of fractions and mixed fractions.
@@ -146,10 +146,10 @@ def fractional(value: float) -> Union[str, float]:
146146
This will always return a string.
147147
148148
Args:
149-
value (float): a number.
149+
value: a number.
150150
151151
Returns:
152-
str: human readable number.
152+
Any: human readable number.
153153
"""
154154
try:
155155
number = float(value)

src/humanizer_portugues/time.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import datetime
88
from typing import Any
99
from typing import Tuple
10-
from typing import Union
1110

1211
__all__ = [
1312
"natural_clock",
@@ -168,20 +167,18 @@ def _informal_time(value, hour):
168167
return clock
169168

170169

171-
def natural_clock(
172-
value: datetime.datetime, formal: bool = True
173-
) -> Union[str, datetime.datetime]:
170+
def natural_clock(value: Any, formal: bool = True) -> Any:
174171
"""Returns human-readable time.
175172
176173
Compares time values to present time returns representing readable of time
177174
with the given day period.
178175
179176
Args:
180-
value (datetime.datetime): any datetime.
177+
value (Any): any datetime.
181178
formal (bool): Formal or informal reading. Defaults to True.
182179
183180
Returns:
184-
str: readable time.
181+
Any: readable time or original object.
185182
"""
186183
try:
187184
time = datetime.time(value.hour, value.minute, value.second)
@@ -222,19 +219,17 @@ def abs_timedelta(delta: datetime.timedelta) -> datetime.timedelta:
222219
return delta
223220

224221

225-
def date_and_delta(
226-
value: Union[datetime.datetime, datetime.timedelta, int]
227-
) -> Tuple[Any, Any]:
222+
def date_and_delta(value: Any) -> Tuple[Any, Any]:
228223
"""Returns date and timedelta.
229224
230225
Turn a value into a date and a timedelta which represents how long ago
231226
it was. If that's not possible, return (None, value).
232227
233228
Args:
234-
value (Union[datetime.datetime, datetime.timedelta, int]): date value.
229+
value (Any): date value.
235230
236231
Returns:
237-
[type]: [description]
232+
Tuple[Any, Any]: date and delta or tuple of None, original value.
238233
"""
239234
now = _now()
240235
if isinstance(value, datetime.datetime):
@@ -297,13 +292,11 @@ def _one_year(days, months, use_months):
297292
return "1 ano e %d dias" % days
298293

299294

300-
def more_than_one_year(years):
295+
def more_than_one_year(years: int) -> str:
301296
return "%d anos" % years
302297

303298

304-
def natural_delta(
305-
value: datetime.datetime, use_months: bool = True
306-
) -> Union[str, datetime.datetime]:
299+
def natural_delta(value: Any, use_months: bool = True) -> Any:
307300
"""Returns human-readable time difference.
308301
309302
Given a timedelta or a number of seconds, return a natural
@@ -313,11 +306,11 @@ def natural_delta(
313306
for fuzziness between years.
314307
315308
Args:
316-
value (datetime.datetime): date.
309+
value (Any): date.
317310
use_months (bool): show the number of months. Defaults to True.
318311
319312
Returns:
320-
str: time representation in natural language.
313+
Any: time representation in natural language or original object.
321314
"""
322315
date, delta = date_and_delta(value)
323316
if date is None:
@@ -338,9 +331,7 @@ def natural_delta(
338331
return more_than_one_year(years)
339332

340333

341-
def natural_time(
342-
value: datetime.datetime, future: bool = False, use_months: bool = True
343-
) -> Union[str, datetime.datetime]:
334+
def natural_time(value: Any, future: bool = False, use_months: bool = True) -> Any:
344335
"""Returns human-readable time.
345336
346337
Given a datetime or a number of seconds, return a natural representation
@@ -351,12 +342,12 @@ def natural_time(
351342
unless ``future`` is set to True.
352343
353344
Args:
354-
value (datetime.datetime): time value.
345+
value (Any): time value.
355346
future (bool): if false uses past tense. Defaults to False.
356347
use_months (bool): if true return number of months. Defaults to True.
357348
358349
Returns:
359-
Union[str, datetime.datetime]: time in natural language.
350+
Any: time in natural language or original object.
360351
"""
361352
now = _now()
362353
date, delta = date_and_delta(value)
@@ -375,21 +366,19 @@ def natural_time(
375366
return ago % delta
376367

377368

378-
def natural_day(
379-
value: Union[datetime.datetime, datetime.date], has_year: bool = False
380-
) -> Union[str, datetime.datetime, datetime.date]:
369+
def natural_day(value: Any, has_year: bool = False) -> Any:
381370
"""Returns human-readable day.
382371
383372
For date values that are tomorrow, today or yesterday compared to
384373
present day returns representing string. Otherwise, returns a string
385374
formatted according to ``format``.
386375
387376
Args:
388-
value (Union[datetime.datetime, datetime.date]): a date.
377+
value (Any): a date.
389378
has_year (bool): if year is added. Defaults to False.
390379
391380
Returns:
392-
Union[str, datetime.datetime]: date formatted in natural language.
381+
Any: date formatted in natural language or original object.
393382
"""
394383
try:
395384
date = datetime.date(value.year, value.month, value.day)

0 commit comments

Comments
 (0)