Skip to content

Commit

Permalink
refactor(const): remove deprecated global constant import
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Constants defined in `gitlab.const` can no longer be imported globally from `gitlab`.
Import them from `gitlab.const` instead.
  • Loading branch information
nejch authored and JohnVillalovos committed Aug 27, 2023
1 parent 4abcd17 commit e4a1f6e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 70 deletions.
21 changes: 0 additions & 21 deletions gitlab/__init__.py
Expand Up @@ -17,10 +17,8 @@
"""Wrapper for the GitLab API."""

import warnings
from typing import Any

import gitlab.config # noqa: F401
from gitlab import utils as _utils
from gitlab._version import ( # noqa: F401
__author__,
__copyright__,
Expand All @@ -35,24 +33,6 @@
warnings.filterwarnings("default", category=DeprecationWarning, module="^gitlab")


# NOTE(jlvillal): We are deprecating access to the gitlab.const values which
# were previously imported into this namespace by the
# 'from gitlab.const import *' statement.
def __getattr__(name: str) -> Any:
# Deprecate direct access to constants without namespace
if name in gitlab.const._DEPRECATED:
_utils.warn(
message=(
f"\nDirect access to constants as 'gitlab.{name}' is deprecated and "
f"will be removed in a future major python-gitlab release. Please "
f"see the usage of constants in the 'gitlab.const' module instead."
),
category=DeprecationWarning,
)
return getattr(gitlab.const, name)
raise AttributeError(f"module {__name__} has no attribute {name}")


__all__ = [
"__author__",
"__copyright__",
Expand All @@ -63,5 +43,4 @@ def __getattr__(name: str) -> Any:
"Gitlab",
"GitlabList",
]
__all__.extend(gitlab.const._DEPRECATED)
__all__.extend(gitlab.exceptions.__all__)
66 changes: 29 additions & 37 deletions gitlab/const.py
Expand Up @@ -2,42 +2,6 @@

from gitlab._version import __title__, __version__

# NOTE(jlvillal): '_DEPRECATED' only affects users accessing constants via the
# top-level gitlab.* namespace. See 'gitlab/__init__.py:__getattr__()' for the
# consumer of '_DEPRECATED' For example 'x = gitlab.NO_ACCESS'. We want users
# to instead use constants by doing code like: gitlab.const.NO_ACCESS.
_DEPRECATED = [
"ADMIN_ACCESS",
"DEFAULT_URL",
"DEVELOPER_ACCESS",
"GUEST_ACCESS",
"MAINTAINER_ACCESS",
"MINIMAL_ACCESS",
"NO_ACCESS",
"NOTIFICATION_LEVEL_CUSTOM",
"NOTIFICATION_LEVEL_DISABLED",
"NOTIFICATION_LEVEL_GLOBAL",
"NOTIFICATION_LEVEL_MENTION",
"NOTIFICATION_LEVEL_PARTICIPATING",
"NOTIFICATION_LEVEL_WATCH",
"OWNER_ACCESS",
"REPORTER_ACCESS",
"SEARCH_SCOPE_BLOBS",
"SEARCH_SCOPE_COMMITS",
"SEARCH_SCOPE_GLOBAL_SNIPPET_TITLES",
"SEARCH_SCOPE_ISSUES",
"SEARCH_SCOPE_MERGE_REQUESTS",
"SEARCH_SCOPE_MILESTONES",
"SEARCH_SCOPE_PROJECT_NOTES",
"SEARCH_SCOPE_PROJECTS",
"SEARCH_SCOPE_USERS",
"SEARCH_SCOPE_WIKI_BLOBS",
"USER_AGENT",
"VISIBILITY_INTERNAL",
"VISIBILITY_PRIVATE",
"VISIBILITY_PUBLIC",
]


class GitlabEnum(str, Enum):
"""An enum mixed in with str to make it JSON-serializable."""
Expand Down Expand Up @@ -138,5 +102,33 @@ class SearchScope(GitlabEnum):
"Visibility",
"NotificationLevel",
"SearchScope",
"ADMIN_ACCESS",
"DEFAULT_URL",
"DEVELOPER_ACCESS",
"GUEST_ACCESS",
"MAINTAINER_ACCESS",
"MINIMAL_ACCESS",
"NO_ACCESS",
"NOTIFICATION_LEVEL_CUSTOM",
"NOTIFICATION_LEVEL_DISABLED",
"NOTIFICATION_LEVEL_GLOBAL",
"NOTIFICATION_LEVEL_MENTION",
"NOTIFICATION_LEVEL_PARTICIPATING",
"NOTIFICATION_LEVEL_WATCH",
"OWNER_ACCESS",
"REPORTER_ACCESS",
"SEARCH_SCOPE_BLOBS",
"SEARCH_SCOPE_COMMITS",
"SEARCH_SCOPE_GLOBAL_SNIPPET_TITLES",
"SEARCH_SCOPE_ISSUES",
"SEARCH_SCOPE_MERGE_REQUESTS",
"SEARCH_SCOPE_MILESTONES",
"SEARCH_SCOPE_PROJECT_NOTES",
"SEARCH_SCOPE_PROJECTS",
"SEARCH_SCOPE_USERS",
"SEARCH_SCOPE_WIKI_BLOBS",
"USER_AGENT",
"VISIBILITY_INTERNAL",
"VISIBILITY_PRIVATE",
"VISIBILITY_PUBLIC",
]
__all__.extend(_DEPRECATED)
75 changes: 74 additions & 1 deletion gitlab/exceptions.py
Expand Up @@ -342,4 +342,77 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any:
return wrap


__all__ = [name for name in dir() if name.endswith("Error")]
# Export manually to keep mypy happy
__all__ = [
"GitlabActivateError",
"GitlabAttachFileError",
"GitlabAuthenticationError",
"GitlabBanError",
"GitlabBlockError",
"GitlabBuildCancelError",
"GitlabBuildEraseError",
"GitlabBuildPlayError",
"GitlabBuildRetryError",
"GitlabCancelError",
"GitlabCherryPickError",
"GitlabCiLintError",
"GitlabConnectionError",
"GitlabCreateError",
"GitlabDeactivateError",
"GitlabDeleteError",
"GitlabDeploymentApprovalError",
"GitlabError",
"GitlabFollowError",
"GitlabGetError",
"GitlabGroupTransferError",
"GitlabHeadError",
"GitlabHousekeepingError",
"GitlabHttpError",
"GitlabImportError",
"GitlabInvitationError",
"GitlabJobCancelError",
"GitlabJobEraseError",
"GitlabJobPlayError",
"GitlabJobRetryError",
"GitlabLicenseError",
"GitlabListError",
"GitlabMRApprovalError",
"GitlabMRClosedError",
"GitlabMRForbiddenError",
"GitlabMROnBuildSuccessError",
"GitlabMRRebaseError",
"GitlabMRResetApprovalError",
"GitlabMarkdownError",
"GitlabOperationError",
"GitlabOwnershipError",
"GitlabParsingError",
"GitlabPipelineCancelError",
"GitlabPipelinePlayError",
"GitlabPipelineRetryError",
"GitlabProjectDeployKeyError",
"GitlabPromoteError",
"GitlabProtectError",
"GitlabRenderError",
"GitlabRepairError",
"GitlabRestoreError",
"GitlabRetryError",
"GitlabRevertError",
"GitlabSearchError",
"GitlabSetError",
"GitlabStopError",
"GitlabSubscribeError",
"GitlabTimeTrackingError",
"GitlabTodoError",
"GitlabTopicMergeError",
"GitlabTransferProjectError",
"GitlabUnbanError",
"GitlabUnblockError",
"GitlabUnfollowError",
"GitlabUnsubscribeError",
"GitlabUpdateError",
"GitlabUploadError",
"GitlabUserApproveError",
"GitlabUserRejectError",
"GitlabVerifyError",
"RedirectError",
]
Expand Up @@ -6,10 +6,21 @@
import pkgutil
from typing import Set

import gitlab.exceptions
import gitlab.v4.objects


def test_verify_v4_objects_imported() -> None:
def test_all_exceptions_imports_are_exported() -> None:
assert gitlab.exceptions.__all__ == sorted(
[
name
for name in dir(gitlab.exceptions)
if name.endswith("Error") and not name.startswith("_")
]
)


def test_all_v4_objects_are_imported() -> None:
assert len(gitlab.v4.objects.__path__) == 1

init_files: Set[str] = set()
Expand Down
10 changes: 0 additions & 10 deletions tests/unit/test_gitlab.py
Expand Up @@ -336,16 +336,6 @@ def test_gitlab_user_agent(kwargs, expected_agent):
assert gl.headers["User-Agent"] == expected_agent


def test_gitlab_deprecated_global_const_warns():
with pytest.deprecated_call(
match="'gitlab.NO_ACCESS' is deprecated.*constants in the 'gitlab.const'"
) as record:
no_access = gitlab.NO_ACCESS

assert len(record) == 1
assert no_access == 0


def test_gitlab_enum_const_does_not_warn(recwarn):
no_access = gitlab.const.AccessLevel.NO_ACCESS

Expand Down

0 comments on commit e4a1f6e

Please sign in to comment.