Skip to content

Commit 233b79e

Browse files
chore: explicitly import gitlab.v4.objects/cli
As we only support the v4 Gitlab API, explicitly import gitlab.v4.objects and gitlab.v4.clie instead of dynamically importing it depending on the API version. This has the added benefit of mypy being able to type check the Gitlab __init__() function as currently it will fail if we enable type checking of __init__() it will fail. Also, this also helps by not confusing tools like pyinstaller/cx_freeze with dynamic imports so you don't need hooks for standalone executables. And according to https://docs.gitlab.com/ee/api/, "GraphQL co-exists with the current v4 REST API. If we have a v5 API, this should be a compatibility layer on top of GraphQL."
1 parent 5cc60d5 commit 233b79e

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

gitlab/cli.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import argparse
2121
import functools
22-
import importlib
2322
import re
2423
import sys
2524

@@ -158,12 +157,18 @@ def docs():
158157
sys.exit("Docs parser is only intended for build_sphinx")
159158

160159
parser = _get_base_parser(add_help=False)
161-
cli_module = importlib.import_module("gitlab.v4.cli")
160+
# NOTE: We must delay import of gitlab.v4.cli until now or
161+
# otherwise it will cause circular import errors
162+
import gitlab.v4.cli
162163

163-
return _get_parser(cli_module)
164+
return _get_parser(gitlab.v4.cli)
164165

165166

166167
def main():
168+
# NOTE: We must delay import of gitlab.v4.cli until now or
169+
# otherwise it will cause circular import errors
170+
import gitlab.v4.cli
171+
167172
if "--version" in sys.argv:
168173
print(gitlab.__version__)
169174
sys.exit(0)
@@ -181,10 +186,12 @@ def main():
181186
parser.print_help()
182187
sys.exit(0)
183188
sys.exit(e)
184-
cli_module = importlib.import_module("gitlab.v%s.cli" % config.api_version)
189+
# We only support v4 API at this time
190+
if config.api_version not in ("4",):
191+
raise ModuleNotFoundError(name="gitlab.v%s.cli" % self._api_version)
185192

186193
# Now we build the entire set of subcommands and do the complete parsing
187-
parser = _get_parser(cli_module)
194+
parser = _get_parser(gitlab.v4.cli)
188195
try:
189196
import argcomplete
190197

@@ -229,6 +236,6 @@ def main():
229236
if debug:
230237
gl.enable_debug()
231238

232-
cli_module.run(gl, what, action, args, verbose, output, fields)
239+
gitlab.v4.cli.run(gl, what, action, args, verbose, output, fields)
233240

234241
sys.exit(0)

gitlab/client.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
"""Wrapper for the GitLab API."""
1818

19-
import importlib
2019
import time
2120

2221
import requests
@@ -99,7 +98,14 @@ def __init__(
9998
self.pagination = pagination
10099
self.order_by = order_by
101100

102-
objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
101+
# We only support v4 API at this time
102+
if self._api_version not in ("4",):
103+
raise ModuleNotFoundError(name="gitlab.v%s.objects" % self._api_version)
104+
# NOTE: We must delay import of gitlab.v4.objects until now or
105+
# otherwise it will cause circular import errors
106+
import gitlab.v4.objects
107+
108+
objects = gitlab.v4.objects
103109
self._objects = objects
104110

105111
self.broadcastmessages = objects.BroadcastMessageManager(self)
@@ -147,8 +153,14 @@ def __getstate__(self):
147153

148154
def __setstate__(self, state):
149155
self.__dict__.update(state)
150-
objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
151-
self._objects = objects
156+
# We only support v4 API at this time
157+
if self._api_version not in ("4",):
158+
raise ModuleNotFoundError(name="gitlab.v%s.objects" % self._api_version)
159+
# NOTE: We must delay import of gitlab.v4.objects until now or
160+
# otherwise it will cause circular import errors
161+
import gitlab.v4.objects
162+
163+
self._objects = gitlab.v4.objects
152164

153165
@property
154166
def url(self):

0 commit comments

Comments
 (0)