Skip to content

Commit a2302d7

Browse files
chore: reduce usage of 'from mod_foo import class_foo'
Reduce usage of doing: from SOME_MODULE import SOME_CLASS_OR_FUNCTION Instead use: import SOME_MODULE And then use the full module name. This improves readability and makes obvious to the reader where the class or function has come from.
1 parent f1ef565 commit a2302d7

File tree

6 files changed

+29
-30
lines changed

6 files changed

+29
-30
lines changed

gitlab/_backends/requests_backend.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
from typing import Any, BinaryIO, Dict, Optional, TYPE_CHECKING, Union
55

66
import requests
7-
from requests import PreparedRequest
8-
from requests.auth import AuthBase
9-
from requests.structures import CaseInsensitiveDict
107
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
118

129
from . import protocol
@@ -17,24 +14,24 @@ def __init__(self, token: str):
1714
self.token = token
1815

1916

20-
class OAuthTokenAuth(TokenAuth, AuthBase):
21-
def __call__(self, r: PreparedRequest) -> PreparedRequest:
17+
class OAuthTokenAuth(TokenAuth, requests.auth.AuthBase):
18+
def __call__(self, r: requests.PreparedRequest) -> requests.PreparedRequest:
2219
r.headers["Authorization"] = f"Bearer {self.token}"
2320
r.headers.pop("PRIVATE-TOKEN", None)
2421
r.headers.pop("JOB-TOKEN", None)
2522
return r
2623

2724

28-
class PrivateTokenAuth(TokenAuth, AuthBase):
29-
def __call__(self, r: PreparedRequest) -> PreparedRequest:
25+
class PrivateTokenAuth(TokenAuth, requests.auth.AuthBase):
26+
def __call__(self, r: requests.PreparedRequest) -> requests.PreparedRequest:
3027
r.headers["PRIVATE-TOKEN"] = self.token
3128
r.headers.pop("JOB-TOKEN", None)
3229
r.headers.pop("Authorization", None)
3330
return r
3431

3532

36-
class JobTokenAuth(TokenAuth, AuthBase):
37-
def __call__(self, r: PreparedRequest) -> PreparedRequest:
33+
class JobTokenAuth(TokenAuth, requests.auth.AuthBase):
34+
def __call__(self, r: requests.PreparedRequest) -> requests.PreparedRequest:
3835
r.headers["JOB-TOKEN"] = self.token
3936
r.headers.pop("PRIVATE-TOKEN", None)
4037
r.headers.pop("Authorization", None)
@@ -68,7 +65,7 @@ def status_code(self) -> int:
6865
return self._response.status_code
6966

7067
@property
71-
def headers(self) -> CaseInsensitiveDict[str]:
68+
def headers(self) -> requests.structures.CaseInsensitiveDict[str]:
7269
return self._response.headers
7370

7471
@property

gitlab/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Union,
2020
)
2121

22-
from requests.structures import CaseInsensitiveDict
22+
import requests
2323

2424
import gitlab.config
2525
from gitlab.base import RESTObject
@@ -115,7 +115,7 @@ def die(msg: str, e: Optional[Exception] = None) -> None:
115115
def gitlab_resource_to_cls(
116116
gitlab_resource: str, namespace: ModuleType
117117
) -> Type[RESTObject]:
118-
classes = CaseInsensitiveDict(namespace.__dict__)
118+
classes = requests.structures.CaseInsensitiveDict(namespace.__dict__)
119119
lowercase_class = gitlab_resource.replace("-", "")
120120
class_type = classes[lowercase_class]
121121
if TYPE_CHECKING:

gitlab/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import time
6+
import urllib
67
from typing import (
78
Any,
89
BinaryIO,
@@ -15,7 +16,6 @@
1516
TYPE_CHECKING,
1617
Union,
1718
)
18-
from urllib import parse
1919

2020
import requests
2121

@@ -517,10 +517,10 @@ def _set_auth_info(self) -> None:
517517
)
518518

519519
def enable_debug(self, mask_credentials: bool = True) -> None:
520+
import http.client
520521
import logging
521-
from http import client
522522

523-
client.HTTPConnection.debuglevel = 1
523+
http.client.HTTPConnection.debuglevel = 1
524524
logging.basicConfig()
525525
logger = logging.getLogger()
526526
logger.setLevel(logging.DEBUG)
@@ -538,7 +538,7 @@ def enable_debug(self, mask_credentials: bool = True) -> None:
538538
def print_as_log(*args: Any) -> None:
539539
httpclient_log.log(logging.DEBUG, " ".join(args))
540540

541-
setattr(client, "print", print_as_log)
541+
setattr(http.client, "print", print_as_log)
542542

543543
if not mask_credentials:
544544
return
@@ -684,11 +684,11 @@ def http_request(
684684
raw_url = self._build_url(path)
685685

686686
# parse user-provided URL params to ensure we don't add our own duplicates
687-
parsed = parse.urlparse(raw_url)
688-
params = parse.parse_qs(parsed.query)
687+
parsed = urllib.parse.urlparse(raw_url)
688+
params = urllib.parse.parse_qs(parsed.query)
689689
utils.copy_dict(src=query_data, dest=params)
690690

691-
url = parse.urlunparse(parsed._replace(query=""))
691+
url = urllib.parse.urlunparse(parsed._replace(query=""))
692692

693693
# Deal with kwargs: by default a user uses kwargs to send data to the
694694
# gitlab server, but this generates problems (python keyword conflicts

gitlab/config.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import configparser
22
import os
3+
import pathlib
34
import shlex
45
import subprocess
5-
from os.path import expanduser, expandvars
6-
from pathlib import Path
76
from typing import List, Optional, Union
87

98
from gitlab.const import USER_AGENT
109

1110
_DEFAULT_FILES: List[str] = [
1211
"/etc/python-gitlab.cfg",
13-
str(Path.home() / ".python-gitlab.cfg"),
12+
str(pathlib.Path.home() / ".python-gitlab.cfg"),
1413
]
1514

1615
HELPER_PREFIX = "helper:"
@@ -20,8 +19,8 @@
2019
_CONFIG_PARSER_ERRORS = (configparser.NoOptionError, configparser.NoSectionError)
2120

2221

23-
def _resolve_file(filepath: Union[Path, str]) -> str:
24-
resolved = Path(filepath).resolve(strict=True)
22+
def _resolve_file(filepath: Union[pathlib.Path, str]) -> str:
23+
resolved = pathlib.Path(filepath).resolve(strict=True)
2524
return str(resolved)
2625

2726

@@ -269,7 +268,10 @@ def _get_values_from_helper(self) -> None:
269268
continue
270269

271270
helper = value[len(HELPER_PREFIX) :].strip()
272-
commmand = [expanduser(expandvars(token)) for token in shlex.split(helper)]
271+
commmand = [
272+
os.path.expanduser(os.path.expandvars(token))
273+
for token in shlex.split(helper)
274+
]
273275

274276
try:
275277
value = (

gitlab/const.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from enum import Enum, IntEnum
1+
import enum
22

33
from gitlab._version import __title__, __version__
44

55

6-
class GitlabEnum(str, Enum):
6+
class GitlabEnum(str, enum.Enum):
77
"""An enum mixed in with str to make it JSON-serializable."""
88

99

1010
# https://gitlab.com/gitlab-org/gitlab/-/blob/e97357824bedf007e75f8782259fe07435b64fbb/lib/gitlab/access.rb#L12-18
11-
class AccessLevel(IntEnum):
11+
class AccessLevel(enum.IntEnum):
1212
NO_ACCESS: int = 0
1313
MINIMAL_ACCESS: int = 5
1414
GUEST: int = 10

gitlab/v4/objects/packages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
https://docs.gitlab.com/ee/user/packages/generic_packages/
55
"""
66

7-
from pathlib import Path
7+
import pathlib
88
from typing import (
99
Any,
1010
BinaryIO,
@@ -57,7 +57,7 @@ def upload(
5757
package_name: str,
5858
package_version: str,
5959
file_name: str,
60-
path: Optional[Union[str, Path]] = None,
60+
path: Optional[Union[str, pathlib.Path]] = None,
6161
select: Optional[str] = None,
6262
data: Optional[Union[bytes, BinaryIO]] = None,
6363
**kwargs: Any,

0 commit comments

Comments
 (0)