Skip to content

Commit 0895732

Browse files
chore: explicitly import gitlab.v4.objects
As we only support the v4 Gitlab API, explicitly import gitlab.v4.objects 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.
1 parent 5cc60d5 commit 0895732

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

gitlab/client.py

Lines changed: 16 additions & 4 deletions
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)