Skip to content

Commit e4a1f6e

Browse files
nejchJohnVillalovos
authored andcommitted
refactor(const): remove deprecated global constant import
BREAKING CHANGE: Constants defined in `gitlab.const` can no longer be imported globally from `gitlab`. Import them from `gitlab.const` instead.
1 parent 4abcd17 commit e4a1f6e

File tree

5 files changed

+115
-70
lines changed

5 files changed

+115
-70
lines changed

gitlab/__init__.py

-21
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
"""Wrapper for the GitLab API."""
1818

1919
import warnings
20-
from typing import Any
2120

2221
import gitlab.config # noqa: F401
23-
from gitlab import utils as _utils
2422
from gitlab._version import ( # noqa: F401
2523
__author__,
2624
__copyright__,
@@ -35,24 +33,6 @@
3533
warnings.filterwarnings("default", category=DeprecationWarning, module="^gitlab")
3634

3735

38-
# NOTE(jlvillal): We are deprecating access to the gitlab.const values which
39-
# were previously imported into this namespace by the
40-
# 'from gitlab.const import *' statement.
41-
def __getattr__(name: str) -> Any:
42-
# Deprecate direct access to constants without namespace
43-
if name in gitlab.const._DEPRECATED:
44-
_utils.warn(
45-
message=(
46-
f"\nDirect access to constants as 'gitlab.{name}' is deprecated and "
47-
f"will be removed in a future major python-gitlab release. Please "
48-
f"see the usage of constants in the 'gitlab.const' module instead."
49-
),
50-
category=DeprecationWarning,
51-
)
52-
return getattr(gitlab.const, name)
53-
raise AttributeError(f"module {__name__} has no attribute {name}")
54-
55-
5636
__all__ = [
5737
"__author__",
5838
"__copyright__",
@@ -63,5 +43,4 @@ def __getattr__(name: str) -> Any:
6343
"Gitlab",
6444
"GitlabList",
6545
]
66-
__all__.extend(gitlab.const._DEPRECATED)
6746
__all__.extend(gitlab.exceptions.__all__)

gitlab/const.py

+29-37
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,6 @@
22

33
from gitlab._version import __title__, __version__
44

5-
# NOTE(jlvillal): '_DEPRECATED' only affects users accessing constants via the
6-
# top-level gitlab.* namespace. See 'gitlab/__init__.py:__getattr__()' for the
7-
# consumer of '_DEPRECATED' For example 'x = gitlab.NO_ACCESS'. We want users
8-
# to instead use constants by doing code like: gitlab.const.NO_ACCESS.
9-
_DEPRECATED = [
10-
"ADMIN_ACCESS",
11-
"DEFAULT_URL",
12-
"DEVELOPER_ACCESS",
13-
"GUEST_ACCESS",
14-
"MAINTAINER_ACCESS",
15-
"MINIMAL_ACCESS",
16-
"NO_ACCESS",
17-
"NOTIFICATION_LEVEL_CUSTOM",
18-
"NOTIFICATION_LEVEL_DISABLED",
19-
"NOTIFICATION_LEVEL_GLOBAL",
20-
"NOTIFICATION_LEVEL_MENTION",
21-
"NOTIFICATION_LEVEL_PARTICIPATING",
22-
"NOTIFICATION_LEVEL_WATCH",
23-
"OWNER_ACCESS",
24-
"REPORTER_ACCESS",
25-
"SEARCH_SCOPE_BLOBS",
26-
"SEARCH_SCOPE_COMMITS",
27-
"SEARCH_SCOPE_GLOBAL_SNIPPET_TITLES",
28-
"SEARCH_SCOPE_ISSUES",
29-
"SEARCH_SCOPE_MERGE_REQUESTS",
30-
"SEARCH_SCOPE_MILESTONES",
31-
"SEARCH_SCOPE_PROJECT_NOTES",
32-
"SEARCH_SCOPE_PROJECTS",
33-
"SEARCH_SCOPE_USERS",
34-
"SEARCH_SCOPE_WIKI_BLOBS",
35-
"USER_AGENT",
36-
"VISIBILITY_INTERNAL",
37-
"VISIBILITY_PRIVATE",
38-
"VISIBILITY_PUBLIC",
39-
]
40-
415

426
class GitlabEnum(str, Enum):
437
"""An enum mixed in with str to make it JSON-serializable."""
@@ -138,5 +102,33 @@ class SearchScope(GitlabEnum):
138102
"Visibility",
139103
"NotificationLevel",
140104
"SearchScope",
105+
"ADMIN_ACCESS",
106+
"DEFAULT_URL",
107+
"DEVELOPER_ACCESS",
108+
"GUEST_ACCESS",
109+
"MAINTAINER_ACCESS",
110+
"MINIMAL_ACCESS",
111+
"NO_ACCESS",
112+
"NOTIFICATION_LEVEL_CUSTOM",
113+
"NOTIFICATION_LEVEL_DISABLED",
114+
"NOTIFICATION_LEVEL_GLOBAL",
115+
"NOTIFICATION_LEVEL_MENTION",
116+
"NOTIFICATION_LEVEL_PARTICIPATING",
117+
"NOTIFICATION_LEVEL_WATCH",
118+
"OWNER_ACCESS",
119+
"REPORTER_ACCESS",
120+
"SEARCH_SCOPE_BLOBS",
121+
"SEARCH_SCOPE_COMMITS",
122+
"SEARCH_SCOPE_GLOBAL_SNIPPET_TITLES",
123+
"SEARCH_SCOPE_ISSUES",
124+
"SEARCH_SCOPE_MERGE_REQUESTS",
125+
"SEARCH_SCOPE_MILESTONES",
126+
"SEARCH_SCOPE_PROJECT_NOTES",
127+
"SEARCH_SCOPE_PROJECTS",
128+
"SEARCH_SCOPE_USERS",
129+
"SEARCH_SCOPE_WIKI_BLOBS",
130+
"USER_AGENT",
131+
"VISIBILITY_INTERNAL",
132+
"VISIBILITY_PRIVATE",
133+
"VISIBILITY_PUBLIC",
141134
]
142-
__all__.extend(_DEPRECATED)

gitlab/exceptions.py

+74-1
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,77 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any:
342342
return wrap
343343

344344

345-
__all__ = [name for name in dir() if name.endswith("Error")]
345+
# Export manually to keep mypy happy
346+
__all__ = [
347+
"GitlabActivateError",
348+
"GitlabAttachFileError",
349+
"GitlabAuthenticationError",
350+
"GitlabBanError",
351+
"GitlabBlockError",
352+
"GitlabBuildCancelError",
353+
"GitlabBuildEraseError",
354+
"GitlabBuildPlayError",
355+
"GitlabBuildRetryError",
356+
"GitlabCancelError",
357+
"GitlabCherryPickError",
358+
"GitlabCiLintError",
359+
"GitlabConnectionError",
360+
"GitlabCreateError",
361+
"GitlabDeactivateError",
362+
"GitlabDeleteError",
363+
"GitlabDeploymentApprovalError",
364+
"GitlabError",
365+
"GitlabFollowError",
366+
"GitlabGetError",
367+
"GitlabGroupTransferError",
368+
"GitlabHeadError",
369+
"GitlabHousekeepingError",
370+
"GitlabHttpError",
371+
"GitlabImportError",
372+
"GitlabInvitationError",
373+
"GitlabJobCancelError",
374+
"GitlabJobEraseError",
375+
"GitlabJobPlayError",
376+
"GitlabJobRetryError",
377+
"GitlabLicenseError",
378+
"GitlabListError",
379+
"GitlabMRApprovalError",
380+
"GitlabMRClosedError",
381+
"GitlabMRForbiddenError",
382+
"GitlabMROnBuildSuccessError",
383+
"GitlabMRRebaseError",
384+
"GitlabMRResetApprovalError",
385+
"GitlabMarkdownError",
386+
"GitlabOperationError",
387+
"GitlabOwnershipError",
388+
"GitlabParsingError",
389+
"GitlabPipelineCancelError",
390+
"GitlabPipelinePlayError",
391+
"GitlabPipelineRetryError",
392+
"GitlabProjectDeployKeyError",
393+
"GitlabPromoteError",
394+
"GitlabProtectError",
395+
"GitlabRenderError",
396+
"GitlabRepairError",
397+
"GitlabRestoreError",
398+
"GitlabRetryError",
399+
"GitlabRevertError",
400+
"GitlabSearchError",
401+
"GitlabSetError",
402+
"GitlabStopError",
403+
"GitlabSubscribeError",
404+
"GitlabTimeTrackingError",
405+
"GitlabTodoError",
406+
"GitlabTopicMergeError",
407+
"GitlabTransferProjectError",
408+
"GitlabUnbanError",
409+
"GitlabUnblockError",
410+
"GitlabUnfollowError",
411+
"GitlabUnsubscribeError",
412+
"GitlabUpdateError",
413+
"GitlabUploadError",
414+
"GitlabUserApproveError",
415+
"GitlabUserRejectError",
416+
"GitlabVerifyError",
417+
"RedirectError",
418+
]

tests/unit/meta/test_v4_objects_imported.py renamed to tests/unit/meta/test_imports.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66
import pkgutil
77
from typing import Set
88

9+
import gitlab.exceptions
910
import gitlab.v4.objects
1011

1112

12-
def test_verify_v4_objects_imported() -> None:
13+
def test_all_exceptions_imports_are_exported() -> None:
14+
assert gitlab.exceptions.__all__ == sorted(
15+
[
16+
name
17+
for name in dir(gitlab.exceptions)
18+
if name.endswith("Error") and not name.startswith("_")
19+
]
20+
)
21+
22+
23+
def test_all_v4_objects_are_imported() -> None:
1324
assert len(gitlab.v4.objects.__path__) == 1
1425

1526
init_files: Set[str] = set()

tests/unit/test_gitlab.py

-10
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,6 @@ def test_gitlab_user_agent(kwargs, expected_agent):
336336
assert gl.headers["User-Agent"] == expected_agent
337337

338338

339-
def test_gitlab_deprecated_global_const_warns():
340-
with pytest.deprecated_call(
341-
match="'gitlab.NO_ACCESS' is deprecated.*constants in the 'gitlab.const'"
342-
) as record:
343-
no_access = gitlab.NO_ACCESS
344-
345-
assert len(record) == 1
346-
assert no_access == 0
347-
348-
349339
def test_gitlab_enum_const_does_not_warn(recwarn):
350340
no_access = gitlab.const.AccessLevel.NO_ACCESS
351341

0 commit comments

Comments
 (0)