Skip to content

Commit 064d375

Browse files
fix: Improve OpenSearch import errors (#2939)
1 parent 8de2695 commit 064d375

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

awswrangler/_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"pyodbc": "sqlserver",
106106
"gremlin_python": "gremlin",
107107
"opensearchpy": "opensearch",
108+
"jsonpath_ng": "opensearch",
108109
"oracledb": "oracle",
109110
}
110111

awswrangler/opensearch/_read.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
from __future__ import annotations
55

6-
from typing import Any, Collection, Mapping
6+
from typing import TYPE_CHECKING, Any, Collection, Mapping
77

88
import awswrangler.pandas as pd
99
from awswrangler import _utils, exceptions
1010
from awswrangler.opensearch._utils import _get_distribution, _is_serverless
1111

12-
opensearchpy = _utils.import_optional_dependency("opensearchpy")
12+
if TYPE_CHECKING:
13+
try:
14+
import opensearchpy
15+
except ImportError:
16+
pass
17+
else:
18+
opensearchpy = _utils.import_optional_dependency("opensearchpy")
1319

1420

1521
def _resolve_fields(row: Mapping[str, Any]) -> Mapping[str, Any]:

awswrangler/opensearch/_write.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import ast
77
import json
88
import logging
9-
from typing import Any, Generator, Iterable, Mapping, cast
9+
from typing import TYPE_CHECKING, Any, Generator, Iterable, Mapping, cast
1010

1111
import boto3
1212
import numpy as np
@@ -17,11 +17,31 @@
1717
from awswrangler._utils import parse_path
1818
from awswrangler.opensearch._utils import _get_distribution, _get_version_major, _is_serverless
1919

20-
progressbar = _utils.import_optional_dependency("progressbar")
21-
opensearchpy = _utils.import_optional_dependency("opensearchpy")
22-
if opensearchpy:
23-
from jsonpath_ng import parse
24-
from jsonpath_ng.exceptions import JsonPathParserError
20+
if TYPE_CHECKING:
21+
try:
22+
import jsonpath_ng
23+
except ImportError:
24+
pass
25+
else:
26+
jsonpath_ng = _utils.import_optional_dependency("jsonpath_ng")
27+
28+
29+
if TYPE_CHECKING:
30+
try:
31+
import opensearchpy
32+
except ImportError:
33+
pass
34+
else:
35+
opensearchpy = _utils.import_optional_dependency("opensearchpy")
36+
37+
if TYPE_CHECKING:
38+
try:
39+
import progressbar
40+
except ImportError:
41+
pass
42+
else:
43+
progressbar = _utils.import_optional_dependency("progressbar")
44+
2545

2646
_logger: logging.Logger = logging.getLogger(__name__)
2747

@@ -95,9 +115,12 @@ def _file_line_generator(path: str, is_json: bool = False) -> Generator[Any, Non
95115
yield line.strip()
96116

97117

118+
@_utils.check_optional_dependency(jsonpath_ng, "jsonpath_ng")
98119
def _get_documents_w_json_path(documents: list[Mapping[str, Any]], json_path: str) -> list[Any]:
120+
from jsonpath_ng.exceptions import JsonPathParserError
121+
99122
try:
100-
jsonpath_expression = parse(json_path)
123+
jsonpath_expression = jsonpath_ng.parse(json_path)
101124
except JsonPathParserError as e:
102125
_logger.error("invalid json_path: %s", json_path)
103126
raise e

0 commit comments

Comments
 (0)