Skip to content

Commit b2e8b6a

Browse files
committed
Reduce unnecessary, expensive imports in typing.py and words.py
1 parent 5b7fd55 commit b2e8b6a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

domdf_python_tools/typing.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
# stdlib
4242
import os
4343
import pathlib
44-
import typing
4544
from decimal import Decimal
46-
from json import JSONDecoder, JSONEncoder
47-
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Type, Union
45+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Type, TypeVar, Union
4846

4947
# 3rd party
5048
from typing_extensions import Protocol, runtime_checkable
@@ -53,6 +51,9 @@
5351
import domdf_python_tools
5452

5553
if TYPE_CHECKING or domdf_python_tools.__docs: # pragma: no cover
54+
# stdlib
55+
from json import JSONDecoder, JSONEncoder
56+
5657
# 3rd party
5758
from pandas import DataFrame, Series
5859

@@ -63,7 +64,7 @@
6364
JSONEncoder.__module__ = "json"
6465

6566
#: .. versionadded:: 1.0.0
66-
FrameOrSeries = typing.TypeVar("FrameOrSeries", "Series", "DataFrame")
67+
FrameOrSeries = TypeVar("FrameOrSeries", "Series", "DataFrame")
6768

6869
__all__ = [
6970
"PathLike",
@@ -92,7 +93,7 @@
9293
.. seealso:: :py:obj:`domdf_python_tools.typing.PathType`
9394
"""
9495

95-
PathType = typing.TypeVar("PathType", str, pathlib.Path, os.PathLike)
96+
PathType = TypeVar("PathType", str, pathlib.Path, os.PathLike)
9697
"""
9798
Type variable for objects that represent filesystem paths.
9899
@@ -138,7 +139,7 @@ def dumps(
138139
ensure_ascii: bool = ...,
139140
check_circular: bool = ...,
140141
allow_nan: bool = ...,
141-
cls: Optional[Type[JSONEncoder]] = ...,
142+
cls: Optional[Type["JSONEncoder"]] = ...,
142143
indent: Union[None, int, str] = ...,
143144
separators: Optional[Tuple[str, str]] = ...,
144145
default: Optional[Callable[[Any], Any]] = ...,
@@ -165,7 +166,7 @@ def dumps(
165166
def loads(
166167
s: Union[str, bytes],
167168
*,
168-
cls: Optional[Type[JSONDecoder]] = ...,
169+
cls: Optional[Type["JSONDecoder"]] = ...,
169170
object_hook: Optional[Callable[[Dict[Any, Any]], Any]] = ...,
170171
parse_float: Optional[Callable[[str], Any]] = ...,
171172
parse_int: Optional[Callable[[str], Any]] = ...,

domdf_python_tools/words.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
# stdlib
4040
import functools
41-
import platform
4241
import random
4342
import re
4443
from gettext import ngettext
@@ -48,7 +47,7 @@
4847

4948
# this package
5049
import domdf_python_tools
51-
from domdf_python_tools.compat import PYPY, importlib_resources
50+
from domdf_python_tools.compat import PYPY
5251
from domdf_python_tools.doctools import prettify_docstrings
5352

5453
__all__ = [
@@ -115,6 +114,9 @@ def get_words_list(min_length: int = 0, max_length: int = -1) -> List[str]:
115114
:return: The list of words meeting the above specifiers.
116115
""" # noqa D400
117116

117+
# this package
118+
from domdf_python_tools.compat import importlib_resources
119+
118120
words: str = importlib_resources.read_text("domdf_python_tools", "google-10000-english-no-swears.txt")
119121
words_list: List[str] = words.splitlines()
120122

0 commit comments

Comments
 (0)