88import datetime as dt
99import math
1010import warnings
11- from enum import Enum , EnumMeta
11+ from enum import Enum
1212from functools import total_ordering
1313
1414from .i18n import _gettext as _
2525
2626
2727@total_ordering
28- class _Unit (Enum ):
28+ class Unit (Enum ):
2929 MICROSECONDS = 0
3030 MILLISECONDS = 1
3131 SECONDS = 2
@@ -41,44 +41,6 @@ def __lt__(self, other):
4141 return NotImplemented
4242
4343
44- class _UnitMeta (EnumMeta ):
45- """Metaclass for an enum that emits deprecation warnings when accessed."""
46-
47- def __getattribute__ (self , name ):
48- # Temporarily comment out to avoid warning during 'import humanize'
49- # warnings.warn(
50- # "`Unit` has been deprecated. "
51- # "The enum is still available as the private member `_Unit`.",
52- # DeprecationWarning,
53- # )
54- return EnumMeta .__getattribute__ (_Unit , name )
55-
56- def __getitem__ (cls , name ):
57- warnings .warn (
58- "`Unit` has been deprecated. "
59- "The enum is still available as the private member `_Unit`." ,
60- DeprecationWarning ,
61- )
62- return _Unit .__getitem__ (name )
63-
64- def __call__ (
65- cls , value , names = None , * , module = None , qualname = None , type = None , start = 1
66- ):
67- warnings .warn (
68- "`Unit` has been deprecated. "
69- "The enum is still available as the private member `_Unit`." ,
70- DeprecationWarning ,
71- )
72- return _Unit .__call__ (
73- value , names , module = module , qualname = qualname , type = type , start = start
74- )
75-
76-
77- class Unit (Enum , metaclass = _UnitMeta ):
78- # Temporary alias for _Unit to allow backwards-compatible usage.
79- pass
80-
81-
8244def _now ():
8345 return dt .datetime .now ()
8446
@@ -194,8 +156,8 @@ def naturaldelta(
194156
195157 assert naturaldelta(later - now) == "30 minutes"
196158 """
197- tmp = _Unit [minimum_unit .upper ()]
198- if tmp not in (_Unit .SECONDS , _Unit .MILLISECONDS , _Unit .MICROSECONDS ):
159+ tmp = Unit [minimum_unit .upper ()]
160+ if tmp not in (Unit .SECONDS , Unit .MILLISECONDS , Unit .MICROSECONDS ):
199161 raise ValueError (f"Minimum unit '{ minimum_unit } ' not supported" )
200162 minimum_unit = tmp
201163
@@ -218,13 +180,13 @@ def naturaldelta(
218180
219181 if not years and days < 1 :
220182 if seconds == 0 :
221- if minimum_unit == _Unit .MICROSECONDS and delta .microseconds < 1000 :
183+ if minimum_unit == Unit .MICROSECONDS and delta .microseconds < 1000 :
222184 return (
223185 _ngettext ("%d microsecond" , "%d microseconds" , delta .microseconds )
224186 % delta .microseconds
225187 )
226- elif minimum_unit == _Unit .MILLISECONDS or (
227- minimum_unit == _Unit .MICROSECONDS
188+ elif minimum_unit == Unit .MILLISECONDS or (
189+ minimum_unit == Unit .MICROSECONDS
228190 and 1000 <= delta .microseconds < 1_000_000
229191 ):
230192 milliseconds = delta .microseconds / 1000
@@ -369,20 +331,20 @@ def _quotient_and_remainder(value, divisor, unit, minimum_unit, suppress):
369331 represent the remainder because it would require a unit smaller than the
370332 `minimum_unit`.
371333
372- >>> from humanize.time import _quotient_and_remainder, _Unit
373- >>> _quotient_and_remainder(36, 24, _Unit .DAYS, _Unit .DAYS, [])
334+ >>> from humanize.time import _quotient_and_remainder, Unit
335+ >>> _quotient_and_remainder(36, 24, Unit .DAYS, Unit .DAYS, [])
374336 (1.5, 0)
375337
376338 If unit is in `suppress`, the quotient will be zero and the remainder will be the
377339 initial value. The idea is that if we cannot use `unit`, we are forced to use a
378340 lower unit so we cannot do the division.
379341
380- >>> _quotient_and_remainder(36, 24, _Unit .DAYS, _Unit .HOURS, [_Unit .DAYS])
342+ >>> _quotient_and_remainder(36, 24, Unit .DAYS, Unit .HOURS, [Unit .DAYS])
381343 (0, 36)
382344
383345 In other case return quotient and remainder as `divmod` would do it.
384346
385- >>> _quotient_and_remainder(36, 24, _Unit .DAYS, _Unit .HOURS, [])
347+ >>> _quotient_and_remainder(36, 24, Unit .DAYS, Unit .HOURS, [])
386348 (1, 12)
387349
388350 """
@@ -401,20 +363,20 @@ def _carry(value1, value2, ratio, unit, min_unit, suppress):
401363 (carry to right). The idea is that if we cannot represent `value1` we need to
402364 represent it in a lower unit.
403365
404- >>> from humanize.time import _carry, _Unit
405- >>> _carry(2, 6, 24, _Unit .DAYS, _Unit .SECONDS, [_Unit .DAYS])
366+ >>> from humanize.time import _carry, Unit
367+ >>> _carry(2, 6, 24, Unit .DAYS, Unit .SECONDS, [Unit .DAYS])
406368 (0, 54)
407369
408370 If the unit is the minimum unit, `value2` is divided by `ratio` and added to
409371 `value1` (carry to left). We assume that `value2` has a lower unit so we need to
410372 carry it to `value1`.
411373
412- >>> _carry(2, 6, 24, _Unit .DAYS, _Unit .DAYS, [])
374+ >>> _carry(2, 6, 24, Unit .DAYS, Unit .DAYS, [])
413375 (2.25, 0)
414376
415377 Otherwise, just return the same input:
416378
417- >>> _carry(2, 6, 24, _Unit .DAYS, _Unit .SECONDS, [])
379+ >>> _carry(2, 6, 24, Unit .DAYS, Unit .SECONDS, [])
418380 (2, 6)
419381 """
420382 if unit == min_unit :
@@ -430,21 +392,21 @@ def _suitable_minimum_unit(min_unit, suppress):
430392
431393 If not suppressed, return the same unit:
432394
433- >>> from humanize.time import _suitable_minimum_unit, _Unit
434- >>> _suitable_minimum_unit(_Unit .HOURS, []).name
395+ >>> from humanize.time import _suitable_minimum_unit, Unit
396+ >>> _suitable_minimum_unit(Unit .HOURS, []).name
435397 'HOURS'
436398
437399 But if suppressed, find a unit greather than the original one that is not
438400 suppressed:
439401
440- >>> _suitable_minimum_unit(_Unit .HOURS, [_Unit .HOURS]).name
402+ >>> _suitable_minimum_unit(Unit .HOURS, [Unit .HOURS]).name
441403 'DAYS'
442404
443- >>> _suitable_minimum_unit(_Unit .HOURS, [_Unit .HOURS, _Unit .DAYS]).name
405+ >>> _suitable_minimum_unit(Unit .HOURS, [Unit .HOURS, Unit .DAYS]).name
444406 'MONTHS'
445407 """
446408 if min_unit in suppress :
447- for unit in _Unit :
409+ for unit in Unit :
448410 if unit > min_unit and unit not in suppress :
449411 return unit
450412
@@ -458,12 +420,12 @@ def _suitable_minimum_unit(min_unit, suppress):
458420def _suppress_lower_units (min_unit , suppress ):
459421 """Extend suppressed units (if any) with all units lower than the minimum unit.
460422
461- >>> from humanize.time import _suppress_lower_units, _Unit
462- >>> [x.name for x in sorted(_suppress_lower_units(_Unit .SECONDS, [_Unit .DAYS]))]
423+ >>> from humanize.time import _suppress_lower_units, Unit
424+ >>> [x.name for x in sorted(_suppress_lower_units(Unit .SECONDS, [Unit .DAYS]))]
463425 ['MICROSECONDS', 'MILLISECONDS', 'DAYS']
464426 """
465427 suppress = set (suppress )
466- for u in _Unit :
428+ for u in Unit :
467429 if u == min_unit :
468430 break
469431 suppress .add (u )
@@ -542,11 +504,11 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f") ->
542504 if date is None :
543505 return value
544506
545- suppress = [_Unit [s .upper ()] for s in suppress ]
507+ suppress = [Unit [s .upper ()] for s in suppress ]
546508
547509 # Find a suitable minimum unit (it can be greater the one that the
548510 # user gave us if it is suppressed).
549- min_unit = _Unit [minimum_unit .upper ()]
511+ min_unit = Unit [minimum_unit .upper ()]
550512 min_unit = _suitable_minimum_unit (min_unit , suppress )
551513 del minimum_unit
552514
@@ -560,7 +522,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f") ->
560522 usecs = delta .microseconds
561523
562524 MICROSECONDS , MILLISECONDS , SECONDS , MINUTES , HOURS , DAYS , MONTHS , YEARS = list (
563- _Unit
525+ Unit
564526 )
565527
566528 # Given DAYS compute YEARS and the remainder of DAYS as follows:
@@ -609,7 +571,7 @@ def precisedelta(value, minimum_unit="seconds", suppress=(), format="%0.2f") ->
609571 ]
610572
611573 texts = []
612- for unit , fmt in zip (reversed (_Unit ), fmts ):
574+ for unit , fmt in zip (reversed (Unit ), fmts ):
613575 singular_txt , plural_txt , value = fmt
614576 if value > 0 or (not texts and unit == min_unit ):
615577 fmt_txt = _ngettext (singular_txt , plural_txt , value )
0 commit comments