Skip to content

Commit de281d6

Browse files
authored
Set module metadata on imported names
1 parent b292ca0 commit de281d6

File tree

4 files changed

+90
-24
lines changed

4 files changed

+90
-24
lines changed

elasticsearch/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import re
2222
import warnings
2323

24+
from ._utils import fixup_module_metadata
2425
from ._version import __versionstr__
2526

2627
_version_groups = re.search(r"^(\d+)\.(\d+)\.(\d+)", __versionstr__).groups() # type: ignore
@@ -74,3 +75,6 @@
7475
"UnsupportedProductError",
7576
"ElasticsearchWarning",
7677
]
78+
79+
fixup_module_metadata(__name__, globals())
80+
del fixup_module_metadata

elasticsearch/_utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import re
19+
from typing import Any, Dict
20+
21+
22+
def fixup_module_metadata(module_name: str, namespace: Dict[str, Any]) -> None:
23+
# Yoinked from python-trio/outcome, thanks Nathaniel! License: MIT
24+
def fix_one(obj: Any) -> None:
25+
mod = getattr(obj, "__module__", None)
26+
if mod is not None and re.match(r"^elasticsearch[0-9]*\.", mod) is not None:
27+
obj.__module__ = module_name
28+
if isinstance(obj, type):
29+
for attr_value in obj.__dict__.values():
30+
fix_one(attr_value)
31+
32+
for objname in namespace["__all__"]:
33+
obj = namespace[objname]
34+
fix_one(obj)

elasticsearch/client.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
from ._sync.client.transform import TransformClient as TransformClient # noqa: F401
6262
from ._sync.client.watcher import WatcherClient as WatcherClient # noqa: F401
6363
from ._sync.client.xpack import XPackClient as XPackClient # noqa: F401
64+
from ._utils import fixup_module_metadata
6465

6566
# This file exists for backwards compatibility.
6667
warnings.warn(
@@ -69,3 +70,43 @@
6970
category=DeprecationWarning,
7071
stacklevel=2,
7172
)
73+
74+
__all__ = [
75+
"AsyncSearchClient",
76+
"AutoscalingClient",
77+
"CatClient",
78+
"CcrClient",
79+
"ClusterClient",
80+
"DanglingIndicesClient",
81+
"Elasticsearch",
82+
"EnrichClient",
83+
"EqlClient",
84+
"FeaturesClient",
85+
"FleetClient",
86+
"GraphClient",
87+
"IlmClient",
88+
"IndicesClient",
89+
"IngestClient",
90+
"LicenseClient",
91+
"LogstashClient",
92+
"MigrationClient",
93+
"MlClient",
94+
"MonitoringClient",
95+
"NodesClient",
96+
"RollupClient",
97+
"SearchableSnapshotsClient",
98+
"SecurityClient",
99+
"ShutdownClient",
100+
"SlmClient",
101+
"SnapshotClient",
102+
"SqlClient",
103+
"SslClient",
104+
"TasksClient",
105+
"TextStructureClient",
106+
"TransformClient",
107+
"WatcherClient",
108+
"XPackClient",
109+
]
110+
111+
fixup_module_metadata(__name__, globals())
112+
del fixup_module_metadata

elasticsearch/helpers/__init__.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from .actions import (
19-
_chunk_actions,
20-
_process_bulk_chunk,
21-
bulk,
22-
expand_action,
23-
parallel_bulk,
24-
reindex,
25-
scan,
26-
streaming_bulk,
27-
)
18+
from .._async.helpers import async_bulk, async_reindex, async_scan, async_streaming_bulk
19+
from .._utils import fixup_module_metadata
20+
from .actions import _chunk_actions # noqa: F401
21+
from .actions import _process_bulk_chunk # noqa: F401
22+
from .actions import bulk, expand_action, parallel_bulk, reindex, scan, streaming_bulk
2823
from .errors import BulkIndexError, ScanError
2924

3025
__all__ = [
@@ -36,19 +31,11 @@
3631
"parallel_bulk",
3732
"scan",
3833
"reindex",
39-
"_chunk_actions",
40-
"_process_bulk_chunk",
34+
"async_scan",
35+
"async_bulk",
36+
"async_reindex",
37+
"async_streaming_bulk",
4138
]
4239

43-
44-
try:
45-
from .._async.helpers import (
46-
async_bulk,
47-
async_reindex,
48-
async_scan,
49-
async_streaming_bulk,
50-
)
51-
52-
__all__ += ["async_scan", "async_bulk", "async_reindex", "async_streaming_bulk"]
53-
except ImportError: # pragma: nocover
54-
pass
40+
fixup_module_metadata(__name__, globals())
41+
del fixup_module_metadata

0 commit comments

Comments
 (0)