Skip to content

Commit 9e0c073

Browse files
committed
Defer typing import for faster overall import
1 parent b2562da commit 9e0c073

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

src/humanize/i18n.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import gettext as gettext_module
66
from threading import local
7-
from typing import TYPE_CHECKING
87

8+
TYPE_CHECKING = False
99
if TYPE_CHECKING:
1010
import os
1111
import pathlib

src/humanize/lists.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from __future__ import annotations
44

5-
from typing import Any
5+
TYPE_CHECKING = False
6+
if TYPE_CHECKING:
7+
from typing import Any
68

79
__all__ = ["natural_list"]
810

src/humanize/number.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44

55
import math
66
import re
7-
import sys
8-
from typing import TYPE_CHECKING
97

108
from .i18n import _gettext as _
119
from .i18n import _ngettext, decimal_separator, thousands_separator
1210
from .i18n import _ngettext_noop as NS_
1311
from .i18n import _pgettext as P_
1412

13+
TYPE_CHECKING = False
1514
if TYPE_CHECKING:
15+
import sys
16+
1617
if sys.version_info >= (3, 10):
1718
from typing import TypeAlias
1819
else:
1920
from typing_extensions import TypeAlias
2021

21-
# This type can be better defined by typing.SupportsInt, typing.SupportsFloat
22-
# but that's a Python 3.8 only typing option.
23-
NumberOrString: TypeAlias = "float | str"
22+
# This type can be better defined by typing.SupportsFloat
23+
# but that's a Python 3.8 only typing option.
24+
NumberOrString: TypeAlias = float | str
2425

2526

2627
def _format_not_finite(value: float) -> str:

src/humanize/time.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
import collections.abc
99
import datetime as dt
1010
import math
11-
import typing
1211
from enum import Enum
1312
from functools import total_ordering
14-
from typing import Any
1513

1614
from .i18n import _gettext as _
1715
from .i18n import _ngettext
1816
from .number import intcomma
1917

18+
TYPE_CHECKING = False
19+
if TYPE_CHECKING:
20+
from collections.abc import Iterable
21+
from typing import Any
22+
2023
__all__ = [
2124
"naturaldate",
2225
"naturalday",
@@ -37,7 +40,7 @@ class Unit(Enum):
3740
MONTHS = 6
3841
YEARS = 7
3942

40-
def __lt__(self, other: typing.Any) -> typing.Any:
43+
def __lt__(self, other: Any) -> Any:
4144
if self.__class__ is other.__class__:
4245
return self.value < other.value
4346
return NotImplemented
@@ -62,9 +65,7 @@ def _abs_timedelta(delta: dt.timedelta) -> dt.timedelta:
6265
return delta
6366

6467

65-
def _date_and_delta(
66-
value: typing.Any, *, now: dt.datetime | None = None
67-
) -> tuple[typing.Any, typing.Any]:
68+
def _date_and_delta(value: Any, *, now: dt.datetime | None = None) -> tuple[Any, Any]:
6869
"""Turn a value into a date and a timedelta which represents how long ago it was.
6970
7071
If that's not possible, return `(None, value)`.
@@ -368,7 +369,7 @@ def _carry(
368369
ratio: float,
369370
unit: Unit,
370371
min_unit: Unit,
371-
suppress: typing.Iterable[Unit],
372+
suppress: Iterable[Unit],
372373
) -> tuple[float, float]:
373374
"""Return a tuple with two values.
374375
@@ -401,7 +402,7 @@ def _carry(
401402
return value1, value2
402403

403404

404-
def _suitable_minimum_unit(min_unit: Unit, suppress: typing.Iterable[Unit]) -> Unit:
405+
def _suitable_minimum_unit(min_unit: Unit, suppress: Iterable[Unit]) -> Unit:
405406
"""Return a minimum unit suitable that is not suppressed.
406407
407408
If not suppressed, return the same unit:
@@ -430,7 +431,7 @@ def _suitable_minimum_unit(min_unit: Unit, suppress: typing.Iterable[Unit]) -> U
430431
return min_unit
431432

432433

433-
def _suppress_lower_units(min_unit: Unit, suppress: typing.Iterable[Unit]) -> set[Unit]:
434+
def _suppress_lower_units(min_unit: Unit, suppress: Iterable[Unit]) -> set[Unit]:
434435
"""Extend suppressed units (if any) with all units lower than the minimum unit.
435436
436437
>>> from humanize.time import _suppress_lower_units, Unit
@@ -449,7 +450,7 @@ def _suppress_lower_units(min_unit: Unit, suppress: typing.Iterable[Unit]) -> se
449450
def precisedelta(
450451
value: dt.timedelta | int | None,
451452
minimum_unit: str = "seconds",
452-
suppress: typing.Iterable[str] = (),
453+
suppress: Iterable[str] = (),
453454
format: str = "%0.2f",
454455
) -> str:
455456
"""Return a precise representation of a timedelta.

0 commit comments

Comments
 (0)