summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn L. Villalovos <debian.org@sodarock.com>2021-02-20 17:41:22 -0800
committerJohn L. Villalovos <debian.org@sodarock.com>2021-02-21 17:50:24 -0800
commit233b79ed442aac66faf9eb4b0087ea126d6dffc5 (patch)
tree05870d8084efc6e6ade18eb4911098b5057d8b8f
parent5cc60d5a8ac129652611d3dc12b350b5ca7262b9 (diff)
downloadgitlab-233b79ed442aac66faf9eb4b0087ea126d6dffc5.tar.gz
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."
-rw-r--r--gitlab/cli.py19
-rw-r--r--gitlab/client.py20
2 files changed, 29 insertions, 10 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py
index ff98a4f..d858a74 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -19,7 +19,6 @@
import argparse
import functools
-import importlib
import re
import sys
@@ -158,12 +157,18 @@ def docs():
sys.exit("Docs parser is only intended for build_sphinx")
parser = _get_base_parser(add_help=False)
- cli_module = importlib.import_module("gitlab.v4.cli")
+ # NOTE: We must delay import of gitlab.v4.cli until now or
+ # otherwise it will cause circular import errors
+ import gitlab.v4.cli
- return _get_parser(cli_module)
+ return _get_parser(gitlab.v4.cli)
def main():
+ # NOTE: We must delay import of gitlab.v4.cli until now or
+ # otherwise it will cause circular import errors
+ import gitlab.v4.cli
+
if "--version" in sys.argv:
print(gitlab.__version__)
sys.exit(0)
@@ -181,10 +186,12 @@ def main():
parser.print_help()
sys.exit(0)
sys.exit(e)
- cli_module = importlib.import_module("gitlab.v%s.cli" % config.api_version)
+ # We only support v4 API at this time
+ if config.api_version not in ("4",):
+ raise ModuleNotFoundError(name="gitlab.v%s.cli" % self._api_version)
# Now we build the entire set of subcommands and do the complete parsing
- parser = _get_parser(cli_module)
+ parser = _get_parser(gitlab.v4.cli)
try:
import argcomplete
@@ -229,6 +236,6 @@ def main():
if debug:
gl.enable_debug()
- cli_module.run(gl, what, action, args, verbose, output, fields)
+ gitlab.v4.cli.run(gl, what, action, args, verbose, output, fields)
sys.exit(0)
diff --git a/gitlab/client.py b/gitlab/client.py
index dbfc834..6d0401d 100644
--- a/gitlab/client.py
+++ b/gitlab/client.py
@@ -16,7 +16,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Wrapper for the GitLab API."""
-import importlib
import time
import requests
@@ -99,7 +98,14 @@ class Gitlab(object):
self.pagination = pagination
self.order_by = order_by
- objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
+ # We only support v4 API at this time
+ if self._api_version not in ("4",):
+ raise ModuleNotFoundError(name="gitlab.v%s.objects" % self._api_version)
+ # NOTE: We must delay import of gitlab.v4.objects until now or
+ # otherwise it will cause circular import errors
+ import gitlab.v4.objects
+
+ objects = gitlab.v4.objects
self._objects = objects
self.broadcastmessages = objects.BroadcastMessageManager(self)
@@ -147,8 +153,14 @@ class Gitlab(object):
def __setstate__(self, state):
self.__dict__.update(state)
- objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
- self._objects = objects
+ # We only support v4 API at this time
+ if self._api_version not in ("4",):
+ raise ModuleNotFoundError(name="gitlab.v%s.objects" % self._api_version)
+ # NOTE: We must delay import of gitlab.v4.objects until now or
+ # otherwise it will cause circular import errors
+ import gitlab.v4.objects
+
+ self._objects = gitlab.v4.objects
@property
def url(self):